[stripe] gemini 3 thought signatures in tool call id

Merged
CO
1y ago
+374-35

Gemini 3 thought signature in tool call id

Summary

Problem
Gemini 3 returns thought signatures in tool calls in its responses and expects the client to send back the thought signature in subsequent requests. If it is not provided gemini will return 400 error. Many chat completions clients do not do this OOTB (ex langchain's ChatOpenAI)

see Gemini docs

Solution:
Store gemini thought signatures in tool call id. This way chat completions clients will always pass back tool call id in chat history. Litellm extracts the thought signature from the tool call id and sends it to Gemini

NOTE:
This only works for chat completions not responses api

Relevant issues

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have Added testing in the tests/litellm/ directory, Adding at least 1 test is a hard requirement - see details
  • I have added a screenshot of my new test passing locally
  • My PR passes all unit tests on make test-unit
  • My PR's scope is as isolated as possible, it only solves 1 specific problem

Tests

run with
poetry run pytest tests/test_litellm/llms/vertex_ai/gemini/test_thought_signature_in_tool_call_id.py -v

Screenshot 2025-11-20 at 6 29 33 PM

Test with cURL

curl --verbose -X POST http://0.0.0.0:4000/chat/completions \
  -H 'Authorization: Bearer sk-123' \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "gemini-3-pro",
    "messages": [
      {
        "role": "user",
        "content": "What is 2 + 2?"
      }
    ],
    "tools": [
      {
        "type": "function",
        "function": {
          "name": "calculator",
          "description": "A basic calculator that can add, subtract, multiply, or divide two numbers",
          "parameters": {
            "type": "object",
            "properties": {
              "operation": {
                "type": "string",
                "enum": ["add", "subtract", "multiply", "divide"],
                "description": "The mathematical operation to perform"
              },
              "num1": {
                "type": "number",
                "description": "The first number"
              },
              "num2": {
                "type": "number",
                "description": "The second number"
              }
            },
            "required": ["operation", "num1", "num2"]
          }
        }
      }
    ]
}' 

Response

notice the tool call id

"id": "call_00d09e4f03654b668abe3e361f1e__thought__....

