fix(LiteLlm): preserve signature-only blocks for streaming thinking aggregation#6311
Merged
Conversation
7f25f2a to
710f6be
Compare
…thinking aggregation Key changes: * **Streaming Signature Preservation:** When Anthropic thinking mode is enabled and responses are streamed via LiteLLM/OpenAI format, Anthropic splits thinking blocks across deltas: text-only chunks first (`signature: None`), followed by a final signature-only chunk at `block_stop` with empty text (`thinking: ""`). This change ensures signature-only deltas are preserved rather than discarded. * **Signal Detection & Part Conversion:** Updated `_has_meaningful_signal` to recognize `thinking_blocks` so signature-only deltas are not dropped as empty noise. Updated `_convert_reasoning_value_to_parts` to preserve blocks when `signature` is present, storing the cryptographic signature on `Part.thought_signature`. * **Outbound Thought Aggregation:** Added `_aggregate_streaming_thought_parts` helper in `lite_llm.py` and integrated it into `_content_to_message_param` (Anthropic branch). It joins fragmented text chunks and attaches the cryptographic signature from whichever part carries it, emitting a clean `thinking_blocks` entry for multi-turn session history. * **Evidence & Verification:** Added unit test `test_aggregate_streaming_thought_parts` and updated `test_content_to_message_param_anthropic_aggregates_streaming_split_thinking` to verify that streaming deltas (split text chunks + signature chunk `ErEDClsIDBACGAIfull`) are re-joined into a valid Anthropic `thinking_block` with base64 signature (`RXJFRENsc0lEQkFDR0FJZnVsbA==`). Verified 321 unit tests and 957 internal TAP targets pass 100%. Note for callers: This fixes continuity of Anthropic extended thinking across streaming responses that include tool use or multi-turn chats. Previously, after a tool call or first streaming turn, thinking blocks were rejected by Anthropic API with a 400 Bad Request error (`missing thinking signature`) because the signature was discarded during streaming. Fixes: #4801 Closes: #5437 Co-authored-by: Yi Liu <yiliuly@google.com> COPYBARA_INTEGRATE_REVIEW=#5437 from gurjot-05:fix/anthropic-thinking-streaming-continuity 0ce04f1 PiperOrigin-RevId: 944305912
710f6be to
78d1957
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.
fix(LiteLlm): preserve signature-only blocks for streaming thinking aggregation
Merge #5437
Original PR by Gurjot Singh gurjot14143132@gmail.com
Co-authored-by: Yi Liu yiliuly@google.com