Repository navigation
fix(gemini): keep thoughtSignature on replayed tool calls - #668
Conversation
Gemini 3+ returns a thoughtSignature beside each signed functionCall part and rejects the follow-up request when the replayed call lost it. Decode it onto llm.ToolCall, persist it with the session, and write it back on the same functionCall part. The OpenAI-compatible wire marshals llm.ToolCall verbatim, so it drops the field before sending. Signed-off-by: Noah Khomer <108771853+noahkhomer18@users.noreply.github.com>
|
No actionable comments were generated in the recent review. Summary
Merge Risk:
|
There was a problem hiding this comment.
The core fix is correct: the signature is decoded with its functionCall part, persisted on the tool call, replayed on the same part, and parallel calls don't bleed into each other. It is also stripped before the OpenAI-compatible wire. No issues there.
One gap needs filling before merge: unsigned functionCall parts have no fallback.
Problem
The public Gemini 3 API (https://generativelanguage.googleapis.com/v1beta, phi's default endpoint) requires a signature on every functionCall part. The PR's own fixture [sig-a, bare] is the real shape of a Gemini 3 parallel turn: the second call arrives unsigned. phi replays it bare, and the public API answers 400.
Three paths hit this:
- Parallel calls — Gemini 3 signs only the first call of a parallel turn. The rest go back unsigned.
- Model switch — switching from another provider to Gemini 3. None of the history has a signature.
- Old sessions — sessions saved before this PR already lost their signatures. They stay broken until compaction rotates those messages out.
Suggested fix
In toAssistantMessage (internal/llm/gemini/client.go:264), when tc.ThoughtSignature == "", substitute the bypass sentinel skip_thought_signature_validator that Google documents for Gemini 3. The policy differs by endpoint:
- Public API: every unsigned call gets the sentinel.
- Vertex AI (
aiplatform.googleapis.com): only when the first call of the turn is unsigned. A signed-first parallel turn leaves secondary calls bare.
getURL (internal/llm/gemini/client.go:291) already tells the two endpoints apart, so the predicate is available. BuildRequest currently has no ModelConfig, so this needs either a parameter or a post-pass in Stream — your call.
Verification
- Unconfirmed: the public API returns 400 on an unsigned parallel call. To confirm, send one replay with two unsigned calls using a Gemini 3 key.
- Verified: the PR's core path (decode → persist → replay → strip on non-Gemini wires) is covered by tests and
make check.
Consider
Gate replay on same-model signatures. phi currently sends a gemini-3-pro signature to gemini-3-flash after a /model switch. Whether the API rejects a foreign signature is unconfirmed; if it does, this folds into the sentinel fix.
Gemini 3+ rejects a step whose first functionCall has no thoughtSignature. That happens for history from another provider and for sessions saved before signatures were kept. Give that call Google's documented skip_thought_signature_validator placeholder. Later calls of a parallel step stay bare, as Gemini returns them, and earlier turns are not validated, so they are left as they are. Signed-off-by: Noah Khomer <108771853+noahkhomer18@users.noreply.github.com>
|
Thanks @libinops, good catch on the unsigned history. Fixed in 7806eff, with one difference from the suggestion: the placeholder goes only on the first Parallel calls are accepted bare after a signed first call. I let each model make three parallel
That matches Google's docs ("only the first Your paths 2 and 3 were real. A session switched from another provider builds a step with no signatures at all. I dumped the request phi itself builds for that (two unsigned
Changes
On your "Consider": I replayed a gemini-3-flash-preview signature to gemini-4-neon, and a gemini-4-neon one to gemini-3.8-flash. Both returned 200 with the real signature in place, so a |
libinops
left a comment
There was a problem hiding this comment.
Thank you very much for your contribution.
Closes #665
Problem
Gemini 3+ returns a
thoughtSignaturebeside a signedfunctionCallpart. The adapter dropped it when the response was decoded (parthad no field for it), so the next request replayed the call unsigned, and Gemini can reject that with400 INVALID_ARGUMENT.Changes
gemini.partdecodesthoughtSignatureand emits it withomitempty, so a request with no signatures is unchanged on the wire.llm.ToolCallgets an optionalThoughtSignature(json:"thought_signature,omitempty").toLLMToolCallsets it, andtoAssistantMessagewrites it back on the samefunctionCallpart. Each signature stays on the call it arrived with, so in a parallel batch where only the first call is signed, the others stay unsigned.llm.ToolCallverbatim (apiMessage.ToolCalls), sotoAPIMessagenow drops the field before sending. Without that, a session that switches from Gemini to an OpenAI-compatible model would send an unknown field on every replayed call. The session's own copy keeps the signature.I put it on
llm.ToolCallrather than inProviderStatebecause compaction clearsProviderStateon retained messages (agent/session.go), and these signatures have to survive that to keep the retained calls valid.Tests
TestProcessStreamKeepsThoughtSignaturePerCallTestBuildRequestReplaysThoughtSignatureTestBuildRequestWithoutThoughtSignatureOmitsFieldthoughtSignaturekey when there is noneTestThoughtSignatureSurvivesSessionRoundTripTestBuildRequestDropsThoughtSignature(openai)To check the tests catch the bug, I removed each part of the fix in turn and reran them:
toLLMToolCall...PerCall,...SessionRoundTriptoAssistantMessage...ReplaysThoughtSignature,...SessionRoundTripllm.ToolCall(set to-)...SessionRoundTripwireToolCallsin the OpenAI adapterTestBuildRequestDropsThoughtSignaturemake check(fmt-check, golangci-lint v2.13.0, deadcode, generate-check) andmake testpass.Not in this PR
extra_content.google.thought_signature. Happy to follow up if that route is used.