fix(gemini): preserve thoughtSignature emitted on a separate thought part

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
This commit is contained in:
jeanhabib 2026-09-09 10:26:28 -03:00
parent 328a5f5d60
commit d4e0ee35b0
2 changed files with 85 additions and 1 deletions

View file

@ -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.

View file

@ -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