fix: join streamed reasoning into one thought part per block - #6896
Open
benclarkeio wants to merge 1 commit into
Open
fix: join streamed reasoning into one thought part per block#6896benclarkeio wants to merge 1 commit into
benclarkeio wants to merge 1 commit into
Conversation
Both stream finalizers passed the reasoning buffer through unjoined, so the aggregated response held one thought part per delta while text was joined into one. `_aggregate_streaming_thought_parts` already does the joining and splits on `thought_signature`, so Anthropic block boundaries survive.
9 tasks
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.
Fixes #6895
Problem
Both stream finalizers join the text buffer but pass the reasoning buffer through as-is:
So the aggregated response — and the session event written from it — holds one
types.Part(thought=True)per streamed delta. With xAI, whose deltas are per-token, one thought becomes ~40 parts. The same call withstream=Falseproduces a single part, so the stored shape depends on the transport rather than on what the model said.Fix
Call
_aggregate_streaming_thought_partsin both finalizers. It already exists for this, and its docstring already claims session history as a target — only the outbound Anthropic path in_content_to_message_paramwas wired to it.Signature boundaries are preserved: Anthropic closes each thinking block with a signature-only delta, and a signature only matches the text of its own block, so the helper splits there rather than merging everything adjacent. Providers that send no signature collapse to one part, which is what they mean.
Partial responses are untouched — they keep yielding one part per delta, which is what makes live streaming work.
Testing
Two tests added:
test_streaming_reasoning_assembled_from_many_fragments— 50reasoning_contentdeltas arrive as 50 partials and aggregate to one thought part carrying the joined text.test_streaming_reasoning_keeps_thinking_block_boundaries— two Anthropic thinking blocks, each closed by a signature-only delta, aggregate to two parts with their own signatures.Both fail before the change.
tests/unittests/models/test_litellm.pypasses in full (405 passed). Formatted withpyinkper the repo config.