{
  "id": "gpUfacGnCPiZ4_UP3qjZkQs",
  "created": 1763677569,
  "model": "gemini-3-pro",
  "object": "chat.completion",
  "choices": [
    {
      "finish_reason": "tool_calls",
      "index": 0,
      "message": {
        "role": "assistant",
        "tool_calls": [
          {
            "index": 0,
            "provider_specific_fields": {
              "thought_signature": "CsoDAePx/140cmKGChx3tcqSw7eWw+UgW8B++4PUSrV0C90632IBstKp64dqjYsj5s3DXMgIJ+vARgiiiZFhdZsV+aE2xpA4Y2OM6ejdtMmgHx2Qx4KSUGIr+7rlizh+jXyOuGEPkCRtKXbkyrDZ/MVRWzyEV39lMLzBEAMbzPyG2/wpboPf9j41vboJ1mqVUd4ZOA1srhufHpazEQuPAqYW3eqIGQsJi+J/CDfY8mA6tLTT+0z6yt6UDQzr0dHNgSk3x3VwW65RieDPDx3ou/6Nd+Z6MYPB8hYHxhMM87e7HppRI4Zg6mc79ow0Fb1UMKfmTuR2L9d7Ipk4cUJUIx8TQ0QWaqP6eOE+4OAYbuakUQKeNe1pTx757hPvmD/kFrQjaWxmn202IFAgKiPtXGubQO7Mgy9DFj5rlSG5e9UrGHCIi5dqxpmrjPmH02eOWBu/9GkXMyX6wl03Yqf5tBycWJKAlkTWJNF472HvqK6MFoaUTo2k9lzFAPviCzA6XgetnUcxe3fLA2xOBcS/HUjMK4vmIACEJSezoeJG0TZpDZpbiSw8vIGGKsAixJSL5MjiSv6XLOoEpE30/w+GB42SmM6DSK0TYIy3zi8="
            },
            "function": {
              "arguments": "{\"num1\": 2, \"operation\": \"add\", \"num2\": 2}",
              "name": "calculator"
            },
            "id": "call_00d09e4f03654b668abe3e361f1e__thought__CsoDAePx/140cmKGChx3tcqSw7eWw+UgW8B++4PUSrV0C90632IBstKp64dqjYsj5s3DXMgIJ+vARgiiiZFhdZsV+aE2xpA4Y2OM6ejdtMmgHx2Qx4KSUGIr+7rlizh+jXyOuGEPkCRtKXbkyrDZ/MVRWzyEV39lMLzBEAMbzPyG2/wpboPf9j41vboJ1mqVUd4ZOA1srhufHpazEQuPAqYW3eqIGQsJi+J/CDfY8mA6tLTT+0z6yt6UDQzr0dHNgSk3x3VwW65RieDPDx3ou/6Nd+Z6MYPB8hYHxhMM87e7HppRI4Zg6mc79ow0Fb1UMKfmTuR2L9d7Ipk4cUJUIx8TQ0QWaqP6eOE+4OAYbuakUQKeNe1pTx757hPvmD/kFrQjaWxmn202IFAgKiPtXGubQO7Mgy9DFj5rlSG5e9UrGHCIi5dqxpmrjPmH02eOWBu/9GkXMyX6wl03Yqf5tBycWJKAlkTWJNF472HvqK6MFoaUTo2k9lzFAPviCzA6XgetnUcxe3fLA2xOBcS/HUjMK4vmIACEJSezoeJG0TZpDZpbiSw8vIGGKsAixJSL5MjiSv6XLOoEpE30/w+GB42SmM6DSK0TYIy3zi8=",
            "type": "function"
          }
        ],
        "images": [],
        "thinking_blocks": [
          {
            "type": "thinking",
            "thinking": "{\"functionCall\": {\"name\": \"calculator\", \"args\": {\"num1\": 2, \"operation\": \"add\", \"num2\": 2}}}",
            "signature": "CsoDAePx/140cmKGChx3tcqSw7eWw+UgW8B++4PUSrV0C90632IBstKp64dqjYsj5s3DXMgIJ+vARgiiiZFhdZsV+aE2xpA4Y2OM6ejdtMmgHx2Qx4KSUGIr+7rlizh+jXyOuGEPkCRtKXbkyrDZ/MVRWzyEV39lMLzBEAMbzPyG2/wpboPf9j41vboJ1mqVUd4ZOA1srhufHpazEQuPAqYW3eqIGQsJi+J/CDfY8mA6tLTT+0z6yt6UDQzr0dHNgSk3x3VwW65RieDPDx3ou/6Nd+Z6MYPB8hYHxhMM87e7HppRI4Zg6mc79ow0Fb1UMKfmTuR2L9d7Ipk4cUJUIx8TQ0QWaqP6eOE+4OAYbuakUQKeNe1pTx757hPvmD/kFrQjaWxmn202IFAgKiPtXGubQO7Mgy9DFj5rlSG5e9UrGHCIi5dqxpmrjPmH02eOWBu/9GkXMyX6wl03Yqf5tBycWJKAlkTWJNF472HvqK6MFoaUTo2k9lzFAPviCzA6XgetnUcxe3fLA2xOBcS/HUjMK4vmIACEJSezoeJG0TZpDZpbiSw8vIGGKsAixJSL5MjiSv6XLOoEpE30/w+GB42SmM6DSK0TYIy3zi8="
          }
        ]
      }
    }
  ],
  "usage": {
    "completion_tokens": 121,
    "prompt_tokens": 53,
    "total_tokens": 174,
    "completion_tokens_details": {
      "reasoning_tokens": 108,
      "text_tokens": 13
    },
    "prompt_tokens_details": {
      "text_tokens": 53
    }
  },
  "vertex_ai_grounding_metadata": [],
  "vertex_ai_url_context_metadata": [],
  "vertex_ai_safety_results": [],
  "vertex_ai_citation_metadata": []
}

Type

🆕 New Feature
📖 Documentation
✅ Test

Changes