Skip to content

translator: accept thinking_blocks signatures and fall back to skip_thought_signature_validator for Gemini function calls#2366

Open
a-nych wants to merge 1 commit into
envoyproxy:mainfrom
a-nych:fix/gemini-thought-signature-fallback
Open

translator: accept thinking_blocks signatures and fall back to skip_thought_signature_validator for Gemini function calls#2366
a-nych wants to merge 1 commit into
envoyproxy:mainfrom
a-nych:fix/gemini-thought-signature-fallback

Conversation

@a-nych

@a-nych a-nych commented Jul 14, 2026

Copy link
Copy Markdown

Description

Gemini 3.x requires the thought signature (see [1]) returned with a functionCall to be echoed back on that part in subsequent requests; otherwise Vertex rejects the request with 400 INVALID_ARGUMENT ("Function call is missing a thought_signature in functionCall parts"). The OpenAI → GCPVertexAI translator emits the signature in responses as message.thinking_blocks[].signature (#1995) but on the request side only reads it from assistant content parts of type "thinking" (#1726), and sends nothing when absent. Standard OpenAI-schema clients have no signature field to echo, so every tool round-trip with Gemini 3.x fails.

This PR changes assistantMsgToGeminiParts to:

  • also accept a signature echoed via the assistant message's thinking_blocks[].signature — the same shape the gateway itself emits, so round-tripping the response message now works. Content-part signatures keep priority.
  • fall back to Google's documented compatibility value skip_thought_signature_validator on the first functionCall part when the client echoed no signature at all (matching LiteLLM's behavior). Attached to the first part only, consistent with how Gemini attaches signatures for parallel calls.

ChatCompletionAssistantMessageParam gains the thinking_blocks field to make the request/response contract symmetric.

Related Issues/PRs (if applicable)

Fixes #2365
Related PRs: #1726, #1995

Special notes for reviewers (if applicable)

  • Four pre-existing fixtures in gemini_helper_test.go / TestOpenAIMessagesToGeminiContents exercised tool calls without a signature and now expect the dummy value — that's the intended new behavior, not weakened assertions.
  • Verified end-to-end with aigw run against real Vertex (gemini-3.5-flash, eu): the signature-less round-trip returns 200 with this patch and 400 on main; thinking_blocks echo returns 200 and carries the real signature; streaming unaffected. Vertex accepts the dummy value (documented compatibility path, so no hard dependency on Google keeping undocumented behavior).

1: https://ai.google.dev/gemini-api/docs/thought-signatures

… Gemini function calls

Gemini 3.x rejects multi-turn tool-calling requests with HTTP 400 when a
function call is missing a thought_signature. The OpenAI->Gemini request
translator only read signatures from assistant content parts of type
"thinking" and had no fallback when a client (correctly, per the OpenAI
schema) doesn't echo one back.

- Add ThinkingBlocks to ChatCompletionAssistantMessageParam so clients that
  round-trip response thinking_blocks (LiteLLM convention) can supply a
  signature on requests too.
- In assistantMsgToGeminiParts, fall back to the first non-empty
  thinking_blocks signature when no content-part signature is present.
- When no signature is available at all, attach Google's documented
  compatibility value ("skip_thought_signature_validator") to the first
  functionCall part only, per
  https://ai.google.dev/gemini-api/docs/thought-signatures.

Signed-off-by: Adam Nych <adam.nych@univio.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@a-nych
a-nych requested a review from a team as a code owner July 14, 2026 10:02
@dosubot dosubot Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Jul 14, 2026
@codecov-commenter

codecov-commenter commented Jul 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.83%. Comparing base (f91bb5b) to head (e023c14).
⚠️ Report is 6 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2366   +/-   ##
=======================================
  Coverage   84.82%   84.83%           
=======================================
  Files         148      148           
  Lines       21838    21849   +11     
=======================================
+ Hits        18524    18535   +11     
  Misses       2194     2194           
  Partials     1120     1120           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@missBerg missBerg added bug Something isn't working area/translation Provider/endpoint coverage and schema translation (incl. fidelity bugs) labels Jul 15, 2026
@sqaisar

sqaisar commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

This is an real issue, and we use gemini3 heavily, and always need workarounds for this. Can someone review this?

// that doesn't round-trip thinking_blocks). Gemini 3.x rejects function calls in
// multi-turn requests that are missing a thought_signature, so fall back to
// Google's documented compatibility escape.
funcCallPart.ThoughtSignature = []byte(dummyThoughtSignature)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this causes allocation. can you declare var whatever = []byte("skip_thought_signature_validator") as a global?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/translation Provider/endpoint coverage and schema translation (incl. fidelity bugs) bug Something isn't working size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenAI→GCPVertexAI: Gemini 3.x rejects tool-call follow-ups (400 missing thought_signature) for standard OpenAI clients

5 participants