Claude Opus Prefill Not Supported Error Fix
Claude Opus 4.6 does not support prefilling assistant messages. If you send a request with a prefilled last assistant message, the API returns a 400 error. This guide shows the alternatives.
The Error
{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "Prefilling assistant messages is not supported for this model."
}
}
Quick Fix
- Remove the prefilled assistant message from your request.
- Use
output_config.formatwithjson_schemafor structured JSON output. - Use system prompt instructions to control the output format.
What Causes This
Prefilling is a technique where you add a partial assistant message at the end of the messages array to steer Claude’s response format. For example, starting the response with { to force JSON output.
Claude Opus 4.6 (and Claude Mythos Preview) do not support this technique. The API rejects the request with a 400 invalid_request_error.
Full Solution
Remove the Prefilled Message
import anthropic
client = anthropic.Anthropic()
# WRONG: Prefilled assistant message
messages = [
{"role": "user", "content": "List 3 colors as JSON"},
{"role": "assistant", "content": "{"} # Error on Opus 4.6!
]
# CORRECT: No prefill
messages = [
{"role": "user", "content": "List 3 colors as JSON"}
]
response = client.messages.create(
model="claude-opus-4-6",
max_tokens=1024,
messages=messages
)
Use Structured Outputs (Best Alternative)
The output_config.format parameter guarantees valid JSON output without prefilling:
import anthropic
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-4-6",
max_tokens=1024,
messages=[{"role": "user", "content": "List 3 colors with their hex codes"}],
output_config={
"format": {
"type": "json_schema",
"schema": {
"type": "object",
"properties": {
"colors": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {"type": "string"},
"hex": {"type": "string"}
},
"required": ["name", "hex"],
"additionalProperties": False
}
}
},
"required": ["colors"],
"additionalProperties": False
}
}
}
)
import json
data = json.loads(response.content[0].text)
print(data)
TypeScript with Structured Outputs
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic();
const response = await client.messages.create({
model: "claude-opus-4-6",
max_tokens: 1024,
messages: [{ role: "user", content: "List 3 colors with hex codes" }],
output_config: {
format: {
type: "json_schema",
schema: {
type: "object",
properties: {
colors: {
type: "array",
items: {
type: "object",
properties: {
name: { type: "string" },
hex: { type: "string" }
},
required: ["name", "hex"],
additionalProperties: false
}
}
},
required: ["colors"],
additionalProperties: false
}
}
}
});
const data = JSON.parse(
response.content[0].type === "text" ? response.content[0].text : "{}"
);
console.log(data);
Use System Prompts for Format Control
For simpler format requirements, system prompts work well without prefilling:
response = client.messages.create(
model="claude-opus-4-6",
max_tokens=1024,
system="Always respond with valid JSON. No markdown, no explanations, just the JSON object.",
messages=[{"role": "user", "content": "List 3 colors with hex codes"}]
)
Model Compatibility
Prefilling works on these models but NOT on Opus 4.6:
| Model | Prefill Supported |
|---|---|
| Claude Opus 4.6 | No |
| Claude Mythos Preview | No |
| Claude Sonnet 4.6 | Yes |
| Claude Sonnet 4.5 | Yes |
| Claude Haiku 4.5 | Yes |
| Claude Opus 4.5 | Yes |
If your code depends on prefilling and you need to use Opus 4.6, switch to structured outputs.
Prevention
- Use structured outputs by default:
output_config.formatwithjson_schemais more reliable than prefilling on ALL models and is the only option on Opus 4.6. - Check model compatibility: Before using prefilling, verify the model supports it.
- Write model-agnostic code: Use structured outputs or system prompts so your code works across all Claude models without modification.
Related Guides
- Claude API Error 400 invalid_request_error Fix – the error type returned when prefill is used on unsupported models.
- Claude Extended Thinking API Guide – Opus 4.6 capabilities beyond prefilling.
- Claude API Tool Use Function Calling Deep Dive Guide – full parameter reference including output_config.
- Claude Python SDK Getting Started – basic SDK setup.
- Claude TypeScript SDK Installation Guide – TypeScript SDK setup for structured outputs.