Repository navigation
fix(core): ensure request contents do not end with a model turn - #29527
asieveking wants to merge 2 commits into
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 recurring 400 Bad Request error occurring when the Gemini API receives a request ending in a model turn. By enforcing an invariant that requires the terminal turn to be a user role, the system now automatically injects a continuation prompt when necessary. This ensures stability for operations like chat rewinds or interrupted streams without requiring manual user intervention. 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
|
|
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. |
|
📊 PR Size: size/M
|
There was a problem hiding this comment.
Code Review
This pull request ensures that requests to the Gemini API do not end with a model turn by appending a continuation user turn ("Please continue.") if the last turn in the chat history is from the model, preventing potential 400 Bad Request errors. A unit test has been added to verify this behavior. The reviewer recommends cloning the contentsToUse array before mutating it with .push() to avoid unexpected side effects on shared or read-only arrays.
| contentsToUse.push({ | ||
| role: 'user', | ||
| parts: [{ text: 'Please continue.' }], | ||
| }); |
There was a problem hiding this comment.
Directly mutating contentsToUse using .push() can lead to unexpected side effects if it is a shared or read-only array. However, using the spread operator (...) to append to arrays can lead to potential stack overflow errors if the array grows very large (e.g., long chat histories). To safely append to the array without mutating the original or risking stack overflow, clone the array elements and use .push() on the new array.
const updatedContents = contentsToUse.map(item => structuredClone(item));
updatedContents.push({
role: 'user',
parts: [{ text: 'Please continue.' }],
});
contentsToUse = updatedContents;References
- When appending to arrays, using a loop with push and structuredClone can be a safer alternative to using the spread operator (...) to avoid potential stack overflow errors when dealing with very large arrays.
|
@google-cla I signed it! |
|
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 only guarantee review and consideration of pull requests for issues that are explicitly labeled as 'help wanted'. This PR will be closed in 7 days if it remains without that designation. We encourage you to find and contribute to existing 'help wanted' issues in our backlog! Thank you for your understanding. |
Summary
Fixes a 400 Bad Request error (
Requests ending with a model turn are not supported) when sending requests where history ends with a model turn (e.g., following a/rewindoperation, stream interruption, or when trailing user turns are empty/stripped).Related Issues
Fixes #29530
Scope & Non-Goals
In-Scope (What this PR solves)
generateContentStreamnever terminate on amodelturn by injecting a continuation user turn (Please continue.).structuredClonebefore appending, eliminating unexpected side-effects on shared/read-only arrays./rewind, mid-stream aborts/cancellations, or cases where user prompts are stripped by upstream filters.Non-Goals (Out of Scope)
functionCallandfunctionResponse(which is governed by separate mechanisms inhistoryHardening.ts).thoughtSignaturegeneration, preservation, or validation for reasoning models (Gemini 2.0 / 3.0).Details
The Gemini API strictly requires that multi-turn request contents alternate between
userandmodelroles, with the terminal turn always belonging touser. InGeminiChat.makeApiCallAndProcessStream,contentsToUsecould terminate with amodelturn under several real-world conditions (such as rewinding the chat state or aborting an in-flight tool call).This patch enforces the invariant directly before dispatching to
generateContentStream: if the payload ends with amodelturn, a continuation user turn (Please continue.) is appended, guaranteeing API compliance without disrupting conversation semantics or mutating input references.How to Validate
npm test -w @google/gemini-cli-core -- packages/core/src/core/geminiChat.test.ts --run
GeminiChat > sendMessageStream > should append a continuation user turn if contents ends with a model turn before calling generateContentStreamPre-Merge Checklist