Skip to content

fix: resolve "function response turn must come immediately after function call" error#26691

Merged
scidomino merged 5 commits into
google-gemini:mainfrom
danielweis:fix-function-call-sequence
May 8, 2026
Merged

fix: resolve "function response turn must come immediately after function call" error#26691
scidomino merged 5 commits into
google-gemini:mainfrom
danielweis:fix-function-call-sequence

Conversation

@danielweis
Copy link
Copy Markdown
Contributor

@danielweis danielweis commented May 8, 2026

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 the extractCuratedHistory function 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

  • Fix in geminiChat.ts: Modified isValidContent to consider a part valid if it contains a functionCall or functionResponse, even if its text property is an empty string. This prevents valid tool calls from being filtered out of the history sent to the API.
  • New Test: Added a test case in geminiChat.test.ts to verify that getHistory(true) (curated history) correctly retains model turns with function calls and empty text.

How to Validate

  1. Run the tests in the packages/core directory to ensure the new test passes and no regressions occurred:
    npx vitest run packages/core/src/core/geminiChat.test.ts
  2. Verify that the tests should fail if the stream ends with an empty part and has no finishReason passes successfully.

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

@danielweis danielweis requested a review from a team as a code owner May 8, 2026 08:26
@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 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

  • API Sequence Fix: Updated the content validation logic to ensure that model turns containing function calls or responses are preserved even when the text field is empty, preventing API sequence errors.
  • Regression Testing: Added a new test case to verify that curated history correctly retains model turns with function calls, ensuring the fix is validated and future-proofed.
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.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

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 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.

Comment thread packages/core/src/core/geminiChat.ts
@gemini-cli gemini-cli Bot added the status/need-issue Pull requests that need to have an associated issue. label May 8, 2026
@scidomino scidomino added this pull request to the merge queue May 8, 2026
Merged via the queue into google-gemini:main with commit 6b9b778 May 8, 2026
46 of 47 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status/need-issue Pull requests that need to have an associated issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants