Repository navigation
fix(core): preserve interrupted placeholder after empty model responses to tool turns - #30228
Conversation
Summary of ChangesHello, 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
Using Gemini Code AssistThe 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
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 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
|
|
📊 PR Size: size/L
|
|
/gemini review |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 review |
There was a problem hiding this comment.
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.
Summary
Fixes
400 Bad Request ("Requests ending with a model turn are not supported")and silent user-prompt fusion intofunctionResponseturns when the model responds to a tool output (such asread_fileon an image/binary file) with an empty (parts: []), thought-only, orthoughtSignature-only turn ({ text: '', thoughtSignature: '...' }).Details
When
read_filereturns image or binary content (either via native multimodalfunctionResponse.partson Gemini 3 models orBINARY_INJECTION_KEYsibling[functionResponse, inlineData]turns on Gemini 2.5 models) and the model stream terminates withparts: [], thought-only parts, or athoughtSignature-only empty text part:GeminiChat(packages/core/src/core/geminiChat.ts):isToolResponseTurnAtto recognize tool response turns both directly and acrossBINARY_INJECTION_KEYsynthetic binary acknowledgments ([user(functionResponse), model(syntheticBinaryAck), user(inlineData)]).addHistoryto invokecloseUnansweredToolCallsTurn()andcloseUnansweredToolResponseTurn()before appending a non-functionResponseuserturn (e.g., IDE context updates), preventing empty/thought-only model turns from being stranded in the middle of history.extractCuratedHistory,stripThoughts, andstripToolCallIdPrefixesto replace empty (parts: []) orthoughtSignature-only model turns following a tool response withINTERRUPTED_RESPONSE_PLACEHOLDER([The previous response was interrupted before it completed.]) rather than dropping them.packages/core/src/utils/historyHardening.ts):stripThoughtsinhardenHistory,scrubHistory, andscrubContentsto handleparts: []andBINARY_INJECTION_KEYtool response sequences consistently, preservingINTERRUPTED_RESPONSE_PLACEHOLDERso consecutiveuserturns are not fused intofunctionResponseturns.packages/core/src/utils/nextSpeakerChecker.ts):checkNextSpeakerto immediately return{ next_speaker: 'model' }without making an LLMgenerateJsoncall whenchat.getHistory()ends withINTERRUPTED_RESPONSE_PLACEHOLDER, avoiding sending acontentsarray ending on amodelturn.Related Issues
Fixes #29574
How to Validate
npm test -w @google/gemini-cli-core -- src/core/geminiChat.test.ts src/utils/historyHardening.test.ts src/utils/nextSpeakerChecker.test.tsReadFileToolreading PNG images ongemini-3-flash-preview(multimodalfunctionResponse.parts) andgemini-2.5-flash(BINARY_INJECTION_KEY[functionResponse, inlineData]) followed byparts: [], thought-only, orthoughtSignature-only model responses.checkNextSpeaker(Please continue.),chat.addHistory(IDE context delta) +sendMessageStream,chat.getHistory(true)/chat.getHistory(false), Context ManagementapiHistoryOverride, and session resume (convertSessionToClientHistory).Pre-Merge Checklist