Repository navigation
fix(core): prevent thoughtSignature-only turns and preserve multimodal tool responses - #30153
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 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
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 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.
There was a problem hiding this comment.
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.
|
/gemini review |
There was a problem hiding this comment.
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.
Summary
Fixes
400 Bad Request ("Requests ending with a model turn are not supported")and empty-response retry loops when usingread_fileon images (or receivingthoughtSignature-only model responses after tool execution).When Gemini 2.5 / 3 models return a post-tool response containing only
thoughtSignature(or empty text alongsidethoughtSignature), the history validation and sanitization pipeline previously treatedthoughtSignatureas an active content key. This allowed empty/signature-only model turns intoagentHistory, bypassedcloseUnansweredToolResponseTurn, and causedstripToolCallIdPrefixesto drop the model turn afterensureTerminalUserTurnhad already run—leaving the outgoing API payload ending on amodelturn or triggering an invalid-stream retry loop. Additionally, multimodal tool response parts (such as siblinginlineDataimage parts) were dropped in subagent execution (LocalAgentExecutor) and when dynamic model configuration lacked an explicitmultimodalToolUsefeature flag.Details
thoughtSignaturefrom active content-key checks (packages/core/src/core/geminiChat.ts):isValidContent,isEmptyTextPart, andstripToolCallIdPrefixesto ignore'thoughtSignature'when checking whether aPartcarries active payload keys (functionCall,functionResponse,inlineData,fileData, etc.).closeUnansweredToolResponseTurnto check!hasNonEmptyParts(targetTurn.content)and use immutablethis.agentHistory.set(...)when insertingINTERRUPTED_RESPONSE_PLACEHOLDER.makeApiCallAndProcessStreamsostripToolCallIdPrefixesexecutes beforeensureTerminalUserTurn, and preservedmodelturns that immediately follow auserfunctionResponseturn by falling back toINTERRUPTED_RESPONSE_PLACEHOLDERinstead of dropping the turn.packages/core/src/config/models.ts):supportsMultimodalFunctionResponseto passconfig?.getHasAccessToPreviewModel?.() ?? truetoresolveModeland fall back to/^gemini-3(\.|-|$)/.test(resolved)whendefinition?.features?.multimodalToolUseisundefined.Part[]inLocalAgentExecutor(packages/core/src/agents/local-executor.ts):syncResultsinprocessFunctionCallsfromMap<string, Part>toMap<string, Part[]>so allcall.response.responseParts(including siblinginlineDataimage parts whensupportsMultimodalFunctionResponseisfalse) are forwarded instead of onlyresponseParts[0].Related Issues
Fixes #30074
How to Validate
GeminiChat, model feature resolution, andLocalAgentExecutor:npm test -w @google/gemini-cli-core -- \ src/core/geminiChat.test.ts \ src/config/models.test.ts \ src/agents/local-executor.test.tsread_fileand 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.tsPre-Merge Checklist