Skip to content

feat(openrouter): native content-block streaming events#7

Open
LennyMalcolm0 wants to merge 3 commits into
masterfrom
devasign-clone/pr-38030
Open

feat(openrouter): native content-block streaming events#7
LennyMalcolm0 wants to merge 3 commits into
masterfrom
devasign-clone/pr-38030

Conversation

@LennyMalcolm0

@LennyMalcolm0 LennyMalcolm0 commented Jul 8, 2026

Copy link
Copy Markdown
Owner

Stacked on #14 (core BlockStreamTracker + Anthropic), which it branches from. Depends only on the core primitive — sibling of the OpenAI chain. Retargets to master once #14 lands.

Gives ChatOpenRouter native _stream_chat_model_events / _astream_chat_model_events hooks so stream_events(version="v3") maps OpenRouter's stream directly to content-block events.

How it works

A bespoke converter (convert_openrouter_stream / async twin), sibling of the groq converter, builds text, reasoning, and tool-call blocks directly from OpenRouter's raw OpenAI-shaped delta, feeding the shared BlockStreamTracker. Tool-call args stream incrementally and finalize to a parsed dict. It does not depend on langchain-openai — it reuses only OpenRouter's own _create_usage_metadata.

OpenRouter specifics it handles: error chunks are propagated as a ValueError (matching _stream); top-level usage is accumulated; and OpenRouter's cost surfacing (cost/cost_details) plus native_finish_reason/model_name/etc. are carried into message-finish metadata, matching the compat bridge.

The win over the bridge

OpenRouter's chunk parser leaves reasoning in additional_kwargs; the native converter surfaces it as a reasoning block (and tool calls as tool_call blocks).

Worth careful review

  • Error-chunk propagation and the cost/finish metadata parity with _stream (a test asserts cost survives to message-finish).
  • The _StreamState helper shares per-chunk coercion/usage/error logic across the sync/async twins (no event-sequence drift).
  • Scope cut: reasoning_details not yet surfaced as blocks (noted in-code).
  • message_id keyword-only; message-start carries the LangChain run id.

Cloned from langchain-ai#38030 for DevAsign stress testing.

@devasign-test-app

devasign-test-app Bot commented Jul 9, 2026

Copy link
Copy Markdown

AI review failed to complete. Please retry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants