{"slug": "gemini-requests-ending-with-a-model-turn-are-not-supported-the-cause-and-the-fix", "title": "Gemini \"Requests ending with a model turn are not supported\": the cause and the fix", "summary": "Google's Gemini API now returns a 400 INVALID_ARGUMENT error with the message \"Requests ending with a model turn are not supported\" when the last non-empty turn in a request's contents has the role model, starting with Gemini 3.6 Flash and Gemini 3.5 Flash-Lite and applying to all later models including Gemini 3.8 Flash and the Gemini 4 models. The change breaks prefill code that worked on Gemini 2.5 and 3.5 Flash, and on October 8, 2026 the API began routing gemini-3.5-flash requests to gemini-3.6-flash, so unchanged code started failing; on December 1, 2026 gemini-2.5-pro routes to gemini-3.8-flash and gemini-2.5-flash routes to gemini-3.5-flash-lite, both of which reject prefill. Google's migration guide directs developers to end every request on a non-empty user turn, replace prefill with system_instruction or structured output, and send a function response for every functionCall, including cancelled ones, before the next request.", "body_md": "[← all posts](https://www.evalshift.dev/blog)\n\n# Gemini \"Requests ending with a model turn are not supported\": the cause and the fix\n\nGemini 3.6 Flash and later return a 400 when a request ends on a model turn. Why prefill stopped working, five causes in agent loops, and the fix.\n\nGemini returns `400 INVALID_ARGUMENT` with the message \"Requests ending with a model turn are not\nsupported.\" when the last non-empty turn in `contents` has the role `model`. Starting with Gemini\n3.6 Flash and Gemini 3.5 Flash-Lite, Google no longer supports prefilling a model turn, and the rule\napplies to every later Gemini model too, Gemini 3.8 Flash and the Gemini 4 models included. Code that\nworked on Gemini 2.5 or 3.5 Flash breaks on the first request.\n\nThe fix takes three changes:\n\n- +**End every request on a user turn that has content.** That turn is the user's text or the\nfunction responses for the model's last function calls. An empty user turn does not count: Google\nchecks the last*non-empty* turn.\n- +**Replace prefill with `system_instruction` or structured output.** If you prefilled`{` to force\nJSON or`Translation:` to suppress a preamble, Google's migration guide names those two as the\nreplacements.\n- +**Close every function call before the next request.** Send a function response for each`functionCall` the model made, including a cancelled one, in a`user` turn with the call's`id` and`name` .\n\nYou may not have changed anything in your code. On October 8, 2026 the Gemini API started routing\nrequests for `gemini-3.5-flash` to `gemini-3.6-flash`, so prefill code that never changed its model\nstring began returning this error. On December 1, 2026 the Gemini API does the same to\n`gemini-2.5-pro` (routed to `gemini-3.8-flash`) and `gemini-2.5-flash` (routed to\n`gemini-3.5-flash-lite`). Both targets reject prefill.\n\nThe rest of this post covers the exact error, the five ways an agent loop ends up sending a model turn last, code for each fix, and Claude's version of the same 400. Facts were checked against Google's and Anthropic's docs on November 17, 2026.\n\n## The error\n\nOn the Gemini API, and on Vertex AI (now documented as Gemini Enterprise Agent Platform), the response body is:\n\n```\n{\n  \"error\": {\n    \"code\": 400,\n    \"message\": \"Requests ending with a model turn are not supported.\",\n    \"status\": \"INVALID_ARGUMENT\"\n  }\n}\n```\n\nIt is a client error, so retrying the same request fails the same way. One voice-agent framework reported its agent going silent because it wrapped this 400 as a retryable connection error and resent the same history in a loop. Treat it as a bug in the request, not as an outage.\n\n## Why Gemini rejects a request that ends on a model turn\n\nGoogle's [Gemini 3.6 migration guide ↗](https://aistudio.google.com/docs/whats-new-gemini-3.6#prefilled-model-turn-validation)\nlists it as one of two API changes that start with Gemini 3.6 Flash and Gemini 3.5 Flash-Lite and\n\"apply to these models and all future Gemini model releases\": prefilling model turns is no longer\nsupported, and if the last non-empty turn is a `model` turn, the API returns a `400`. The Gemini 3.8\nFlash checklist repeats it as \"Remove prefilled model turns and ensure the final user turn contains\nnon-empty text.\" Vertex's\n[Gemini 3.8 Flash guide ↗](https://docs.cloud.google.com/gemini-enterprise-agent-platform/models/guides/gemini-3-8-flash)\nsays history payloads cannot end with a `model` role turn.\n\nPrefill was a common technique. You end `contents` with a partial `model` turn, and the model\ncontinues from it:\n\n```\n{\n  \"contents\": [\n    {\"role\": \"user\", \"parts\": [{\"text\": \"Translate 'Hello world' to Spanish.\"}]},\n    {\"role\": \"model\", \"parts\": [{\"text\": \"Translation:\"}]}\n  ]\n}\n```\n\nGemini 2.5 and the 3.x models before 3.6 Flash accept this request, and Gemini 3.6 Flash and every later model reject it. Here is which model IDs fall on which side, and the reroutes that move a model ID across without a code change:\n\n| Model ID you call | Prefill / model turn last | Notes | \n|---|---|---|\n| `gemini-2.5-pro` ,`gemini-2.5-flash` on the Gemini API | Accepted until December 1, 2026 | From December 1 these route to `gemini-3.8-flash` and`gemini-3.5-flash-lite` and get the 400 | \n| `gemini-3.5-flash` on the Gemini API | 400 since October 8, 2026 | Routed to `gemini-3.6-flash` | \n| `gemini-3.6-flash` ,`gemini-3.5-flash-lite` | 400 | The models the rule started with | \n| `gemini-3.7-flash` on the Gemini API | 400 | Routed to `gemini-3.8-flash` since October 8, 2026 | \n| `gemini-3.8-flash` ,`gemini-4-*` | 400 | \"All future Gemini model releases\" | \n\nThe reroutes are why this error spikes without a deploy. The\n[Gemini model deprecation checklist](https://www.evalshift.dev/blog/gemini-model-deprecation-migration-checklist) has the\nfull list of dates for both surfaces.\n\n## Five ways an agent loop ends on a model turn\n\nMost reports of this error come from agent frameworks, not from code that prefills on purpose. Before\nyou change anything, print the role and part types of the last three entries in `contents` for a\nfailing request. One of these five patterns is almost always the cause.\n\n1. +**Deliberate prefill.** A trailing`model` turn with`{` ,```` ``` json ```` ,` Answer:` or a persona\nopener, added to force a format. This is the case Google's guide describes.\n2. +**Continuation after a cut-off answer.** When a response stops at`MAX_TOKENS` , some code appends\nthe partial answer as a`model` turn and calls again so the model continues from it. That is a\nprefill too.\n3. +**An empty last user turn.** Google checks the last*non-empty* turn, and its 3.8 Flash guide says\nempty turns are dropped or fail validation. A user turn with`\"\"` , only whitespace, or a part your\nSDK serializes to nothing leaves the`model` turn before it last. The voice-agent report above was\nthis case. The user interrupted the agent mid-reply, the interruption produced an empty user\nmessage, it was dropped, and the request ended on the partial reply.\n4. +**An unanswered function call.** The model's`functionCall` turn is appended to history, but the\ntool was cancelled, timed out or never ran, so no function response follows it. The next request\nends on that`model` turn. Several framework issues trace the error to detached or cancelled tool\ncalls.\n5. +**History rebuilt in the wrong order.** History reloaded from a database sorted by a timestamp\nthat went backwards, trimmed to fit the context window from the wrong end, or merged from two\nsessions can put a`model` turn last. One coding agent hit it after a backward clock step made\nstored message IDs non-monotonic.\n\nFramework issues on GitHub describe two more triggers, but Google's docs don't: a function response\nthat is only an image, and a user turn that mixes a `functionResponse` part with a text part. Google's\n3.8 Flash checklist tells you to place multimodal assets inside the function response payload. If\nyour tools return images, check that first.\n\n## Fix 1: end every request on a non-empty user turn\n\nCheck the request before sending it, and fail in your own code with a message that points at the bug,\ninstead of getting a 400 from Google. With the `google-genai` SDK:\n\n``` php\nfrom google.genai import types\n\ndef _has_content(part: types.Part) -> bool:\n    if part.text is not None:\n        return bool(part.text.strip())\n    return any(\n        (part.function_call, part.function_response, part.inline_data, part.file_data)\n    )\n\ndef check_ends_on_user_turn(contents: list[types.Content]) -> None:\n    \"\"\"Raise if Gemini 3.6+ would reject this history with a 400.\"\"\"\n    for content in reversed(contents):\n        if any(_has_content(p) for p in content.parts or []):\n            if content.role != \"user\":\n                raise ValueError(\n                    f\"history ends on a {content.role!r} turn; \"\n                    \"Gemini 3.6 Flash and later reject prefilled model turns\"\n                )\n            return\n    raise ValueError(\"history has no non-empty turn\")\n```\n\nCall it right before `client.models.generate_content(...)`. Then fix the cause the check reports. Don't\nmake the error go away by appending a filler user turn such as \"Continue.\": that changes what the\nmodel is answering, and it hides causes 3 to 5, which are bugs in your history handling.\n\nThe exception is continuing a cut-off answer (cause 2). There you do want the model to go on, so say\nso in a real user turn, for example \"Your previous answer stopped mid-sentence. Continue from the last\ncomplete sentence without repeating it.\" Or raise `max_output_tokens` and skip the continuation.\n\n## Fix 2: replace prefill with system_instruction or structured output\n\nPrefill had two jobs: suppress the preamble and force a format. Google's guide gives a replacement for each.\n\nTo suppress a preamble, say so in `system_instruction`:\n\n``` python\nfrom google import genai\n\nclient = genai.Client()\n\nresponse = client.models.generate_content(\n    model=\"gemini-3.8-flash\",\n    contents=\"Translate 'Hello world' to Spanish.\",\n    config={\"system_instruction\": \"Output only the translation, with no introductory text.\"},\n)\n```\n\nTo force JSON, use structured output instead of a prefilled `{`. The schema is enforced by the API,\nwhich a prefill never guaranteed:\n\n``` python\nfrom pydantic import BaseModel\n\nclass Verdict(BaseModel):\n    label: str\n    confidence: float\n\nresponse = client.models.generate_content(\n    model=\"gemini-3.8-flash\",\n    contents=f\"Classify this support ticket: {ticket_text}\",\n    config={\n        \"response_mime_type\": \"application/json\",\n        \"response_json_schema\": Verdict.model_json_schema(),\n    },\n)\nverdict = Verdict.model_validate_json(response.text)\n```\n\nIf you prefilled a persona opener or a few words to steer the tone, that moves into\n`system_instruction` too. Removing a prefill changes the output even when the request succeeds, so\ncompare the old and new answers on real inputs before you ship (see the last section).\n\n## Fix 3: close every function call before the next request\n\nIn the function-calling loop, a turn that ends with a `functionCall` must be followed by a `user`\nturn that carries a function response for each call, with the call's `id` and `name`. Google's 3.8\nFlash checklist requires both fields on every `FunctionResponse` in `generateContent`. Append the\nmodel's turn exactly as returned, because Gemini 3 models expect their thought signatures back in\nfunction-calling history. Google's changelog lists `thought_signature` validation among the breaking\nchanges for the December 1 reroutes.\n\n```\nresponse = client.models.generate_content(model=\"gemini-3.8-flash\", contents=contents, config=config)\nmodel_turn = response.candidates[0].content\ncontents.append(model_turn)  # as returned, thought signatures included\n\ncalls = [p.function_call for p in model_turn.parts if p.function_call]\nif calls:\n    parts = []\n    for call in calls:\n        try:\n            result = {\"result\": run_tool(call.name, call.args)}\n        except ToolCancelled:\n            result = {\"error\": \"The tool call was cancelled before it finished.\"}\n        parts.append(\n            types.Part.from_function_response(name=call.name, response=result, id=call.id)\n        )\n    contents.append(types.Content(role=\"user\", parts=parts))\n```\n\nA cancelled or failed tool still gets a function response, with the error in it. The model can then explain the failure or try something else, and the history never ends on the model's call. If the user interrupted, add their new message as text in that same user turn after the function responses, or as the next user turn. Don't drop it.\n\n## Claude returns the same kind of 400\n\nIf you route the same agent to Claude, the equivalent rule is older. Anthropic's\n[API errors page ↗](https://platform.claude.com/docs/en/api/errors#prefill-not-supported) says Claude\n4.6 and later models, and Claude Mythos Preview, do not support prefilling the assistant message. A\nrequest whose last message is a prefilled assistant turn returns a 400 `invalid_request_error`:\n\n```\n{\n  \"type\": \"error\",\n  \"error\": {\n    \"type\": \"invalid_request_error\",\n    \"message\": \"This model does not support assistant message prefill. The conversation must end with a user message.\"\n  }\n}\n```\n\nAnthropic's replacements match Google's: structured outputs, instructions in the system prompt, or\n`output_config.format`. The check from fix 1 carries over. The last message must have the role\n`user`, and in Claude's tool loop that is the message carrying the `tool_result` blocks. The\ndifference is that Claude 4.5 and earlier still accept prefill. Gemini enforces the rule on every\nmodel from 3.6 Flash on, and the reroutes move older model IDs onto it.\n\n## Test the fix before December 1\n\nThe 400 is the easy part, because it fails on the first request. The quieter problem is the request that now succeeds: a translation that gained a preamble once the prefill was gone, a JSON route whose schema now forces a field the prompt used to leave out, an agent that picks a different tool after its cancelled call reads as an error. You only see those by running the same real inputs before and after the change and diffing the outputs and tool calls.\n\nDisclosure: I'm the founder of EvalShift, which does that diff. The capture SDK wraps the\n`google-genai` client and records real agent runs, tool calls included, and `compare` replays them\nagainst another model:\n\n``` python\nfrom google import genai\nfrom evalshift import capture\nfrom evalshift.adapters.genai import wrap_genai\n\nclient = wrap_genai(genai.Client())  # each generate_content() inside a capture records one call\n\nTOOLS = [...]  # the tools the agent is offered, or [] for none\n\n@capture.agent(suite=\"support\", redact=True, tools=TOOLS)  # no-op unless EVALSHIFT_CAPTURE=1\ndef handle_ticket(message): ...\npip install \"evalshift[google-genai]\"\nEVALSHIFT_CAPTURE=1 python my_agent.py   # record production-shaped runs\nevalshift capture sync                   # recorded runs -> golden suite\nevalshift compare --suite-name support --to gemini-3.8-flash\n```\n\nRun it while `gemini-2.5-pro` or `gemini-2.5-flash` still answers as itself, before December 1, so the\nbaseline is the model you actually shipped on. Docs: [/docs/sdk-adapters](https://www.evalshift.dev/docs/sdk-adapters),\n[/docs/captures](https://www.evalshift.dev/docs/captures), [/docs/migration-policy](https://www.evalshift.dev/docs/migration-policy).\n\n## FAQ\n\n**What does \"Requests ending with a model turn are not supported\" mean?** The last non-empty entry in\nyour request's `contents` has the role `model`. Gemini 3.6 Flash, Gemini 3.5 Flash-Lite and every later\nGemini model reject that with a 400 `INVALID_ARGUMENT`, because Google no longer supports prefilling a\nmodel turn. End the request on a non-empty `user` turn: the user's text, or the function responses for\nthe model's last function calls.\n\n**Why did the error start without a code change?** Google reroutes deprecated model IDs on the Gemini\nAPI. Since October 8, 2026, requests for `gemini-3.5-flash` go to `gemini-3.6-flash` and requests for `gemini-3.7-flash`\ngo to `gemini-3.8-flash`. From December 1, 2026, the Gemini API routes `gemini-2.5-pro` to\n`gemini-3.8-flash` and `gemini-2.5-flash` to `gemini-3.5-flash-lite`. Every target rejects a request\nthat ends on a model turn.\n\n**Does adding an empty user turn fix it?** No. Google checks the last non-empty turn, and empty turns\nare dropped or fail validation. The final user turn has to contain text or function responses.\n\n**How do I force JSON output on Gemini without prefill?** Use structured output. Set\n`response_mime_type` to `application/json` and pass a JSON schema in `response_json_schema`. The API\nenforces the schema, which a prefilled `{` never did. For format rules that a schema can't express,\nuse `system_instruction`.\n\n**Does Claude support prefill?** Not from Claude 4.6 on. Claude 4.6 and later models return a 400\n`invalid_request_error` saying \"This model does not support assistant message prefill. The\nconversation must end with a user message.\" Claude 4.5 and earlier models still accept it.\n\n## Keep reading\n\n- +[Gemini model deprecation: dates, replacements and a migration checklist](https://www.evalshift.dev/blog/gemini-model-deprecation-migration-checklist) — every Gemini retirement and reroute date, and the other request changes for Gemini 3.8 Flash.\n- +[LLM drift](https://www.evalshift.dev/blog/llm-drift) — how a model can change under a model ID you never edited, the\nreroutes above included.\n- +[What actually breaks when you switch LLMs](https://www.evalshift.dev/blog/what-breaks-when-you-switch-llms) — the\nregressions that don't fail as loudly as a 400.\n- +[How to catch LLM regressions before a model upgrade ships](https://www.evalshift.dev/blog/llm-regression-testing-model-upgrade) — replaying real traffic on the old and new model, step by step.", "url": "https://wpnews.pro/news/gemini-requests-ending-with-a-model-turn-are-not-supported-the-cause-and-the-fix", "canonical_source": "https://www.evalshift.dev/blog/gemini-requests-ending-with-model-turn", "published_at": "2026-11-17 20:11:14.045648+00:00", "updated_at": "2026-11-17 20:11:16.520553+00:00", "lang": "en", "topics": ["large-language-models", "ai-agents", "developer-tools", "ai-tools"], "entities": ["Google", "Gemini API", "Gemini 3.6 Flash", "Gemini 3.5 Flash-Lite", "Gemini 3.8 Flash", "Gemini 2.5 Pro", "Vertex AI", "Anthropic"], "also_reported_by": [], "alternates": {"html": "https://wpnews.pro/news/gemini-requests-ending-with-a-model-turn-are-not-supported-the-cause-and-the-fix", "markdown": "https://wpnews.pro/news/gemini-requests-ending-with-a-model-turn-are-not-supported-the-cause-and-the-fix.md", "text": "https://wpnews.pro/news/gemini-requests-ending-with-a-model-turn-are-not-supported-the-cause-and-the-fix.txt", "jsonld": "https://wpnews.pro/news/gemini-requests-ending-with-a-model-turn-are-not-supported-the-cause-and-the-fix.jsonld"}}