fix(hooks): preserve non-text parts in fromHookLLMRequest#23340
fix(hooks): preserve non-text parts in fromHookLLMRequest#23340mk-imagine wants to merge 4 commits into
Conversation
fromHookLLMRequest creates brand-new Content objects with only text parts, discarding all non-text parts (functionCall, functionResponse, inlineData, thought, etc.) from baseRequest. This breaks any BeforeModel hook that modifies text content in conversations containing tool calls — the tool call/response history is silently destroyed, causing the model to loop. When baseRequest is provided, merge hook messages back into baseRequest.contents instead of replacing them: update text parts from hook messages, preserve non-text parts in their original positions, and keep skipped entries (pure functionCall/functionResponse) intact. Falls back to current text-only behavior when no baseRequest is provided.
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
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 delivers a crucial enhancement to the 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 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 counter productive. 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
|
There was a problem hiding this comment.
Code Review
This pull request addresses a critical bug in fromHookLLMRequest that caused non-text parts of a request to be discarded after a BeforeModel hook modified text content, leading to potential infinite loops with tool calls. The fix intelligently merges the hook's text modifications back into the original request, preserving crucial non-text parts like functionCall and functionResponse. The implementation is robust, and the addition of a comprehensive test suite ensures the fix is effective and covers various edge cases. The changes are well-executed and I found no issues.
|
Hi there! Thank you for your interest in contributing to Gemini CLI. To ensure we maintain high code quality and focus on our prioritized roadmap, we have updated our contribution policy (see Discussion #17383). We only guarantee review and consideration of pull requests for issues that are explicitly labeled as 'help wanted'. All other community pull requests are subject to closure after 14 days if they do not align with our current focus areas. For this reason, we strongly recommend that contributors only submit pull requests against issues explicitly labeled as 'help-wanted'. This pull request is being closed as it has been open for 14 days without a 'help wanted' designation. We encourage you to find and contribute to existing 'help wanted' issues in our backlog! Thank you for your understanding and for being part of our community! |
|
LGTM, can you merge main and adapt to the latest |
|
Hi there! Thank you for your interest in contributing to Gemini CLI. To ensure we maintain high code quality and focus on our prioritized roadmap, we have updated our contribution policy (see Discussion #17383). We only guarantee review and consideration of pull requests for issues that are explicitly labeled as 'help wanted'. All other community pull requests are subject to closure after 14 days if they do not align with our current focus areas. For this reason, we strongly recommend that contributors only submit pull requests against issues explicitly labeled as 'help-wanted'. This pull request is being closed as it has been open for 14 days without a 'help wanted' designation. We encourage you to find and contribute to existing 'help wanted' issues in our backlog! Thank you for your understanding and for being part of our community! |
Summary
fromHookLLMRequestcreates newContentobjects with only text parts, discarding all non-text parts (functionCall,functionResponse,inlineData,thought, etc.) frombaseRequest. This breaks anyBeforeModelhook that modifies text content in conversations containing tool calls — tool call/response history is silently destroyed, causing the model to lose context and loop.Root cause
toHookLLMRequestintentionally filters Content entries to text-only messages (documented design for a simplified hook API). Some Content entries are skipped entirely (purefunctionCall/functionResponsewith no text), and others have their non-text parts stripped.However,
fromHookLLMRequestdoes not reverse this mapping — it replaces all contents with new text-only entries instead of merging text changes back into the original structure.Reproduction
Any
BeforeModelhook that modifies text + an agent loop with tool calls:BeforeModelhook receives the text-onlymessages, modifies text, returns modifiedllm_requestfromHookLLMRequestcreates new text-onlycontents, destroyingfunctionCall/functionResponsepartsQueries that don't trigger text modification (hook returns
{}) work correctly because the original SDK request with all parts is preserved.Fix
When
baseRequestis provided,fromHookLLMRequestnow merges hook messages back intobaseRequest.contents:baseRequest.contentsin order, maintaining a cursor intohookRequest.messagesfunctionCall) → preserve as-is in original positionbaseRequest→ fall back to current behavior (fully backwards-compatible)Test plan
functionCallparts) — non-text parts preservedfunctionCall— text updated,functionCalluntouchedtoHookLLMRequest's join)baseRequestundefined → fallback to current behaviorbaseRequestwith no contents → fallback