Skip to content

Gemini 3 and 4: sign handed-back tool calls in every engine that runs Gemini - #4

Closed
lorenzejay wants to merge 3 commits into
mainfrom
fix/gemini-thought-signatures
Closed

lorenzejay wants to merge 3 commits into
mainfrom
fix/gemini-thought-signatures

Conversation

@lorenzejay

Copy link
Copy Markdown
Owner

Fixes CopilotKit#2363.

Why

Gemini 3 and later refuse a function call in the current turn that does not carry the thought
signature the model gave it:

400 Function call is missing a thought_signature in functionCall parts

OpenBot runs each tool between two runs, and an AG-UI tool call has no field for a signature, so
every engine handed the call back unsigned. Every Bot on Gemini 3.x or Gemini 4 failed as soon as a
tool's result came back. That happened with a Google AI Studio key saved in Settings or Desktop
(Google's OpenAI-compatible endpoint) and with BOT_PROVIDER=google.

Checked against the live API (2026-11-27) on gemini-3.8-flash and gemini-4-hydrogen:

request native generateContent OpenAI-compatible
handed-back call, no signature 400 missing thought_signature 400 missing thought_signature
with skip_thought_signature_validator (raw or base64, one or two parallel calls) OK OK

This PR stands alone. It does not depend on the default-model PR (#2).

What changes

Signing, once per language, at the HTTP request

  • shared/gemini_signatures.py is installed by every Python engine (ADK, AG2, CrewAI, LangChain,
    Langroid, LlamaIndex, Strands), beside run_model_key. It wraps httpx/httpx2 sends and
    aiohttp requests (google-genai sends through aiohttp when that is installed). It gives each
    unsigned call Google's documented placeholder in two places:

    • Gemini's own API: a functionCall part, on a POST to .../models/gemini-*:(stream)generateContent, on any host.
    • Google's OpenAI-compatible endpoint, or OpenBot's gateway asked for a Gemini model:
      extra_content.google.thought_signature on the tool call.

    A real signature is kept. A request to any other provider, or for any other model, is sent byte
    for byte.

  • shared/gemini-signatures.ts has the same rules as a fetch wrapper. LangGraph (TypeScript) and
    Mastra pass it to their OpenAI-compatible clients. On its native path, LangGraph (TypeScript)
    adds the placeholder in forGemini when the thread brought back no signature. The real one still
    travels as encryptedValue when the server keeps it.

BOT_PROVIDER=google reaches Gemini at all

  • ADK, Strands, Langroid, CrewAI and LlamaIndex built google/<model>, but LiteLLM reads
    gemini/, so they failed with "LLM Provider NOT provided". Fixed through litellm_model in
    shared/model_providers.py.
  • AG2 fell through to its OpenAI config and asked for OPENAI_API_KEY. It now uses AG2's
    GeminiConfig with the Google key. This adds the ag2[gemini] extra, with google-genai held
    <3 like the other engines. AG2's Gemini config also round-trips the real signature as
    REASONING_ENCRYPTED_VALUE.

CI catches it

  • scripts/model-compat.ts: the one run per model is now a hand-back (handBackRun): the user's
    ask, the assistant's unsigned call to look_up_order, and its result. A model a Bot cannot finish
    a tool call on no longer passes, and so no longer gets into the lock. model-lock.json is
    untouched; CI writes it.

Not changed

  • Antigravity hands the thread to its Go harness as labelled text, so it never sends a handed-back
    call as a function call. It passed the live hand-back unchanged.
  • Mastra answers on OpenAI and Anthropic only, and refuses BOT_PROVIDER=google at startup by
    design. Its Gemini path is Google's compatible endpoint, which is now signed.
  • LlamaIndex's AG-UI router replays a handed-back call as text today. The signer is installed so
    real calls are covered if the router changes.
  • Agno, Microsoft and Pydantic AI refuse every Gemini model. They are out of scope.

Tests

  • One test_gemini_handback.py per Python engine. Each runs the real AG-UI endpoint against the
    shared fake provider, which now also serves Gemini's generateContent. It checks two things:

    • On BOT_PROVIDER=google and on a compatible address with a Gemini model, the handed-back call
      reaches the model signed.
    • On another model, the call is sent as it was.

    With the signer switched off, the compatible-path case fails in every engine.

  • agent-adk/tests/test_gemini_signatures.py and shared/gemini-signatures.test.ts cover the
    signing rules: hosts, model names, keeping a real signature, leaving other providers alone, and
    the wrapped clients.

  • LangGraph (TS) hand-back.test.ts and Mastra index.test.ts add hand-back cases. They fail
    without the fix.

  • scripts/model-compat.test.ts covers the run body.

Live hand-back runs (2026-11-27, paid-tier key, images built from this branch's head)

Each run hands back an unsigned look_up_order call and its result, and passes when the engine
answers "Order 42 has shipped" or similar. "Compatible" is Google's OpenAI-compatible endpoint,
which is how Desktop and Settings save a Gemini key. "Native" is BOT_PROVIDER=google.

engine 4-hydrogen compatible 4-hydrogen native 3.8-flash compatible 3.8-flash native
ADK pass pass pass pass
AG2 pass pass pass pass
CrewAI pass pass pass pass
LangChain (langgraph-agui) pass pass pass pass
LangGraph (TypeScript) pass pass pass pass
Langroid pass pass pass pass
LlamaIndex pass pass pass pass
Mastra pass refused by design pass refused by design
Strands pass pass pass pass
Antigravity (Gemini's own API only) pass pass

Mastra loads no Google module and refuses BOT_PROVIDER=google at startup, as before. Its Gemini
path is the compatible endpoint.

Before this change, the published latest images failed every compatible-path cell with "Function
call is missing a thought_signature". On the native path, ADK, Strands, Langroid and CrewAI failed
with "LLM Provider NOT provided", and AG2 asked for an OpenAI key.

Checks run locally:

  • every changed Python engine's full pytest suite;
  • LangGraph (TS) npm test and typecheck;
  • Mastra's bun test;
  • shared/ and scripts/ bun tests;
  • tests/compose.test.ts;
  • biome lint and format on the changed files.

…s fix did not reach

Gemini 3 and later refuse a function call in the current turn without the thought signature the
model gave it ("Function call is missing a thought_signature in functionCall parts"). OpenBot runs
each tool between two runs and an AG-UI tool call has no field for a signature. 1.2.2 carries the
real signature through the thread on both LangGraph engines (CopilotKit#2334, CopilotKit#2335); every other engine
still sent the handed-back call unsigned, so a Bot on Gemini 3.x or 4 failed after its first tool.

- shared/gemini_signatures.py: ADK, AG2, CrewAI, Langroid, LlamaIndex and Strands install it beside
  run_model_key. It wraps httpx/httpx2 sends and aiohttp requests and gives each unsigned call
  Google's documented placeholder, `skip_thought_signature_validator`: on Gemini's own API
  (`functionCall` parts, any host) and on Google's OpenAI-compatible endpoint or OpenBot's gateway
  for a Gemini model (`extra_content.google.thought_signature`). A real signature is kept; any other
  provider's or model's request is sent byte for byte.
- shared/gemini-signatures.ts: the same rules as a fetch wrapper, for Mastra's OpenAI-compatible
  client.
- LiteLLM engines (ADK, Strands, Langroid, CrewAI, LlamaIndex) address Gemini as `gemini/`, not
  `google/` ("LLM Provider NOT provided"), through `litellm_model` in model_providers.py.
- AG2 answers `BOT_PROVIDER=google` with its GeminiConfig on the Google key, instead of the OpenAI
  config that asked for OPENAI_API_KEY.
- scripts/model-compat.ts: the one run per model now hands back an unsigned tool call and its
  result (`handBackRun`), so the lock no longer verifies a model a Bot cannot finish a tool call on.
- Tests: a hand-back test per engine against the shared fake provider (now also serving Gemini's
  generateContent), signing-rule tests for both modules, model-compat's run body.
tests/compose.test.ts imports the LangChain harness with httpx stubbed as a bare module; the
signer, installed at import, assumed httpx.Client and stopped the import. It now patches only a
client that is there.
…cord gets the placeholder

thought_signatures.py (CopilotKit#2335) sends back the signature Gemini gave a call, when the thread carries
it. A thread without one (a conversation from before it, or a caller that drops encryptedValue)
still reached Google's OpenAI-compatible endpoint unsigned and was refused with "Function call is
missing a thought_signature". The engine now installs shared/gemini_signatures.py like the other
Python engines: it fills in Google's placeholder only where a call has no signature, so a real one
still goes as it came, and nothing changes for another provider's model. The LangGraph (TypeScript)
engine already does this in thought-signatures.ts.
@lorenzejay
lorenzejay force-pushed the fix/gemini-thought-signatures branch from 4d83608 to ba68f20 Compare November 28, 2026 03:27
@lorenzejay

Copy link
Copy Markdown
Owner Author

Opened upstream as CopilotKit#2365, from this same branch. The branch is now rebased on upstream 1.2.2: it leaves both LangGraph engines to CopilotKit#2334/CopilotKit#2335 and adds the placeholder on LangGraph (Python) only for a call with no signature on record.

@lorenzejay lorenzejay closed this Nov 28, 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.

Gemini 3 and 4 Bots fail after their first tool call: "Function call is missing a thought_signature"

1 participant