fix: preserve reasoning content signature from LiteLLM thinking models#1789
Open
giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
Open
fix: preserve reasoning content signature from LiteLLM thinking models#1789giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
giulio-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>
Author
|
All CI checks pass. Ready for review. |
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.
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