Part 14 / Topic 4 of 7
Structured output
When the result feeds code, don't parse prose — ask for a validated object:
# examples/02_structured_output_get_a_validated_pydantic.py (ran against the local mock)
class Ticket(BaseModel):
category: Literal["shipping", "returns", "payment", "account", "other"]
urgent: bool
summary: str
response = client.messages.parse(model="claude-opus-5-5", max_tokens=16000,
messages=[...], output_format=Ticket)
ticket = response.parsed_output # a validated Ticket instance
# Ticket {'category': 'shipping', 'urgent': True, 'summary': 'placeholder'}
messages.parse sends the schema with the request and returns parsed_output as a real Pydantic instance — types checked, enums enforced (verified against the SDK; the field values above are the mock's placeholders). This is the backbone of the triage mini-project.
🧪 Quick quiz
- What does
client.messages.parse(..., output_format=Ticket)return inparsed_output?
Show the answer
A validated instance of the Pydantic model — verified against the SDK, the result was a Ticket instance.
Done here? The practice set for Part 14 — 4 exercises with solutions — is on the part page.