senpi icon indicating copy to clipboard operation
senpi copied to clipboard

google-vertex: Gemini 4 Carbon 400s on merged functionResponse+text user turns and historical unsigned tool calls

Open yiminhale opened this issue 3 weeks ago • 5 comments

What happened?

Using google-vertex/gemini-4-carbon (senpi 2026.11.9-2, verified on main @ b8fe5e0) hits two 400 INVALID_ARGUMENT errors in packages/ai/src/api/google-shared.ts:

  1. Requests ending with a model turn are not supported. when functionResponse and text share the final user turn: appendContent() (google-shared.ts:214-220) folds any adjacent role: "user" entry into the preceding functionResponse user turn (parts: [{ functionResponse }, { text }]). Whenever a user prompt, reminder, or aborted-turn retry lands right after toolResult at the end of contents, Vertex AI gemini-4-carbon rejects the request with 400 Requests ending with a model turn are not supported.. Keeping functionResponse parts and plain text parts in separate role: "user" entries (prevHasFn === currHasFn) returns 200 OK.

  2. Function call is missing a thought_signature on cross-model session resume: convertMessages() (google-shared.ts:309-310) gates unsigned tool-call text replay behind isCurrentTurn: requiresThoughtSignatureReplay(model.id) && isCurrentTurn && !hasValidFirstSignature When resuming a session (-c) containing historical tool calls from non-Gemini models, Vertex AI gemini-4-carbon rejects unsigned functionCall parts in earlier turns as well, not only the current turn.

Steps to reproduce

  1. Run GOOGLE_CLOUD_PROJECT=<proj> GOOGLE_CLOUD_LOCATION=global senpi -ne --provider google-vertex --model gemini-4-carbon.
  2. Trigger a tool call whose toolResult is followed by a user/custom text message before the next completed assistant turn (or resume -c a session containing prior Claude/OpenAI tool calls).
  3. Observe Vertex 400 INVALID_ARGUMENT (Requests ending with a model turn are not supported. / Function call is missing a thought_signature).

Expected behavior

  • appendContent() does not merge functionResponse user turns with plain text user turns (prevHasFn === currHasFn).
  • replayUnsignedToolCallsAsText replays unsigned historical toolCall + paired toolResult entries as text across all turns when requiresThoughtSignatureReplay(model.id) is true.

Version

2026.11.9-2 (main @ b8fe5e0)

yiminhale avatar Nov 10 '26 18:11 yiminhale

[AI-assisted disclosure per CONTRIBUTING.md] Drafted with Gemini 4 Carbon on macOS arm64 after reproducing against live Vertex AI gemini-4-carbon (senpi 2026.11.9-2) and verifying the local patch.

yiminhale avatar Nov 10 '26 18:11 yiminhale

Thanks @yiminhale, this is a precise report with code locations, which makes it easy to act on. The first symptom (a user turn that folds functionResponse and text together gets a 400) looks like the same root cause as the open #3633, so I've linked them. The unsigned historical tool-call part reads as new. It's passed to the provider owners, and we'll follow up here.

code-yeongyu avatar Nov 10 '26 18:11 code-yeongyu

Current main leaves unsigned historical functionCall parts in the Gemini 4 request: 8 new regression cases fail and 23 pass at dc5c9f1a7ae2b72a79fa5a47f4bc825c276aa23d. The reproduction resumes Claude Opus tool history into gemini-4-neon through google-generative-ai, google-vertex, and a vendor-prefixed model ID. Each cross-model case finds a structured call in the older turn, and the #3633 ordering case emits [functionResponse, text].

I'm carrying #4337's functionResponse-last ordering into this fix, extending the existing text replay to every unsigned historical step, and including #7414's prefix matching. The RED command was bun run --cwd packages/ai test test/google-shared-gemini3-unsigned-tool-call.test.ts test/google-shared-role-alternation.test.ts.

code-yeongyu avatar Nov 27 '26 20:11 code-yeongyu

The same two regression files now pass all 32 cases. convertMessages replays an unsigned tool-call step and its paired result as text regardless of turn age, preserves a same-model step whose first call has a valid signature, recognizes vendor-prefixed model IDs, and keeps functionResponse parts last for Gemini 3+ while retaining Gemini 2's legacy ordering.

This supersedes #4337's partial-turn guard and includes the behavior proposed by #7414. Next are the wider Google suites and a live session with prior Opus tool history.

code-yeongyu avatar Nov 27 '26 20:11 code-yeongyu

Milestone: proposed fix is up as #7825 (fix/gemini-historical-thought-signatures), head 5d1a6e5.

Root cause: the existing guard only text-replayed an unsigned first functionCall in the current turn. Gemini 3/4 applies the thought-signature rule to every replayed step, so a historical or cross-provider step still reached the API as an unsigned functionCall.

What the PR changes:

  • Every unsigned tool-call step is replayed as text (call plus paired result) for Gemini 3+, including historical turns and sessions resumed after a provider switch.
  • Vendor- and path-prefixed model ids are recognized (google-gemini-4-neon, models/gemini-3-pro-preview), absorbing #7414.
  • For Gemini 3+ only, functionResponse parts remain last when user text is merged with tool results, covering #3633's ordering 400. Gemini 2 keeps the legacy order.

Request-shape proof at base 5f05f4e: the focused RED run had 8 failed / 24 passed; the fixed head has 32/32 passing, and the Google provider sweep is 140 passed / 9 skipped. The build exits 0.

Live QA: an Opus tool-call turn followed by google/gemini-4-neon completed three consecutive bash calls with no 400. The live run used the Google API-key path; the reported Vertex gemini-4-carbon 400 could not be reproduced without a Vertex credential, so the unit-level serialized-request assertions carry the request-shape proof. The full local suite is still running; I'll report its result with the merge evidence.

code-yeongyu avatar Nov 30 '26 07:11 code-yeongyu