fix: preserve Gemini thought_signature in LiteLLM multi-turn tool calls#1888
Open
giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
Open
Conversation
When using Gemini thinking models (e.g., gemini-2.5-flash) through the LiteLLM model provider, multi-turn conversations with tool calls fail because thought_signature is lost during the response-to-request round trip. LiteLLM encodes Gemini's thought_signature into the tool call ID using a __thought__ separator. The OpenAI parent format_chunk passes this through as-is, but the signature is never extracted into Strands' reasoningSignature field, which the streaming layer already supports. Changes: - Override format_chunk in LiteLLMModel to detect __thought__ in tool call IDs and provider_specific_fields, extracting the signature into reasoningSignature for proper streaming layer storage - Override format_request_message_tool_call to re-encode reasoningSignature back into the tool call ID when it is not already present, ensuring LiteLLM can reconstruct the Gemini-native format - Add 7 unit tests covering extraction from ID, extraction from provider_specific_fields, no-signature passthrough, encoding, double-encode prevention, and full round-trip preservation Closes strands-agents#1764
5d7f638 to
5fe1504
Compare
Contributor
Author
|
Friendly ping — preserves the Gemini |
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.
Issue
Closes #1764
Description
When using Gemini thinking models (e.g.,
gemini-2.5-flash,gemini-3-flash) through the LiteLLM model provider, multi-turn conversations with tool calls fail with:Root Cause
Gemini thinking models require a
thought_signatureon each function call in multi-turn conversations. LiteLLM encodes this signature into the tool call ID using a__thought__separator (e.g.,call_abc__thought__base64sig). However, Strands'OpenAIModel.format_chunk()passes the encoded ID through without extracting the signature into thereasoningSignaturefield that the streaming layer already supports.While the encoded ID survives the round-trip in simple cases, the signature must also be explicitly stored in
reasoningSignatureso that:provider_specific_fields.thought_signature(an alternative LiteLLM transport) is also capturedChanges
src/strands/models/litellm.py:format_chunkto detect__thought__in tool call IDs and/orprovider_specific_fields.thought_signature, extracting the signature intoreasoningSignatureon thecontentBlockStarteventformat_request_message_tool_callto re-encodereasoningSignatureback into the tool call ID when not already present, ensuring LiteLLM can reconstruct the Gemini-native formattests/strands/models/test_litellm.py:provider_specific_fields, no-signature passthrough, signature encoding, double-encode prevention, and full round-trip preservationChecklist