Lesson 13.3 · 3 of 6 · 5 min read · Reviewed 24 Nov 2026
Prefilling the assistant turn
By the end, you can
- Explain what prefilling the assistant turn does and where it still works.
- Choose the right replacement for prefill on current Claude models.
In the Messages APIMessages API: The chat API endpoint that takes a list of messages and returns the next assistant message., the last message can be a partial assistant message. Claude continues from exactly where it stops. That’s prefillingPrefilling: Writing the first part of the assistant’s reply yourself so the model continues from there..
"messages": [
{"role": "user", "content": "Extract the name and email from: ..."},
{"role": "assistant", "content": "{"}
]
The reply carries on from {, so there’s no “Sure! Here’s the JSON:” before it. Remember to add the { back when you parse.
Other uses
- Prefill
<answer>to skip straight to the answer. - Prefill the first words of a reply in a persona to keep the voice steady.
Limits
- Current Claude models reject prefill. Ending
messageswith an assistant turn returns an error on current models; check your model’s migration guide for when this changed. It still works on some older models and on many other model APIs, so you’ll meet it in existing code. - On current Claude models, use a system-prompt instruction (“Reply with only the JSON object”) or structured outputsStructured outputs: Making a model return data that matches a defined schema, such as JSON with required fields and types. instead.
- It shapes the start; it doesn’t guarantee a valid result.
When you need guaranteed structure, use the stronger tools in chapter 14: structured outputs with a JSON schema, or a tool whose input_schema is the shape you want.
Key points
- Prefilling means putting the first words of the assistant's reply in the request, and the model continues from there.
- On models that accept it, prefilling `{` pushes output straight into JSON and prefilling a tag can skip a preamble.
- Current Claude models reject prefill. Use structured outputs for JSON and a system instruction for format. Prefill still appears in older code, open-weight runtimes and other model APIs.
Replace prefill with a system instruction
Detect requests that rely on prefilling and rewrite them for models that reject a trailing assistant turn.
You will have: A has_prefill check, a rewrite function and a parser that handles both old prefilled replies and new full replies.
Steps
- Read the old request in the starter: it ends with an assistant turn holding '{'.
- Complete has_prefill to spot a messages list whose last message is from the assistant.
- Complete rewrite to drop the trailing assistant turn and add a system instruction asking for only the JSON object.
- Complete parse so it adds the '{' back for an old prefilled reply and parses a full reply as is.
Done when
- has_prefill is True for the old request and False for the rewritten one.
- The rewritten request has a system instruction and ends with a user message.
- Both sample replies parse to the same dict, and the script prints 'same result: True'.
Open in the Playground No API key needed.
Starter code (Python)
import json
OLD = {
"messages": [
{"role": "user", "content": "Extract name and email as JSON from: Priya Shah, priya@example.com"},
{"role": "assistant", "content": "{"},
]
}
def has_prefill(request):
# TODO: True if the last message has role "assistant".
return False
def rewrite(request):
# TODO: copy the request, drop a trailing assistant turn, and add
# "system": "Reply with only the JSON object. No other text."
return request
def parse(reply_text, prefilled):
# TODO: if prefilled, the reply continues after "{", so add it back before json.loads.
return {}
new = rewrite(OLD)
print("old uses prefill:", has_prefill(OLD))
print("new uses prefill:", has_prefill(new))
print("new system:", new.get("system"))
a = parse('"name": "Priya Shah", "email": "priya@example.com"}', prefilled=True)
b = parse('{"name": "Priya Shah", "email": "priya@example.com"}', prefilled=False)
print("same result:", a == b and a != {})
Show a solution
import json, copy
OLD = {
"messages": [
{"role": "user", "content": "Extract name and email as JSON from: Priya Shah, priya@example.com"},
{"role": "assistant", "content": "{"},
]
}
def has_prefill(request):
msgs = request.get("messages", [])
return bool(msgs) and msgs[-1]["role"] == "assistant"
def rewrite(request):
new = copy.deepcopy(request)
if has_prefill(new):
new["messages"].pop()
new["system"] = "Reply with only the JSON object. No other text."
return new
def parse(reply_text, prefilled):
text = "{" + reply_text if prefilled else reply_text
return json.loads(text)
new = rewrite(OLD)
print("old uses prefill:", has_prefill(OLD))
print("new uses prefill:", has_prefill(new))
print("new system:", new.get("system"))
a = parse('"name": "Priya Shah", "email": "priya@example.com"}', prefilled=True)
b = parse('{"name": "Priya Shah", "email": "priya@example.com"}', prefilled=False)
print("same result:", a == b and a != {})