fix: preserve reasoning content signature from LiteLLM thinking models#1789
fix: preserve reasoning content signature from LiteLLM thinking models#1789giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
Conversation
When streaming responses from thinking models (e.g., Gemini) via LiteLLM, the reasoning content signature was silently dropped. The LiteLLM adapter's _process_choice_content only emitted reasoning text from delta.reasoning_content but never captured the signature from delta.thinking.signature. This patch adds a check for the thinking.signature attribute on each streaming delta and emits a contentBlockDelta with reasoningContent.signature so the event loop can accumulate and store the signature in the final content block. An isinstance(sig, str) guard prevents unittest.mock.Mock objects from triggering false positives during testing. Fixes strands-agents#1764 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
All CI checks pass. Ready for review. |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
zastrowm
left a comment
There was a problem hiding this comment.
Is this something we could have an integ test for? Would help prove out that it works against a live model
| and "reasoningContent" in e["contentBlockDelta"]["delta"] | ||
| and "signature" in e["contentBlockDelta"]["delta"]["reasoningContent"] | ||
| ] | ||
| assert len(signature_deltas) == 1 |
There was a problem hiding this comment.
Suggestion: Consider adding a test case where the signature arrives when data_type is not already "reasoning_content" (e.g., signature arrives before any reasoning text, or after text content). This would cover lines 419-420 in the implementation (the for chunk in chunks loop that emits content block start/stop events during content type switches).
Current test flow:
- Chunk 1:
reasoning_content="Let me think..."→ setsdata_type="reasoning_content" - Chunk 2: signature arrives →
_stream_switch_content("reasoning_content", "reasoning_content")returns emptychunks
A scenario where signature arrives first would exercise the content switching path. That said, this is a minor gap and the main functionality is well-tested.
|
Assessment: Approve ✅ Clean fix that correctly preserves reasoning signatures from LiteLLM's Review Details
Thanks for the well-documented PR description - the root cause analysis was particularly helpful! |
Problem
When streaming responses from thinking models (e.g., Gemini) via LiteLLM, the reasoning content signature is silently dropped. The LiteLLM adapter's
_process_choice_contentonly emits reasoning text fromdelta.reasoning_contentbut never captures the signature fromdelta.thinking.signature.The event loop in
streaming.pyalready correctly handlesreasoningContent.signaturedeltas (lines 240-248, 314-315), but the LiteLLM model provider never emits them.Root Cause
LiteLLM provides reasoning signatures via a separate
thinkingattribute on stream deltas (delta.thinking.signature), distinct fromdelta.reasoning_content(which only contains the text). The LiteLLM model provider's_process_choice_contentmethod only processedreasoning_contenttext, never checking for thethinking.signatureattribute.Fix
Added a check in
_process_choice_content()for thethinking.signatureattribute on each streaming delta. When a valid string signature is found, acontentBlockDeltawithreasoningContent.signatureis emitted so the event loop can accumulate and store it in the final content block.An
isinstance(sig, str)guard prevents false positives from unittest.mock.Mock objects that auto-create attributes.Testing
test_stream_preserves_thinking_signaturethat simulates a 3-chunk streaming response with reasoning text and signatureFixes #1764