Skip to content

fix(core): preserve interrupted placeholder after empty model responses to tool turns - #30228

Merged
DavidAPierce merged 2 commits into
google-gemini:mainfrom
tadipavan:b/29574-400_Bad_Request
Nov 11, 2026
Merged

DavidAPierce merged 2 commits into
google-gemini:mainfrom
tadipavan:b/29574-400_Bad_Request

Conversation

@tadipavan

Copy link
Copy Markdown
Contributor

Summary

Fixes 400 Bad Request ("Requests ending with a model turn are not supported") and silent user-prompt fusion into functionResponse turns when the model responds to a tool output (such as read_file on an image/binary file) with an empty (parts: []), thought-only, or thoughtSignature-only turn ({ text: '', thoughtSignature: '...' }).

Details

When read_file returns image or binary content (either via native multimodal functionResponse.parts on Gemini 3 models or BINARY_INJECTION_KEY sibling [functionResponse, inlineData] turns on Gemini 2.5 models) and the model stream terminates with parts: [], thought-only parts, or a thoughtSignature-only empty text part:

  1. GeminiChat (packages/core/src/core/geminiChat.ts):
    • Added isToolResponseTurnAt to recognize tool response turns both directly and across BINARY_INJECTION_KEY synthetic binary acknowledgments ([user(functionResponse), model(syntheticBinaryAck), user(inlineData)]).
    • Updated addHistory to invoke closeUnansweredToolCallsTurn() and closeUnansweredToolResponseTurn() before appending a non-functionResponse user turn (e.g., IDE context updates), preventing empty/thought-only model turns from being stranded in the middle of history.
    • Updated extractCuratedHistory, stripThoughts, and stripToolCallIdPrefixes to replace empty (parts: []) or thoughtSignature-only model turns following a tool response with INTERRUPTED_RESPONSE_PLACEHOLDER ([The previous response was interrupted before it completed.]) rather than dropping them.
  2. History Hardening (packages/core/src/utils/historyHardening.ts):
    • Updated stripThoughts in hardenHistory, scrubHistory, and scrubContents to handle parts: [] and BINARY_INJECTION_KEY tool response sequences consistently, preserving INTERRUPTED_RESPONSE_PLACEHOLDER so consecutive user turns are not fused into functionResponse turns.
  3. Next Speaker Checker (packages/core/src/utils/nextSpeakerChecker.ts):
    • Updated checkNextSpeaker to immediately return { next_speaker: 'model' } without making an LLM generateJson call when chat.getHistory() ends with INTERRUPTED_RESPONSE_PLACEHOLDER, avoiding sending a contents array ending on a model turn.

Related Issues

Fixes #29574

How to Validate

  1. Run the targeted unit test suites for the modified modules:
    npm test -w @google/gemini-cli-core -- src/core/geminiChat.test.ts src/utils/historyHardening.test.ts src/utils/nextSpeakerChecker.test.ts
  2. Run typecheck and linter:
    npm run typecheck
    npm run lint
  3. Manual / Edge Cases Validated:
    • ReadFileTool reading PNG images on gemini-3-flash-preview (multimodal functionResponse.parts) and gemini-2.5-flash (BINARY_INJECTION_KEY [functionResponse, inlineData]) followed by parts: [], thought-only, or thoughtSignature-only model responses.
    • Follow-up turns via checkNextSpeaker (Please continue.), chat.addHistory (IDE context delta) + sendMessageStream, chat.getHistory(true) / chat.getHistory(false), Context Management apiHistoryOverride, and session resume (convertSessionToClientHistory).

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 11, 2026 08:07
@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 issues where empty or thought-only model responses following tool turns (specifically those involving file reads) caused history corruption and incorrect model behavior. By explicitly marking these stranded model turns with an interrupted response placeholder, the system now maintains proper role alternation and prevents subsequent user turns from being incorrectly fused into tool response contexts.

Highlights

  • Tool Response Handling: Introduced isToolResponseTurnAt to accurately identify tool response turns, including those involving synthetic binary acknowledgments, ensuring correct history management.
  • Interrupted Response Placeholder: Implemented INTERRUPTED_RESPONSE_PLACEHOLDER to replace empty or thought-only model turns following tool responses, preventing history corruption and silent user-prompt fusion.
  • History Hardening: Updated addHistory, stripThoughts, and stripToolCallIdPrefixes to proactively close unanswered tool turns and preserve the placeholder, maintaining clear role alternation.
  • Next Speaker Logic: Modified checkNextSpeaker to immediately return 'model' as the next speaker when the history ends with the interrupted placeholder, avoiding unnecessary LLM calls.
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 11, 2026
@github-actions

Copy link
Copy Markdown

📊 PR Size: size/L

  • Lines changed: 626
  • Additions: +591
  • Deletions: -35
  • 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 addresses issue #29574 by improving chat history hardening and role alternation preservation when handling empty or thought-only model turns following tool responses. It introduces helper functions like isToolResponseTurnAt and hasTrailingToolResponseUserTurn to accurately detect unanswered tool responses, ensures INTERRUPTED_RESPONSE_PLACEHOLDER is preserved in curated history, and updates the next speaker checker to handle these placeholder turns efficiently. Comprehensive unit tests have been added to validate these behaviors. The review feedback suggests applying optional chaining to the content property of history turns in three locations to prevent potential runtime TypeErrors if content is undefined.

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

Comment thread packages/core/src/core/geminiChat.ts Resolved
Comment thread packages/core/src/core/geminiChat.ts Resolved

@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 addresses issue #29574 by improving the handling of model turns in the chat history. It introduces the INTERRUPTED_RESPONSE_PLACEHOLDER to correctly manage empty or thought-only model turns that follow tool responses, ensuring proper role alternation and history curation. Key changes include the addition of helper functions to identify tool response turns, updates to addHistory to close unanswered tool calls, and modifications to stripThoughts and checkNextSpeaker to account for these interrupted turns. I have no feedback to provide as there were no review comments.

@gemini-cli gemini-cli Bot added priority/p2 Important but can be addressed in a future release. area/core Issues related to User Interface, OS Support, Core Functionality labels Nov 11, 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 addresses issue #29574 by ensuring role alternation is preserved when empty or thought-only model turns follow tool responses (including across synthetic binary acks). It introduces helper functions like isToolResponseTurnAt and hasTrailingToolResponseUserTurn to accurately identify tool response turns, updates history curation and hardening logic to insert an INTERRUPTED_RESPONSE_PLACEHOLDER instead of fusing user turns, and adjusts the next speaker checker to handle this placeholder. Comprehensive unit tests have been added to verify these changes. I have no feedback to provide as there are no review comments.

@DavidAPierce
DavidAPierce added this pull request to the merge queue Nov 11, 2026
Merged via the queue into google-gemini:main with commit be6b6d8 Nov 11, 2026
33 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 priority/p2 Important but can be addressed in a future release. size/l A large sized PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(core): 400 Bad Request "Requests ending with a model turn are not supported" when reading image files via ReadFile tool

2 participants