Skip to content

Keep Gemini thought signatures that sit on functionCall parts - #315

Merged
fylorn merged 1 commit into
mainfrom
feat/gemini-call-signature
Oct 10, 2026
Merged

fylorn merged 1 commit into
mainfrom
feat/gemini-call-signature

Conversation

@fylorn

@fylorn fylorn commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

What

Gemini with thinking enabled puts a turn's thought signature on the first functionCall part, not on the thought text. The Gemini decoders only read thoughtSignature from thought: true parts, so a signature on a call was dropped in three places (the client's request history, whole upstream responses, upstream streams), and on the way back to Gemini the call always got the skip_thought_signature_validator placeholder.

  • Decode (gemini/request.rs, gemini/response.rs, gemini/stream.rs): a thoughtSignature on a functionCall part becomes a Google-signed Thinking block with empty text placed immediately before the ToolCall it belongs to. No new IR field: other clients already carry such a block with the tw1.g. scheme and hand it back on the next turn. gemini::request::call_signature documents the shape. The placeholder value is not read back as a signature. Adjacent thought parts without a signature are merged into the signed one (streaming writes thought text one part per delta), so the signature covers the whole text.
  • Encode for a Gemini upstream (gemini/request.rs): the shape above is written back on the functionCall part; the placeholder is used only when the call has no real signature.
  • Gemini client output (gemini/response.rs, gemini/stream.rs): a call's signature is emitted on the functionCall part; a thought part's text and signature are emitted in the same part (the stream writer holds back one text delta per thinking block, so thoughts still stream with a one-delta lag). A signature block not followed by a call still gets its own part, including at the end of the stream.
  • Passthrough (convert::strip_carried): a carried signature on a functionCall part is replaced with the placeholder instead of the part being removed.

Chat clients have no field to carry a signature, so Chat → Gemini → Chat still falls back to the placeholder (unchanged).

Tests

  • gemini/request.rs: decode keeps the call signature, ignores the placeholder, merges split thoughts; encode puts it back on the call and nowhere else.
  • gemini/response.rs, gemini/stream.rs: decode keeps it (the existing fixtures already carried it); the stream writer puts text and signature in one part and the call's signature on the call.
  • convert.rs: passthrough replaces a carried call signature with the placeholder.
  • tests/matrix.rs: Gemini upstream behind Anthropic and Responses clients, whole and streamed: the signature reaches the client as a Google-signed block before the call, and the client's next turn puts it back on the functionCall part.

Verified locally: cargo fmt --all -- --check, cargo clippy --workspace --all-targets -- -D warnings, cargo test -p tw-dialect (220 + integration tests, all green), cargo test -p tw-gateway.

New messages

None. No protocol change.

🤖 Generated with Claude Code

…l parts

Gemini with thinking enabled puts a turn's thought signature on the
first functionCall part rather than on the thought text. The Gemini
decoders only read `thoughtSignature` from `thought: true` parts, so a
signature on a call was lost in the client's request history, in whole
upstream responses and in upstream streams; on the way back the
functionCall always got the `skip_thought_signature_validator`
placeholder, and the conversation lost its reasoning continuity.

The IR gets no new field: a signature on a functionCall part is
represented as a Google-signed Thinking block with empty text placed
immediately before the ToolCall it belongs to (`gemini::request::
call_signature` documents the shape). Other clients already carry such a
block with the `tw1.g.` scheme and hand it back on the next turn, and
the Gemini encoder recognises the shape and writes the signature on the
functionCall part again, using the placeholder only when the call has
no real signature. The placeholder itself is not read back as a
signature, and adjacent thought parts without a signature are merged
into the signed one so the signature covers the whole text.

When the client is Gemini and the gateway streams a reply, a thought
part's text and signature are emitted in the same part (the writer
holds back one text delta per thinking block), and a function call's
signature is emitted on the functionCall part. On same-format
passthrough a carried signature on a functionCall part is replaced by
the placeholder rather than removing the part.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@fylorn
fylorn merged commit 11e63ab into main Oct 10, 2026
5 checks passed
@fylorn
fylorn deleted the feat/gemini-call-signature branch October 10, 2026 10:52
@fylorn fylorn mentioned this pull request Oct 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant