Skip to content

fix(core): prevent thoughtSignature-only turns and preserve multimodal tool responses - #30153

Merged
DavidAPierce merged 2 commits into
google-gemini:mainfrom
tadipavan:b/30074-readFile_Image
Nov 5, 2026
Merged

DavidAPierce merged 2 commits into
google-gemini:mainfrom
tadipavan:b/30074-readFile_Image

Conversation

@tadipavan

Copy link
Copy Markdown
Contributor

Summary

Fixes 400 Bad Request ("Requests ending with a model turn are not supported") and empty-response retry loops when using read_file on images (or receiving thoughtSignature-only model responses after tool execution).
When Gemini 2.5 / 3 models return a post-tool response containing only thoughtSignature (or empty text alongside thoughtSignature), the history validation and sanitization pipeline previously treated thoughtSignature as an active content key. This allowed empty/signature-only model turns into agentHistory, bypassed closeUnansweredToolResponseTurn, and caused stripToolCallIdPrefixes to drop the model turn after ensureTerminalUserTurn had already run—leaving the outgoing API payload ending on a model turn or triggering an invalid-stream retry loop. Additionally, multimodal tool response parts (such as sibling inlineData image parts) were dropped in subagent execution (LocalAgentExecutor) and when dynamic model configuration lacked an explicit multimodalToolUse feature flag.

Details

  1. Exclude thoughtSignature from active content-key checks (packages/core/src/core/geminiChat.ts):
    • Updated isValidContent, isEmptyTextPart, and stripToolCallIdPrefixes to ignore 'thoughtSignature' when checking whether a Part carries active payload keys (functionCall, functionResponse, inlineData, fileData, etc.).
    • Updated closeUnansweredToolResponseTurn to check !hasNonEmptyParts(targetTurn.content) and use immutable this.agentHistory.set(...) when inserting INTERRUPTED_RESPONSE_PLACEHOLDER.
    • Reordered makeApiCallAndProcessStream so stripToolCallIdPrefixes executes before ensureTerminalUserTurn, and preserved model turns that immediately follow a user functionResponse turn by falling back to INTERRUPTED_RESPONSE_PLACEHOLDER instead of dropping the turn.
  2. Preserve multimodal tool support in dynamic model configs (packages/core/src/config/models.ts):
    • Updated supportsMultimodalFunctionResponse to pass config?.getHasAccessToPreviewModel?.() ?? true to resolveModel and fall back to /^gemini-3(\.|-|$)/.test(resolved) when definition?.features?.multimodalToolUse is undefined.
  3. Preserve sibling multimodal Part[] in LocalAgentExecutor (packages/core/src/agents/local-executor.ts):
    • Changed syncResults in processFunctionCalls from Map<string, Part> to Map<string, Part[]> so all call.response.responseParts (including sibling inlineData image parts when supportsMultimodalFunctionResponse is false) are forwarded instead of only responseParts[0].

Related Issues

Fixes #30074

How to Validate

  1. Run the targeted unit test suites covering GeminiChat, model feature resolution, and LocalAgentExecutor:
    npm test -w @google/gemini-cli-core -- \
      src/core/geminiChat.test.ts \
      src/config/models.test.ts \
      src/agents/local-executor.test.ts
  2. Run the full read_file and history hardening test suites:
    npm test -w @google/gemini-cli-core -- \
      src/tools/read-file.test.ts \
      src/utils/generateContentResponseUtilities.test.ts \
      src/utils/historyHardening.test.ts

Pre-Merge Checklist

  • Updated relevant documentation and README (if needed)
  • Added/updated tests (if needed)
  • Noted breaking changes (if any)
  • Validated on required platforms/methods:
    • MacOS
      • npm run
      • npx
      • Docker
      • Podman
      • Seatbelt
    • Windows
      • npm run
      • npx
      • Docker
    • Linux
      • npm run
      • npx
      • Docker

@tadipavan
tadipavan requested a review from a team as a code owner November 5, 2026 11:03
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses critical issues with Gemini model history management, specifically preventing 400 Bad Request errors caused by invalid model turns containing only 'thoughtSignature'. It improves the robustness of the history sanitization pipeline, ensures proper role alternation, and fixes bugs where multimodal tool responses were being truncated or dropped during subagent execution.

Highlights

  • History Sanitization: Updated the history pipeline to exclude 'thoughtSignature' from active content-key checks, preventing empty or signature-only model turns from corrupting the agent history.
  • API Protocol Compliance: Modified 'stripToolCallIdPrefixes' and 'closeUnansweredToolResponseTurn' to ensure valid terminal user turns and prevent invalid API payloads that cause 400 errors.
  • Multimodal Support: Ensured 'LocalAgentExecutor' preserves all sibling multimodal parts (like inline images) during tool execution and updated dynamic model configuration to correctly resolve multimodal capabilities.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@github-actions github-actions Bot added the size/l A large sized PR label Nov 5, 2026
@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

📊 PR Size: size/L

  • Lines changed: 837
  • Additions: +739
  • Deletions: -98
  • Files changed: 6

@tadipavan

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request improves the handling of multimodal tool responses, thought signatures, and empty trailing model turns within the Gemini API integration. Specifically, LocalAgentExecutor now preserves all response parts (such as sibling inline data for images) when forwarding tool results. In GeminiChat, logic was updated to correctly handle empty trailing model turns containing thought signatures, ensuring they are closed with an interrupted response placeholder to satisfy Gemini API invariants. Additionally, stripToolCallIdPrefixes was refined to preserve thought signatures on function calls while dropping empty text parts, and supportsMultimodalFunctionResponse was updated to respect preview model access configurations. Comprehensive unit tests have been added to validate these changes. I have no further feedback to provide as there are no review comments.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request enhances the local executor to preserve all response parts (including sibling inlineData parts for images) when forwarding tool results, and refactors GeminiChat to strip tool call ID prefixes and empty parts before ensuring a terminal user turn. It also updates the handling of empty model turns containing only a thoughtSignature. Feedback on the changes suggests that in isValidContent, checking if any individual part is empty and immediately returning false can cause a valid model turn to be completely stripped if it contains both valid parts and trailing empty parts; it is recommended to only return false if all parts in the content are empty.

Comment thread packages/core/src/core/geminiChat.ts Resolved
@gemini-cli gemini-cli Bot added the area/core Issues related to User Interface, OS Support, Core Functionality label Nov 5, 2026
@tadipavan

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request enhances the handling of multimodal tool responses and model turns containing thought signatures in the Gemini Chat implementation. Specifically, it updates the local executor to preserve all response parts (such as sibling inline data for images) by transitioning syncResults to store arrays of parts. It also refines the model capability checks for multimodal tool use and updates history normalization in GeminiChat to correctly strip or preserve turns with empty text and thought signatures, preventing API validation errors and ensuring robust role alternation. No review comments were provided, so there is no feedback to evaluate.

Note: Security Review did not run due to the size of the PR.

@DavidAPierce
DavidAPierce added this pull request to the merge queue Nov 5, 2026
Merged via the queue into google-gemini:main with commit 8e372f9 Nov 5, 2026
35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core Issues related to User Interface, OS Support, Core Functionality size/l A large sized PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(core): read_file on images still triggers 400 "Requests ending with a model turn are not supported" / Empty response nudge in v0.66.0

2 participants