Skip to content

fix(llm): stabilize Gemini 3/2.5 tool calls and thought signature handling - fixes #210#230

Merged
everettjf merged 3 commits intomicroclaw:mainfrom
Erudition:main
Mar 10, 2026
Merged

fix(llm): stabilize Gemini 3/2.5 tool calls and thought signature handling - fixes #210#230
everettjf merged 3 commits intomicroclaw:mainfrom
Erudition:main

Conversation

@Erudition
Copy link
Contributor

This PR addresses several critical issues with the Google OpenAI-compatible Tier, specifically
targeting Gemini 3 models which require strict "thought signature" handling and exhibit
non-standard termination behaviors.

Key Changes:

  1. Forced tool_use Stop Reason:

    • Gemini 3 models frequently return finish_reason: "stop" even when tool calls are
      present in the response.
    • Updated build_stream_response and the OpenAI stream loop to force stop_reason:
      "tool_use" whenever the tool call map is non-empty. This ensures the agent_engine
      correctly triggers the execution turn.
  2. Corrected Thought Signature Handling:

    • Parsing: Updated the response parser to capture thought_signature from
      extra_content.google (where Google places it) rather than incorrectly looking inside
      the function object.
    • Propagation: Updated translate_messages_to_oai_with_reasoning to send the signature
      back in the subsequent turn using the exact nested structure Google requires:
      tool_calls[].extra_content.google.thought_signature. Failing to do this results in 400
      Bad Request errors on Turn 2.
  3. Improved Stream Chunk Indexing:

    • Google’s API sometimes delivers tool call deltas without an index field.
    • Updated process_openai_stream_event to handle missing indexes by matching on the tool
      id or assigning a new unique index. This prevents tool call chunks from overwriting
      each other and ensures all signatures are captured.
  4. Serialization Stability:

    • Updated OaiToolCall and related structs to properly deserialize extra_content for
      non-streaming responses.
    • Ensured thought_signature is correctly propagated through ResponseContentBlock and
      ContentBlock types.

Verified using an automated test suite across multiple scenarios that did not work with #211

  • Non-thinking queries: Basic chat works correctly.
  • Thinking queries: Reasoning content is successfully captured and wrapped in tags.
  • Multi-turn Tool Use: Verified Turn 1 (Thought + Tool Call) followed by Turn 2 (Tool Result
    • Final Answer) works without 400 errors or premature stops.

@everettjf
Copy link
Contributor

Thank you

@everettjf everettjf merged commit c74a2b6 into microclaw:main Mar 10, 2026
4 checks passed
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.

2 participants