Skip to content

bug(core): 400 Bad Request "Requests ending with a model turn are not supported" after /rewind or interrupted stream #29530

Description

@asieveking

What happened?

When a conversation is rewound using the /rewind command or when an in-flight tool stream is aborted/interrupted, the in-memory chat history ends on a model turn (role: 'model').

If a subsequent API request is dispatched before a non-empty user message is recorded (e.g. following rewind, stream cancellation, or when trailing user turns are stripped/empty), GeminiChat.makeApiCallAndProcessStream sends the payload ending with a model turn directly to the Gemini API (ai.models.generateContentStream).

The Gemini API rejects the request with HTTP 400:

400 INVALID_ARGUMENT: Requests ending with a model turn are not supported.

At this point, the session fails with an unhandled API error and the user cannot proceed without discarding the session.

What did you expect to happen?

The client should enforce the Gemini API multi-turn role invariant prior to dispatching network requests, guaranteeing that request payloads never terminate on a model turn (by appending a continuation user turn such as Please continue., consistent with historyHardening.ts).

Client information

Client Information
  • CLI Version: 0.63.0-nightly
  • Operating System: macOS (Darwin arm64) / Linux / Windows
  • Package: @google/gemini-cli-core

Login information

Google Account / OAuth / API Key

Anything else we need to know?

Root cause analysis:
In packages/core/src/core/geminiChat.ts, makeApiCallAndProcessStream constructs contentsToUse from requestContents / contentsForPreviewModel. While historyHardening.ts contains role constraint enforcement (sentinel_end), makeApiCallAndProcessStream does not enforce the terminal user-role invariant before passing contentsToUse to stripToolCallIdPrefixes and generateContentStream.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/agentIssues related to Core Agent, Tools, Memory, Sub-Agents, Hooks, Agent Qualitystatus/need-triageIssues that need to be triaged by the triage automation.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions