gemini-cli - 💡(How to fix) Fix Crash (400 Bad Request) due to missing thought_signature when model executes multiple tool calls in a single turn [2 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
google-gemini/gemini-cli#26558Fetched 2026-05-06 06:35:30
View on GitHub
Comments
2
Participants
2
Timeline
3
Reactions
0
Author
Timeline (top)
commented ×2labeled ×1

Error Message

When using gemini-cli with the gemini-3.1-pro-preview model, the CLI crashes with a 400 Bad Request API Error. The error log shows:

Root Cause

We debugged the minified bundle (specifically chunk-NET4RIEQ.js) and found the root cause in the ensureActiveLoopHasThoughtSignatures function. There are three bugs in the loop that injects the synthetic signatures:

  1. Incorrect Nesting: The synthetic signature is injected at the root of the part object, but the API requires it to be nested inside the part.functionCall object.
  2. Premature Break: The loop contains a break; statement that stops iterating through the parts array as soon as it finds the first functionCall.
  3. Missing Tool Types: The function completely ignores executableCode blocks, which also require this signature in Gemini 3.1.

Fix Action

Fix / Workaround

(Note: This update was automatically analyzed and submitted by Opencode using the Gemini 3.1 Pro Preview model, which successfully diagnosed its own crash loop and hot-patched the minified bundle to restore functionality.)

Code Example

ensureActiveLoopHasThoughtSignatures(requestContents) {
  const newContents = requestContents.slice();
  for (let i = 0; i < newContents.length; i++) {
    const content = newContents[i];
    if (content.role === "model" && content.parts) {
      const newParts = content.parts.slice();
      let changed = false;
      for (let j = 0; j < newParts.length; j++) {
        const part = newParts[j];
        if (part.functionCall) {
          newParts[j] = {
            ...part,
            thoughtSignature: SYNTHETIC_THOUGHT_SIGNATURE,
            thought_signature: SYNTHETIC_THOUGHT_SIGNATURE,
            functionCall: {
              ...part.functionCall,
              thoughtSignature: SYNTHETIC_THOUGHT_SIGNATURE,
              thought_signature: SYNTHETIC_THOUGHT_SIGNATURE
            }
          };
          changed = true;
        }
        if (part.executableCode) {
          newParts[j] = {
            ...newParts[j],
            thoughtSignature: SYNTHETIC_THOUGHT_SIGNATURE,
            thought_signature: SYNTHETIC_THOUGHT_SIGNATURE,
            executableCode: {
              ...part.executableCode,
              thoughtSignature: SYNTHETIC_THOUGHT_SIGNATURE,
              thought_signature: SYNTHETIC_THOUGHT_SIGNATURE
            }
          };
          changed = true;
        }
      }
      if (changed) {
        newContents[i] = { ...content, parts: newParts };
      }
    }
  }
  return newContents;
}

stripThoughtsFromHistory() {
  this.agentHistory.map((content) => {
    const newContent = { ...content };
    if (newContent.parts) {
      newContent.parts = newContent.parts.map((part) => {
        if (part && typeof part === "object") {
          const newPart = { ...part };
          delete newPart.thoughtSignature;
          delete newPart.thought_signature;
          if (newPart.functionCall) {
            newPart.functionCall = { ...newPart.functionCall };
            delete newPart.functionCall.thoughtSignature;
            delete newPart.functionCall.thought_signature;
          }
          if (newPart.executableCode) {
            newPart.executableCode = { ...newPart.executableCode };
            delete newPart.executableCode.thoughtSignature;
            delete newPart.executableCode.thought_signature;
          }
          return newPart;
        }
        return part;
      });
    }
    return newContent;
  });
}
RAW_BUFFERClick to expand / collapse

What happened?

When using gemini-cli with the gemini-3.1-pro-preview model, the CLI crashes with a 400 Bad Request API Error.

The error log shows: "Function call is missing a thought_signature in functionCall parts." (or in executableCode parts)

This consistently happens when the model emits multiple tools in the same response (e.g., an update_topic tool call followed by a run_shell_command). The first tool call succeeds, but the subsequent ones cause the API request to fail.

What did you expect to happen?

The CLI should successfully process all function calls without crashing, correctly attaching the synthetic thought_signature to all parallel tool calls and executable code blocks in the turn.

Client information

CLI Version: 0.41.1 OS: linux Auth Method: gemini-api-key

Anything else we need to know?

We debugged the minified bundle (specifically chunk-NET4RIEQ.js) and found the root cause in the ensureActiveLoopHasThoughtSignatures function. There are three bugs in the loop that injects the synthetic signatures:

  1. Incorrect Nesting: The synthetic signature is injected at the root of the part object, but the API requires it to be nested inside the part.functionCall object.
  2. Premature Break: The loop contains a break; statement that stops iterating through the parts array as soon as it finds the first functionCall.
  3. Missing Tool Types: The function completely ignores executableCode blocks, which also require this signature in Gemini 3.1.

Note: Aggressive object cloning via JSON.parse(JSON.stringify()) must be avoided, as it strips prototypes and breaks the SDK's turn tracking, causing "Please ensure that function call turn comes immediately after a user turn" errors. Shallow copies must be used.

Proposed Fix: Replace ensureActiveLoopHasThoughtSignatures and stripThoughtsFromHistory with the following implementation using shallow copies:

ensureActiveLoopHasThoughtSignatures(requestContents) {
  const newContents = requestContents.slice();
  for (let i = 0; i < newContents.length; i++) {
    const content = newContents[i];
    if (content.role === "model" && content.parts) {
      const newParts = content.parts.slice();
      let changed = false;
      for (let j = 0; j < newParts.length; j++) {
        const part = newParts[j];
        if (part.functionCall) {
          newParts[j] = {
            ...part,
            thoughtSignature: SYNTHETIC_THOUGHT_SIGNATURE,
            thought_signature: SYNTHETIC_THOUGHT_SIGNATURE,
            functionCall: {
              ...part.functionCall,
              thoughtSignature: SYNTHETIC_THOUGHT_SIGNATURE,
              thought_signature: SYNTHETIC_THOUGHT_SIGNATURE
            }
          };
          changed = true;
        }
        if (part.executableCode) {
          newParts[j] = {
            ...newParts[j],
            thoughtSignature: SYNTHETIC_THOUGHT_SIGNATURE,
            thought_signature: SYNTHETIC_THOUGHT_SIGNATURE,
            executableCode: {
              ...part.executableCode,
              thoughtSignature: SYNTHETIC_THOUGHT_SIGNATURE,
              thought_signature: SYNTHETIC_THOUGHT_SIGNATURE
            }
          };
          changed = true;
        }
      }
      if (changed) {
        newContents[i] = { ...content, parts: newParts };
      }
    }
  }
  return newContents;
}

stripThoughtsFromHistory() {
  this.agentHistory.map((content) => {
    const newContent = { ...content };
    if (newContent.parts) {
      newContent.parts = newContent.parts.map((part) => {
        if (part && typeof part === "object") {
          const newPart = { ...part };
          delete newPart.thoughtSignature;
          delete newPart.thought_signature;
          if (newPart.functionCall) {
            newPart.functionCall = { ...newPart.functionCall };
            delete newPart.functionCall.thoughtSignature;
            delete newPart.functionCall.thought_signature;
          }
          if (newPart.executableCode) {
            newPart.executableCode = { ...newPart.executableCode };
            delete newPart.executableCode.thoughtSignature;
            delete newPart.executableCode.thought_signature;
          }
          return newPart;
        }
        return part;
      });
    }
    return newContent;
  });
}

(Note: This update was automatically analyzed and submitted by Opencode using the Gemini 3.1 Pro Preview model, which successfully diagnosed its own crash loop and hot-patched the minified bundle to restore functionality.)

extent analysis

TL;DR

Replace the ensureActiveLoopHasThoughtSignatures and stripThoughtsFromHistory functions with the proposed implementation using shallow copies to fix the synthetic signature injection issue.

Guidance

  • Identify the ensureActiveLoopHasThoughtSignatures function in the chunk-NET4RIEQ.js file and replace it with the proposed implementation.
  • Replace the stripThoughtsFromHistory function with the provided implementation to correctly remove thought signatures from the agent history.
  • Verify that the updated functions correctly inject synthetic signatures into functionCall and executableCode parts, and remove them from the agent history.
  • Test the updated code with multiple tool calls in the same response to ensure the API request no longer fails.

Example

The proposed implementation for ensureActiveLoopHasThoughtSignatures and stripThoughtsFromHistory is provided in the issue body and can be used as a reference for the update.

Notes

The update must use shallow copies to avoid stripping prototypes and breaking the SDK's turn tracking. Aggressive object cloning via JSON.parse(JSON.stringify()) should be avoided.

Recommendation

Apply the proposed workaround by replacing the ensureActiveLoopHasThoughtSignatures and stripThoughtsFromHistory functions with the provided implementation. This should fix the synthetic signature injection issue and prevent the API request from failing.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

gemini-cli - 💡(How to fix) Fix Crash (400 Bad Request) due to missing thought_signature when model executes multiple tool calls in a single turn [2 comments, 2 participants]