fix: resolve "function response turn must come immediately after function call" error#26691
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 a critical issue where the Gemini API would reject requests due to an interrupted call-response sequence. By refining the history curation logic, the system now correctly identifies and includes essential function-related turns that were previously being filtered out, ensuring compliance with the API's strict ordering requirements. 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
|
There was a problem hiding this comment.
Code Review
This pull request updates the isValidContent validation logic in GeminiChat to ensure that model turns containing function calls or function responses are not incorrectly filtered out when the text field is empty. A corresponding test case has been added to verify this behavior. The review feedback suggests further improving this logic by also preserving parts with inlineData or fileData and ensuring that the text property is trimmed before checking for emptiness to prevent whitespace-only values from being accepted.
Summary
Fixes the error "Please ensure that function response turn comes immediately after a function call turn." which was occurring frequently when using
gemini-3.1-flash-lite-preview. This issue was caused by theextractCuratedHistoryfunction inadvertently dropping valid model turns that contained function calls but no text (or empty text), thereby breaking the strict call-response sequence required by the Gemini API.Details
geminiChat.ts: ModifiedisValidContentto consider a part valid if it contains afunctionCallorfunctionResponse, even if itstextproperty is an empty string. This prevents valid tool calls from being filtered out of the history sent to the API.geminiChat.test.tsto verify thatgetHistory(true)(curated history) correctly retains model turns with function calls and empty text.How to Validate
packages/coredirectory to ensure the new test passes and no regressions occurred:should fail if the stream ends with an empty part and has no finishReasonpasses successfully.Pre-Merge Checklist