Skip to content

fix(provider): keep Gemini requests from ending with a model turn - #55382

Closed
heimoshuiyu wants to merge 1 commit into
anomalyco:v1from
heimoshuiyu:gemini-turn-fix
Closed

heimoshuiyu wants to merge 1 commit into
anomalyco:v1from
heimoshuiyu:gemini-turn-fix

Conversation

@heimoshuiyu

Copy link
Copy Markdown
Contributor

Fixes #47034 (same root cause as #45359) on the V1 line.

Problem

Gemini rejects any request whose contents ends with a model turn:

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

V1 produces that shape whenever it continues from a persisted assistant tail:

  • max-steps priming appends MAX_STEPS_PROMPT as an assistant message
  • unknown-finish and empty-output retries replay history that ends with the assistant message

Fix

Append a synthetic user turn ("Continue.") in ProviderTransform.message when the last message is an assistant turn, for the Gemini SDKs only (@ai-sdk/google, @ai-sdk/google-vertex):

  • outbound, in-memory only — the synthetic turn is never persisted, so transcripts and subsequent requests are unchanged
  • appending at the tail leaves the provider's implicit prefix cache intact
  • other providers keep true assistant prefill
  • mirrors the existing wire-sequence repair in normalizeMessages, which inserts a synthetic assistant "Done." when a tool turn is followed by a user turn

Testing

  • bun test test/provider/transform.test.ts — 585 pass, 0 fail
  • bun typecheck (packages/opencode) — no new errors vs pristine v1 tip (42 pre-existing, verified via stash comparison)

@github-actions github-actions Bot added contributor needs:compliance This means the issue will auto-close after 2 hours. labels Oct 14, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

Please edit this PR description to address the above within 72 hours, or it will be automatically closed.

If you believe this was flagged incorrectly, please let a maintainer know.

@github-actions

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

@piercypixel

Copy link
Copy Markdown

Cross-linking: #55514 traces another way v1 ends up with a trailing model turn, with no prefill involved. When the wall clock steps backward inside a step, Identifier.create mints a smaller ID for the tool part, it replays before its own step-start, and the step's reasoning becomes a trailing assistant message. #55515 fixes the generator and adds a Gemini tail guard in ProviderTransform.message that drops a reasoning-only assistant tail and otherwise primes a user turn the same way this PR does. Happy to rebase onto this one if it lands first.

@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been automatically closed because it was not updated to meet our contributing guidelines within the 72-hour window.

Feel free to open a new pull request that follows our guidelines.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants