feat(openrouter): native content-block streaming events#38030
Open
Nick Hollon (nick-hollon-lc) wants to merge 1 commit into
Open
feat(openrouter): native content-block streaming events#38030Nick Hollon (nick-hollon-lc) wants to merge 1 commit into
Nick Hollon (nick-hollon-lc) wants to merge 1 commit into
Conversation
Comment on lines
+170
to
+171
| if choice.get("finish_reason"): | ||
| state.record_finish(chunk_dict, choice) |
Contributor
There was a problem hiding this comment.
🟡 Preserve streamed logprobs metadata
When the caller requests streaming logprobs, OpenRouter includes them on choice["logprobs"]. The existing _stream path copies that into generation_info, and the v3 compat bridge merges it into message-finish.metadata; after this native hook is added, stream_events(version="v3", logprobs=True, ...) takes this converter instead and line 170 only records finish metadata, so those logprobs are dropped from the event stream entirely. Please accumulate/propagate choice.get("logprobs") here (and in the async twin) to keep parity with the bridge.
(Refers to lines 170-171)
Your feedback helps Open SWE learn. React with 👍 or 👎 to tell us if this review comment was useful.
a3ef4d9 to
f7d7e0b
Compare
c2ac312 to
891eae9
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Gives
ChatOpenRouternative_stream_chat_model_events/_astream_chat_model_eventshooks sostream_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 sharedBlockStreamTracker. Tool-call args stream incrementally and finalize to a parsed dict. It does not depend onlangchain-openai— it reuses only OpenRouter's own_create_usage_metadata.OpenRouter specifics it handles: error chunks are propagated as a
ValueError(matching_stream); top-levelusageis accumulated; and OpenRouter's cost surfacing (cost/cost_details) plusnative_finish_reason/model_name/etc. are carried intomessage-finishmetadata, matching the compat bridge.The win over the bridge
OpenRouter's chunk parser leaves
reasoninginadditional_kwargs; the native converter surfaces it as areasoningblock (and tool calls astool_callblocks).Worth careful review
_stream(a test asserts cost survives tomessage-finish)._StreamStatehelper shares per-chunk coercion/usage/error logic across the sync/async twins (no event-sequence drift).reasoning_detailsnot yet surfaced as blocks (noted in-code).message_idkeyword-only;message-startcarries the LangChain run id.