JSON is one of the most widely used formats in the world for applications to exchange data. Structured Outputs is a
feature that ensures the model will always generate responses that adhere to our supplied JSON Schema, so we don’t need
to worry about the model omitting a required key, or hallucinating an invalid enum value.
Without structured outputs, LLM can generate malformed JSON responses or invalid tool inputs that break our
applications. Even with careful prompting, we may encounter:
Parsing errors from invalid JSON syntax
Missing required fields
Inconsistent data types
Schema violations requiring error handling and retries
Structured outputs guarantee schema-compliant responses through constrained decoding:
Reliable type-safety: No need to validate or retry incorrectly formatted responses
Explicit refusals: Safety-based model refusals are now programmatically detectable
Simpler prompting: No need for strongly worded prompts to achieve consistent formatting
JSON outputs control Claude’s response format, ensuring Claude returns valid JSON matching our schema. Use JSON outputs
when we need to:
Control Claude’s response format
Extract data from images or text
Generate structured reports
Format API responses
1
client = anthropic.Anthropic()
2
3
response = client.messages.create(
4
model="claude-opus-5",
5
max_tokens=1024,
6
messages=[
7
{
8
"role":"user",
9
"content":"Extract the key information from this email: John Smith (john@example.com) is interested in our Enterprise plan and wants to schedule a demo for next Tuesday at 2pm.",
print(next(block.text for block in response.content if block.type =="text"))
Response format: Valid JSON matching our schema in the response’s text content block
1
{
2
"name":"John Smith",
3
"email":"john@example.com",
4
"plan_interest":"Enterprise",
5
"demo_requested":true
6
}
How it works
Define our JSON schema: Create a JSON schema that describes the structure we want Claude to follow. The schema
uses standard JSON Schema format with some limitations (see JSON Schema limitations).
Add the output_config.format parameter: Include the output_config.format parameter in our API request with
type: "json_schema" and our schema definition.
Parse the response: Claude’s response is valid JSON matching our schema, returned in the response’s text content
block.
"content":"You are a helpful math tutor. Guide the user through the solution step by step.",
7
},
8
{"role":"user","content":"how can I solve 8x + 7 = -23"},
9
],
10
text={
11
"format":{
12
"type":"json_schema",
13
"name":"math_response",
14
"schema":{
15
"type":"object",
16
"properties":{
17
"steps":{
18
"type":"array",
19
"items":{
20
"type":"object",
21
"properties":{
22
"explanation":{"type":"string"},
23
"output":{"type":"string"},
24
},
25
"required":["explanation","output"],
26
"additionalProperties":False,
27
},
28
},
29
"final_answer":{"type":"string"},
30
},
31
"required":["steps","final_answer"],
32
"additionalProperties":False,
33
},
34
"strict":True,
35
},
36
},
37
)
38
39
print(response.output_text)
NOTE
The first request we make with any schema will have additional latency as our API processes the schema, but subsequent
requests with the same schema will not have additional latency.
How it works
Define our schema: First you must design the JSON Schema that the model should be constrained to follow. While
Structured Outputs supports much of JSON Schema, some features are unavailable either for performance or technical
reasons. See here for more
details.
Supply schema in the API call: To use Structured Outputs, simply specify
Handle edge cases: In some cases, the model might not generate a valid response that matches the provided JSON
schema. This can happen in the case of a refusal, if the model refuses to answer for safety reasons, or if for
example we reach a max tokens limit and the response is incomplete.
In addition to supporting JSON Schema in the REST API, the SDKs of OpenAI, Claude, Gemini for Python and JavaScript also
make it easy to define object schemas using Pydantic and Zod
respectively.
The SDKs provide helpers that make it easier to work with JSON outputs, including schema transformation, automatic
validation, and integration with popular schema libraries.
We can see how to extract information from unstructured text that conforms to a schema defined in vendor-specific
sections below.
"content":"Extract the key information from this email: John Smith (john@example.com) is interested in our Enterprise plan and wants to schedule a demo for next Tuesday at 2pm.",