你的 messages 陣列以 assistant 回合結束,而 Claude 4.6 及後續模型會將其視為預填並拒絕。請以 user 訊息結束對話,然後將預填原本執行的功能——強制輸出 JSON、跳過前言、維持角色設定、繼續被截斷的回答——改用 Anthropic 文件針對各用途提供的替代方案。
錯誤
{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "This model does not support assistant message prefill. The conversation must end with a user message."
},
"request_id": "req_..."
}原因與解決方法一覽
| 原因 | 解決方法 |
|---|---|
| messages 中的最後一筆項目具有 role: "assistant" | 這就是預填,Claude 4.6 及後續模型會拒絕。請以 user 訊息結束。 |
| 某個框架將空的 assistant 訊息留在最後 | 傳送前,移除末尾沒有內容的 assistant 訊息。 |
| 你預填了「{」以強制輸出 JSON | 改用結構化輸出(output_config.format)。 |
| 你使用預填來跳過前言、維持角色設定或繼續被截斷的回答 | 改用 system prompt 指示、system prompt 中的角色設定,或 user 回合的延續訊息。 |
| 記憶體管理器、代理迴圈或交接流程將 assistant 回合留在最後 | 就在請求送出前統一正規化訊息尾端,而不是在每條程式路徑中重複處理。 |
哪些 Claude 模型拒絕預填(截至 2026 年 9 月)
Anthropic 的錯誤參考文件說得很直接:Claude 4.6 及後續模型不支援預填最後一則 assistant 訊息,提出這類請求會得到完全相同的 400 錯誤(https://platform.claude.com/docs/en/api/errors#prefill-not-supported)。具體而言,包括 Claude Opus 4.6 及所有後續 Opus(包括 Claude Opus 5.5)(https://platform.claude.com/docs/en/models/opus-5-5/migration-guide);Claude Sonnet 4.6 與 Claude Sonnet 5(https://platform.claude.com/docs/en/models/sonnet-5/migration-guide);以及 Claude Fable 5 和 Fable 5.1(https://platform.claude.com/docs/en/models/fable-5-1/migration-guide)。Claude Haiku 4.5 仍接受預填,Claude Sonnet 4.5 和 Claude Opus 4.5 也一樣——因此這個錯誤通常是隨著模型 ID 變更而出現,而不是程式碼變更。對話中較早的 assistant 訊息(包括 few-shot 範例)不受影響。
以 user 回合結束——包括一行修正
很多時候,沒有人刻意寫入預填:技術堆疊中的某個元件將 assistant 訊息放在最後。公開報告將問題追溯到會附加一則 assistant 訊息的記憶管理器(Strands issue #1694)、在錯誤認為回合尚未完成後重新請求的代理迴圈(opencode issue #46415)、連續回覆與代理交接(LiveKit issue #4907),以及留在末尾的空 assistant 訊息(AutoGen PR #7931)。如果尾端訊息是空的,請移除它——這就是一行修正。如果其中包含你想繼續的文字,請依 Anthropic 遷移指引中關於續寫的說明:將續寫請求放在 user 訊息中,並引用回答停止的位置。有一個已記錄的例外應維持原樣:伺服器工具(例如網頁搜尋)傳回的 pause_turn,要透過將暫停的 assistant 內容原樣送回來繼續(https://platform.claude.com/docs/en/agents-and-tools/tool-use/server-tools)。
from openai import OpenAI
client = OpenAI(base_url="https://api.kunavo.com/v1", api_key="sk-kn-...")
def end_on_user(messages: list[dict]) -> list[dict]:
"""Claude 4.6 and later reject a conversation whose last turn is the assistant's."""
last = messages[-1] if messages else None
if not last or last["role"] != "assistant" or last.get("tool_calls"):
return messages # tool_calls are owed tool results instead
content = last.get("content")
if not content or (isinstance(content, str) and not content.strip()):
return messages[:-1] # the one line: drop an empty tail
tail = content[-200:] if isinstance(content, str) else "..."
return messages + [{
"role": "user",
"content": f"Your previous response was interrupted and ended with {tail!r}. "
"Continue from where you left off.",
}]
messages = [
{"role": "user", "content": "Explain HTTP caching in three bullets."},
{"role": "assistant", "content": ""}, # the empty tail a framework left behind
]
resp = client.chat.completions.create(
model="claude-sonnet-4-6",
max_tokens=1024,
messages=end_on_user(messages),
)
print(resp.choices[0].message.content)替換預填原本執行的功能
Anthropic 的提示工程指南將預填的各種舊用途對應到替代方案(https://platform.claude.com/docs/en/build-with-claude/prompt-engineering/claude-prompting-best-practices#migrating-away-from-prefilled-responses)。強制輸出 JSON:使用結構化輸出,將回應限制在你的結構描述中;Claude Haiku 4.5、Sonnet 4.5、Opus 4.5 及其後所有模型都支援(https://platform.claude.com/docs/en/build-with-claude/structured-outputs)。對於 YAML 或其他格式,指南建議要求指定結構,若未符合則重試。分類:使用包含有效標籤列舉的工具,或使用結構化輸出。跳過前言:使用 system prompt 指示,例如「直接回答,不要加入前言。」維持角色設定:在 system prompt 中設定角色(https://platform.claude.com/docs/en/build-with-claude/prompt-engineering/claude-prompting-best-practices#give-claude-a-role),並在 user 回合中定期提醒,而不是使用預填 assistant 訊息。被截斷的回答:使用上述 user 回合延續方式。強制工具呼叫並非所有情況下都能直接替代——Claude Fable 5.1 和 Claude Opus 5.5 會拒絕 tool_choice any 和 tool,並各自回傳 400 錯誤(https://platform.claude.com/docs/en/api/errors#forced-tool-use-not-supported)。
# Before: messages ended with {"role": "assistant", "content": "{"}
# Kunavo forwards output_config as sent; the reply is constrained to the schema.
curl https://api.kunavo.com/v1/messages \
-H "x-api-key: sk-kn-..." \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "claude-sonnet-4-6",
"max_tokens": 1024,
"messages": [
{"role": "user", "content": "Extract the name and email: John Smith <john@example.com>"}
],
"output_config": {
"format": {
"type": "json_schema",
"schema": {
"type": "object",
"properties": {
"name": {"type": "string"},
"email": {"type": "string"}
},
"required": ["name", "email"],
"additionalProperties": false
}
}
}
}'如果你透過 Kunavo 呼叫
Kunavo 會轉送末尾的 assistant 回合,不會修復它。/v1/messages 會將你的 messages 陣列原封不動地交給上游;在 /v1/chat/completions 中,轉譯器會將最後一則 role: "assistant" 訊息帶入 Claude 請求,作為最後的 assistant 回合——包括空訊息,因此請自行移除空訊息。我們提供的所有 claude-* 模型(claude-haiku-4-5 除外)都會拒絕預填的最後一回合,而該 400 錯誤會連同原始訊息傳回給你,後面附有上游 request id:/v1/messages 使用 Anthropic 格式,錯誤類型為 Anthropic API 所用的 invalid_request_error(2026 年 9 月 24 日以前為 api_error);/v1/chat/completions 使用 OpenAI 格式(type upstream_error,code upstream_400)。400 錯誤不會在其他通道上重試,失敗的呼叫會以零成本記錄。若要輸出 JSON,請傳送結構描述,而不是預填:/v1/messages 會照你傳送的方式轉送 output_config;在 /v1/chat/completions 中,type 為 json_schema 的 response_format 會轉譯為 output_config.format。2026-09-24 當日,對我們提供的所有 claude-* 模型,該欄位都會將回應限制為符合所傳送的結構描述。json_object response_format 沒有對應的 Claude 功能,因此不會強制執行;而結構描述與預填的最後一回合一同傳送時,即使是 claude-haiku-4-5 也會產生另一個 400 錯誤:「使用輸出格式時,不支援預填 `assistant` 回應。」 原生端點及其轉送的請求格式記載於 Messages API 文件.
常見問題
哪些 Claude 模型不支援 assistant 訊息預填?
根據 Anthropic 截至 2026 年 9 月的文件,所有從 4.6 起的 Claude 模型都不支援:Claude Opus 4.6 及所有後續 Opus、Claude Sonnet 4.6 與 Sonnet 5、Claude Fable 5 和 5.1,以及 Mythos 模型。Claude Haiku 4.5、Sonnet 4.5 和 Opus 4.5 仍接受預填。
如何在不使用預填的情況下強制 Claude 輸出 JSON?
使用結構化輸出:在 output_config.format 中傳入 JSON 結構描述,回應會受到該結構描述限制。透過 OpenAI 相容端點時,請以 type 為 json_schema 的 response_format 傳送;Kunavo 會將其轉譯為 output_config.format。Anthropic 指出,無論如何,訊息預填都與 JSON 輸出不相容。
我從未使用預填,為什麼仍會收到這個錯誤?
你的堆疊中有某個元件將 assistant 訊息留在最後——可能是記憶體或工作階段管理器、在某個回合後重新請求的代理迴圈、代理交接,或沒有人移除的空 assistant 訊息。請逐字記錄實際送出的 messages 陣列;其最後一個元素會具有 role: "assistant"。
我仍然可以在對話中加入 assistant 訊息嗎?
可以。只有最後一個回合受到限制;較早的 assistant 訊息(包括 few-shot 範例)不受影響。
為什麼透過 OpenAI 相容端點也會發生?
因為閘道會將你最後一則 role: "assistant" 訊息帶入 Claude,作為最後的 Claude assistant 回合——也就是重新塑形後的相同預填。Kunavo 的聊天轉譯器正是如此運作,其他 OpenAI 相容閘道也曾回報相同訊息(opencode issue #13768)。修正方式相同:以 user 訊息結束。