Open a rebuilt Gemini turn with its thought step

Merged
GO
1d ago
+32-10

Fixes a Gemini break for apps that use conversation memory with tools: once a stored conversation has a step where the model wrote text and called a tool, every later message in that conversation fails with:

400 Model turns with thought summaries must start with a thought block in thinking models.

When a persisted turn is rebuilt (no replay_blocks, which is the case for every stored row since #1036), MapsMessages::mapAssistantMessage() emits the step's text before the thought step it rebuilds from the tool call's thoughtSignature:

user_input, model_output, thought, function_call, function_result, user_input

Gemini returns these steps as thought, model_output, function_call, and thinking models reject a model turn that does not start with its thought step. Steps with no text were not affected, so the existing signature tests passed. A common case is the model saying "Let me check that." before it calls a tool.

The fix emits the rebuilt thought step before model_output. The verbatim replay_blocks path is unchanged.

We hit this in production on gemini-3.8-flash through the Interactions API. To check it, I sent the same captured history to Gemini in both orders: the current order returns the 400 above, and the reordered history returns 200.

Tests:

  • A mapping test for a rebuilt assistant turn with text and a signed tool call.
  • A persist-then-continue test where Gemini returns thought, model_output, function_call, checking the next request replays the thought first.

Both fail without the change. Other providers are unchanged, and there is no stored-data change, so no UPGRADE.md entry is needed.

šŸ¤– Generated with Claude Code