Skip to content

fix(ai): keep Gemini requests from ending on a model turn - #64641

Open
opencode-agent[bot] wants to merge 1 commit into
devfrom
fix/gemini-trailing-model-turn
Open

opencode-agent[bot] wants to merge 1 commit into
devfrom
fix/gemini-trailing-model-turn

Conversation

@opencode-agent

@opencode-agent opencode-agent Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Fixes the user-visible error in #64628. The root cause in that session isn't confirmed yet.

Problem

Gemini rejects any request whose contents end on a model turn:

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

lowerMessages in packages/ai/src/protocols/gemini.ts lowers whatever history it gets. When that history ends on an assistant message, the request goes out ending on model, and the session stops until the user prompts again. In #64628 it happened three times in one session, each time on the request right after a tool-calling step.

Unanswered local tool calls are already answered with Tool result missing by normalizeToolHistory. So the tail that reaches Gemini is an assistant message with text or reasoning only.

Fix

Before the request is sent, lowerMessages now makes sure the contents end on a user turn:

  • A trailing model turn that contains only thought parts is dropped, since the model can't continue from it.
  • Any other trailing model turn without function calls gets a user turn, Continue., appended. It isn't persisted, and adding it at the end keeps the cached prefix intact.
  • A trailing model turn that has function calls is left as is, because a text turn can't stand in for their function responses.

This only changes Gemini requests. Other protocols are unchanged.

Testing

  • bun test test/provider/gemini.test.ts (packages/ai): 63 pass. Adds tests for both tail shapes and updates two that ended on a model turn.
  • bun test (packages/ai): all pass.
  • bun run typecheck (packages/ai): no errors. oxlint on the changed files is clean.

Requested by: Filip via Slack


Added by the opencode bot (AI-generated).

Related: #64628, #55694

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change looks sound. I ran the PR's Gemini tests against the base, and the trailing-assistant cases there still produce contents ending on a model turn. With this PR they end on a user turn. The Gemini test file passes (63 tests), the rest of the packages/ai suite passes, and tsc is clean. I couldn't check it against the real Gemini API.

Public API: unchanged.

Handling the tail inside the protocol matches what mistral-chat.ts already does for a trailing assistant message. Dropping a thought-only tail and adding Continue. at the end both keep the cached prefix intact, and a tail with function calls is correctly left alone. One gap remains: since the cause in #64628 isn't confirmed, a tail that ends on provider-executed function calls with no results would still fail. The session export should show whether that's the case there, so I've listed #64628 as related rather than fixed.

Related: #64628, #55694

This branch has not been deployed

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

1 participant