Skip to content

fix(LiteLlm): preserve signature-only blocks for streaming thinking aggregation#6311

Merged
copybara-service[bot] merged 1 commit into
mainfrom
copybara/933780190
Jul 8, 2026
Merged

fix(LiteLlm): preserve signature-only blocks for streaming thinking aggregation#6311
copybara-service[bot] merged 1 commit into
mainfrom
copybara/933780190

Conversation

@copybara-service

Copy link
Copy Markdown

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

@copybara-service copybara-service Bot force-pushed the copybara/933780190 branch 4 times, most recently from 7f25f2a to 710f6be Compare July 8, 2026 06:51
…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
@copybara-service copybara-service Bot force-pushed the copybara/933780190 branch from 710f6be to 78d1957 Compare July 8, 2026 06:52
@copybara-service copybara-service Bot merged commit 78d1957 into main Jul 8, 2026
@copybara-service copybara-service Bot deleted the copybara/933780190 branch July 8, 2026 06:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant