From d4e0ee35b07eac55b3b0f9a94b61fe66fef4fc62 Mon Sep 17 00:00:00 2001 From: jeanhabib Date: Wed, 9 Sep 2026 10:26:28 -0300 Subject: [PATCH] fix(gemini): preserve thoughtSignature emitted on a separate thought part MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Gemini 3+ may place the thought signature on a thought part that precedes the functionCall part, rather than on the functionCall part itself. `_transform_parts` only read `thoughtSignature` from parts containing `functionCall`, so in that shape the signature was dropped: never stored, never echoed back on the next turn. Downstream this surfaces as a hard 400 from the Gemini API on the second tool-calling turn: Function call is missing a thought_signature in functionCall parts. This is required for tools to work correctly Carry a pending signature forward from the thought part and consume it on the first function call that follows — in parallel calls only the first one carries a signature. A signature present on the functionCall part keeps precedence, so the existing behaviour is unchanged. Closes #25322 --- .../vertex_and_google_ai_studio_gemini.py | 13 +++- .../test_thought_signature_in_tool_call_id.py | 73 +++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) diff --git a/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py b/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py index 69fe5678de9..7b202b55823 100644 --- a/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py +++ b/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py @@ -1556,14 +1556,25 @@ class VertexGeminiConfig(VertexAIBaseConfig, BaseConfig): ]: function: ChatCompletionToolCallFunctionChunk | None = None _tools: Final[list[ChatCompletionToolCallChunk]] = [] + # Gemini 3+ may emit the thought signature on a preceding thought part + # instead of on the functionCall part itself. Carry it forward so it is + # not dropped; consume it on the first function call that follows, since + # in parallel calls only the first one carries a signature. + pending_thought_signature: str | None = None for part in parts: + if part.get("thought") and part.get("thoughtSignature"): + pending_thought_signature = part.get("thoughtSignature") + continue if "functionCall" in part: _function_chunk: ChatCompletionToolCallFunctionChunk = { "name": part["functionCall"]["name"], "arguments": json.dumps(part["functionCall"]["args"], ensure_ascii=False), } # Extract thought signature if present - thought_signature = part.get("thoughtSignature") + # Extract thought signature if present. Fall back to the one + # carried by a preceding thought part (Gemini 3+ places it there). + thought_signature = part.get("thoughtSignature") or pending_thought_signature + pending_thought_signature = None # Gemini 3.5+ returns a stable `id` per function call to enable # strict response matching. Preserve it as the OpenAI # tool_call_id so it can be echoed back unchanged. diff --git a/tests/test_litellm/llms/vertex_ai/gemini/test_thought_signature_in_tool_call_id.py b/tests/test_litellm/llms/vertex_ai/gemini/test_thought_signature_in_tool_call_id.py index 208cba519f3..6b3fd5ec952 100644 --- a/tests/test_litellm/llms/vertex_ai/gemini/test_thought_signature_in_tool_call_id.py +++ b/tests/test_litellm/llms/vertex_ai/gemini/test_thought_signature_in_tool_call_id.py @@ -285,3 +285,76 @@ def test_parallel_tool_calls_with_signatures(enable_preview_features): assert THOUGHT_SIGNATURE_SEPARATOR not in tools[1]["id"] sig2 = _get_thought_signature_from_tool({"id": tools[1]["id"], "type": "function"}) assert sig2 is None + + +def test_thought_signature_on_separate_thought_part_is_preserved(): + """Gemini 3+ places the signature on a thought part, not on the functionCall part. + + Regression for the case where `_transform_parts` only looked at the + functionCall part and silently dropped the signature, so it was never + echoed back on the next turn. + """ + test_signature = "CvsBAdHtim9thoughtPartSignature" + parts = [ + HttpxPartType(thought=True, thoughtSignature=test_signature), + HttpxPartType(functionCall={"name": "get_weather", "args": {"city": "Recife"}}), + ] + + _, tools, _ = VertexGeminiConfig._transform_parts( + parts=parts, + cumulative_tool_call_idx=0, + is_function_call=False, + ) + + assert tools is not None and len(tools) == 1 + assert ( + tools[0].get("provider_specific_fields", {}).get("thought_signature") + == test_signature + ) + + +def test_thought_signature_on_function_call_part_still_wins(): + """A signature on the functionCall part keeps precedence over a pending one.""" + own_signature = "CvsBAdHtim9onTheCallPart" + parts = [ + HttpxPartType(thought=True, thoughtSignature="CvsBAdHtim9onTheThoughtPart"), + HttpxPartType( + functionCall={"name": "get_weather", "args": {"city": "Recife"}}, + thoughtSignature=own_signature, + ), + ] + + _, tools, _ = VertexGeminiConfig._transform_parts( + parts=parts, + cumulative_tool_call_idx=0, + is_function_call=False, + ) + + assert tools is not None and len(tools) == 1 + assert ( + tools[0].get("provider_specific_fields", {}).get("thought_signature") + == own_signature + ) + + +def test_pending_thought_signature_is_consumed_once(): + """In parallel calls only the first function call carries a signature.""" + test_signature = "CvsBAdHtim9onlyForTheFirstCall" + parts = [ + HttpxPartType(thought=True, thoughtSignature=test_signature), + HttpxPartType(functionCall={"name": "get_weather", "args": {"city": "Recife"}}), + HttpxPartType(functionCall={"name": "get_time", "args": {"city": "Recife"}}), + ] + + _, tools, _ = VertexGeminiConfig._transform_parts( + parts=parts, + cumulative_tool_call_idx=0, + is_function_call=False, + ) + + assert tools is not None and len(tools) == 2 + assert ( + tools[0].get("provider_specific_fields", {}).get("thought_signature") + == test_signature + ) + assert tools[1].get("provider_specific_fields") is None