CONCEPT The opening character decides the format
An LLM continues the most probable path from where the text stops. If the transcript already ends in
{, the only sensible continuation is more JSON — Claude will not backtrack to write
"Here is the JSON you asked for:". That is the whole trick behind format-control prefills:
seed the first character(s) of the shape you want, and the rest follows.
| Want | Prefill | Why it works |
|---|---|---|
| One JSON object, no prose | { | Continuation must complete a JSON object |
| A JSON array | [ | Continuation must complete a JSON array |
| Content inside a tag | <summary> | Response opens inside your XML structure |
| A code block only | ```python | Response starts as fenced code, no explanation |
{ to force straight-into-JSON. It
kills the preamble ("Sure! Here's…") that otherwise breaks a downstream json.loads().CONCEPT Prefill a tag to enforce structure
Prefilling an opening XML tag such as <analysis> forces the response to begin inside a
named section. Combined with a stop sequence on the closing tag (6.4), you extract exactly one clean block —
no wrapper text, no trailing commentary. This is a reliable way to carve a specific field out of a longer
reasoning turn.
DIAGRAM BAD (no prefill) vs. GOOD (prefill {)
{"name": "Ada", "id": 7}'"] PRE --> STRIP["Must regex/strip the preamble
before json.loads()"] B -->|"prefill '{'"| CLEAN["🟢 continues: '"name": "Ada", "id": 7}'"] CLEAN --> PARSE["Prepend '{' → parse directly"] classDef ok fill:#d4edda,stroke:#28a745,color:#155724; classDef bad fill:#f8d7da,stroke:#dc3545,color:#721c24; classDef gate fill:#ffe8cc,stroke:#ff9f40,color:#9a4a0a; class CLEAN ok; class PARSE ok; class PRE bad; class STRIP gate;
CODE Force JSON with {, then reconstruct
import anthropic, json client = anthropic.Anthropic() resp = client.messages.create( model="claude-opus-4-5", max_tokens=300, messages=[ {"role": "user", "content": "Return {name, id} for user Ada as a single JSON object."}, {"role": "assistant", "content": "{"}, # ← force straight into JSON ], ) # content holds ONLY the continuation: '"name": "Ada", "id": 7}' data = json.loads("{" + resp.content[0].text) # prepend prefill, then parse print(data["name"]) # Ada
output_config.format with a JSON schema) for the same guarantee. The prefill technique itself
is examinable, and still valid on earlier models. See 6.5.TRADE-OFFS Prefill { vs. Structured Outputs
Prefill { | Structured Outputs | |
|---|---|---|
| Guarantee | Opening is {; body still free-form | Whole response validates against a schema |
| Model support | Earlier models only (400 on 4.6+) | Current models |
| Parsing | You prepend the prefill by hand | SDK can parse into a typed object |
| Best for | Quick format nudge, skip preamble | Strict, schema-validated JSON |
{; on current models reach for
Structured Outputs. Both answer "give me clean JSON, no chatter."EXAM TRAPS
{ guarantees valid JSON." → It guarantees the response
starts as JSON and skips preamble. The body can still be malformed or run past max_tokens —
for a hard schema guarantee use Structured Outputs.content[0].text begins
after your {. Prepend it, or json.loads() raises.[, an XML tag,
a code fence, a first word. Prefill controls whatever shape you seed.<summary> but keeps going; pair the tag prefill with a stop sequence on the closing
tag (6.4) to get exactly one block.PRACTICE
Q1 · Why does prefilling { remove the "Here is the JSON:" preamble?
The transcript already ends in {, so the most probable continuation is more
JSON, not a fresh sentence of prose. Claude can only continue forward from the seeded character — it cannot
prepend a preamble before text that already exists.
Q2 · What is the modern replacement for a "force JSON" prefill on new models?
Structured Outputs — output_config.format with a JSON schema. It
constrains the entire response to the schema (not just the opening character) and is supported on current
models where last-turn prefill returns a 400.
Q3 · How do you extract exactly one <analysis>…</analysis> block?
Prefill the opening tag <analysis> so the response starts inside it, and
set a stop sequence of </analysis> so generation ends at the close (6.4). Prefill
bounds the start; the stop sequence bounds the end.