Structured Outputs - OpenAI API
Structured Outputs - OpenAI API
Structured Outputs
Ensure responses adhere to a JSON schema.
Try it out
Try it out in the Playground or generate a ready-to-use schema definition to experiment with
structured outputs.
Generate
Introduction
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 your supplied JSON Schema, so you don't need to worry about the model omitting a
required key, or hallucinating an invalid enum value.
In addition to supporting JSON Schema in the REST API, the OpenAI SDKs for Python and
JavaScript also make it easy to define object schemas using Pydantic and Zod respectively.
Below, you can see how to extract information from unstructured text that conforms to a
schema defined in code.
https://platform.openai.com/docs/guides/structured-outputs?api-mode=chat&example=chain-of-thought&type-restrictions=str… 1/24
26/07/2025, 08:12 Structured Outputs - OpenAI API
5
6 class CalendarEvent(BaseModel):
7 name: str
8 date: str
9 participants: list[str]
10
11 completion = client.chat.completions.parse(
12 model="gpt-4o-2024-08-06",
13 messages=[
14 {"role": "system", "content": "Extract the event information."},
15 {"role": "user", "content": "Alice and Bob are going to a science fa
16 ],
17 response_format=CalendarEvent,
18 )
19
20 event = completion.choices[0].message.parsed
Supported models
Structured Outputs is available in our latest large language models, starting with GPT-4o.
Older models like gpt-4-turbo and earlier may use JSON mode instead.
Function calling is useful when you are building an application that bridges the models and
functionality of your application.
For example, you can give the model access to functions that query a database in order to
build an AI assistant that can help users with their orders, or functions that can interact with
the UI.
Conversely, Structured Outputs via response_format are more suitable when you want to
indicate a structured schema for use when the model responds to the user, rather than when
the model calls a tool.
For example, if you are building a math tutoring application, you might want the assistant to
respond to your user using a specific JSON Schema so that you can generate a UI that displays
different parts of the model's output in distinct ways.
https://platform.openai.com/docs/guides/structured-outputs?api-mode=chat&example=chain-of-thought&type-restrictions=str… 2/24
26/07/2025, 08:12 Structured Outputs - OpenAI API
Put simply:
If you are connecting the model to tools, functions, data, etc. in your system, then you
should use function calling
If you want to structure the model's output when it responds to the user, then you should
use a structured response_format
The remainder of this guide will focus on non-function calling use cases in the Chat Completions
API. To learn more about how to use Structured Outputs with function calling, check out the
Function Calling guide.
Structured Outputs is the evolution of JSON mode. While both ensure valid JSON is produced,
only Structured Outputs ensure schema adherance. Both Structured Outputs and JSON mode
are supported in the Responses API,Chat Completions API, Assistants API, Fine-tuning API
and Batch API.
We recommend always using Structured Outputs instead of JSON mode when possible.
Examples
Chain of thought
https://platform.openai.com/docs/guides/structured-outputs?api-mode=chat&example=chain-of-thought&type-restrictions=str… 3/24
26/07/2025, 08:12 Structured Outputs - OpenAI API
You can ask the model to output an answer in a structured, step-by-step way, to guide the user
through the solution.
Example response
1 {
2 "steps": [
3 {
4 "explanation": "Start with the equation 8x + 7 = -23.",
5 "output": "8x + 7 = -23"
6 },
7 {
8 "explanation": "Subtract 7 from both sides to isolate the term with th
9 "output": "8x = -23 - 7"
10 },
11 {
12 "explanation": "Simplify the right side of the equation.",
13 "output": "8x = -30"
14 },
15 {
16 "explanation": "Divide both sides by 8 to solve for x.",
17 "output": "x = -30 / 8"
https://platform.openai.com/docs/guides/structured-outputs?api-mode=chat&example=chain-of-thought&type-restrictions=str… 4/24
26/07/2025, 08:12 Structured Outputs - OpenAI API
18 },
19 {
20 "explanation": "Simplify the fraction.",
21 "output": "x = -15 / 4"
22 }
23 ],
24 "final_answer": "x = -15 / 4"
25 }
Note: for fine tuned models, the first request you 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. Other models do not have this limitation.
First you must define an object or data structure to represent the JSON Schema that the
model should be constrained to follow. See the examples at the top of this guide for reference.
While Structured Outputs supports much of JSON Schema, some features are unavailable
either for performance or technical reasons. See here for more details.
python
https://platform.openai.com/docs/guides/structured-outputs?api-mode=chat&example=chain-of-thought&type-restrictions=str… 5/24
26/07/2025, 08:12 Structured Outputs - OpenAI API
Create clear titles and descriptions for important keys in your structure
Create and use evals to determine the structure that works best for your use case
You can use the parse method to automatically parse the JSON response into the object you
defined.
Under the hood, the SDK takes care of supplying the JSON schema corresponding to your data
structure, and then parsing the response as an object.
python
1 completion = client.chat.completions.parse(
2 model="gpt-4o-2024-08-06",
3 messages=[
4 {"role": "system", "content": "You are a helpful math tutor. Guide th
5 {"role": "user", "content": "how can I solve 8x + 7 = -23"}
6 ],
7 response_format=MathResponse
8 )
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 you reach a max tokens limit and the response is incomplete.
python
1 try:
2 response = client.chat.completions.create(
3 model="gpt-4o-2024-08-06",
4 messages=[
5 {
6 "role": "system",
7 "content": "You are a helpful math tutor. Guide the user thr
8 },
https://platform.openai.com/docs/guides/structured-outputs?api-mode=chat&example=chain-of-thought&type-restrictions=str… 6/24
26/07/2025, 08:12 Structured Outputs - OpenAI API
When using Structured Outputs with user-generated input, OpenAI models may occasionally
refuse to fulfill the request for safety reasons. Since a refusal does not necessarily follow the
schema you have supplied in response_format , the API response will include a new field
called refusal to indicate that the model refused to fulfill the request.
When the refusal property appears in your output object, you might present the refusal in
your UI, or include conditional logic in code that consumes the response to handle the case of
a refused request.
https://platform.openai.com/docs/guides/structured-outputs?api-mode=chat&example=chain-of-thought&type-restrictions=str… 7/24
26/07/2025, 08:12 Structured Outputs - OpenAI API
python
1 class Step(BaseModel):
2 explanation: str
3 output: str
4
5 class MathReasoning(BaseModel):
6 steps: list[Step]
7 final_answer: str
8
9 completion = client.chat.completions.parse(
10 model="gpt-4o-2024-08-06",
11 messages=[
12 {"role": "system", "content": "You are a helpful math tutor. Guide t
13 {"role": "user", "content": "how can I solve 8x + 7 = -23"}
14 ],
15 response_format=MathReasoning,
16 )
17
18 math_reasoning = completion.choices[0].message
19
20 # If the model refuses to respond, you will get a refusal message
21 if (math_reasoning.refusal):
22 print(math_reasoning.refusal)
23 else:
24 print(math_reasoning.parsed)
The API response from a refusal will look something like this:
json
1 {
2 "id": "chatcmpl-9nYAG9LPNonX8DAyrkwYfemr3C8HC",
3 "object": "chat.completion",
4 "created": 1721596428,
5 "model": "gpt-4o-2024-08-06",
6 "choices": [
7 {
8 "index": 0,
9 "message": {
10 "role": "assistant",
11 "refusal": "I'm sorry, I cannot assist with that request."
12 },
13 "logprobs": null,
14 "finish_reason": "stop"
15 }
16 ],
17 "usage": {
18 "prompt_tokens": 81,
19 "completion_tokens": 11,
https://platform.openai.com/docs/guides/structured-outputs?api-mode=chat&example=chain-of-thought&type-restrictions=str… 8/24
26/07/2025, 08:12 Structured Outputs - OpenAI API
20 "total_tokens": 92,
21 "completion_tokens_details": {
22 "reasoning_tokens": 0,
23 "accepted_prediction_tokens": 0,
24 "rejected_prediction_tokens": 0
25 }
26 },
27 "system_fingerprint": "fp_3407719c7f"
28 }
If your application is using user-generated input, make sure your prompt includes instructions
on how to handle situations where the input cannot result in a valid response.
The model will always try to adhere to the provided schema, which can result in hallucinations
if the input is completely unrelated to the schema.
You could include language in your prompt to specify that you want to return empty
parameters, or a specific sentence, if the model detects that the input is incompatible with the
task.
Handling mistakes
Structured Outputs can still contain mistakes. If you see mistakes, try adjusting your
instructions, providing examples in the system instructions, or splitting tasks into simpler
subtasks. Refer to the prompt engineering guide for more guidance on how to tweak your
inputs.
To prevent your JSON Schema and corresponding types in your programming language from
diverging, we strongly recommend using the native Pydantic/zod sdk support.
If you prefer to specify the JSON schema directly, you could add CI rules that flag when either
the JSON schema or underlying data objects are edited, or add a CI step that auto-generates
the JSON Schema from type definitions (or vice-versa).
Streaming
https://platform.openai.com/docs/guides/structured-outputs?api-mode=chat&example=chain-of-thought&type-restrictions=str… 9/24
26/07/2025, 08:12 Structured Outputs - OpenAI API
You can use streaming to process model responses or function call arguments as they are
being generated, and parse them as structured data.
That way, you don't have to wait for the entire response to complete before handling it. This is
particularly useful if you would like to display JSON fields one by one, or handle function call
arguments as soon as they are available.
You can find an example of how to stream function call arguments without the SDK stream
helper in the function calling guide.
Here is how you can stream a model response with the stream helper:
python
final_completion = stream.get_final_completion()
print("Final completion:", final_completion)
You can also use the stream helper to parse function call arguments:
python
Supported schemas
Structured Outputs supports a subset of the JSON Schema language.
Supported types
String
Number
https://platform.openai.com/docs/guides/structured-outputs?api-mode=chat&example=chain-of-thought&type-restrictions=s… 11/24
26/07/2025, 08:12 Structured Outputs - OpenAI API
Boolean
Integer
Object
Array
Enum
anyOf
Supported properties
In addition to specifying the type of a property, you can specify a selection of additional
constraints:
date-time
time
date
duration
hostname
ipv4
ipv6
uuid
Here are some examples on how you can use these type restrictions:
json
1 {
2 "name": "user_data",
3 "strict": true,
4 "schema": {
5 "type": "object",
6 "properties": {
7 "name": {
8 "type": "string",
9 "description": "The name of the user"
10 },
11 "username": {
12 "type": "string",
13 "description": "The username of the user. Must start with @"
14 "pattern": "^@[a-zA-Z0-9_]+$"
15 },
16 "email": {
17 "type": "string",
18 "description": "The email of the user",
19 "format": "email"
20 }
21 },
22 "additionalProperties": false,
23 "required": [
24 "name", "username", "email"
25 ]
26 }
27 }
Note these constraints are not yet supported for fine-tuned models.
Note that the root level object of a schema must be an object, and not use anyOf . A pattern
that appears in Zod (as one example) is using a discriminated union, which produces an
anyOf at the top level. So code such as the following won't work:
javascript
https://platform.openai.com/docs/guides/structured-outputs?api-mode=chat&example=chain-of-thought&type-restrictions=s… 13/24
26/07/2025, 08:12 Structured Outputs - OpenAI API
To use Structured Outputs, all fields or function parameters must be specified as required .
json
1 {
2 "name": "get_weather",
3 "description": "Fetches the weather in the given location",
4 "strict": true,
5 "parameters": {
6 "type": "object",
7 "properties": {
8 "location": {
9 "type": "string",
10 "description": "The location to get the weather for"
11 },
12 "unit": {
13 "type": "string",
14 "description": "The unit to return the temperature in",
15 "enum": ["F", "C"]
16 }
17 },
18 "additionalProperties": false,
19 "required": ["location", "unit"]
20 }
21 }
Although all fields must be required (and the model will return a value for each parameter), it is
possible to emulate an optional parameter by using a union type with null .
json
https://platform.openai.com/docs/guides/structured-outputs?api-mode=chat&example=chain-of-thought&type-restrictions=s… 14/24
26/07/2025, 08:12 Structured Outputs - OpenAI API
1 {
2 "name": "get_weather",
3 "description": "Fetches the weather in the given location",
4 "strict": true,
5 "parameters": {
6 "type": "object",
7 "properties": {
8 "location": {
9 "type": "string",
10 "description": "The location to get the weather for"
11 },
12 "unit": {
13 "type": ["string", "null"],
14 "description": "The unit to return the temperature in",
15 "enum": ["F", "C"]
16 }
17 },
18 "additionalProperties": false,
19 "required": [
20 "location", "unit"
21 ]
22 }
23 }
A schema may have up to 5000 object properties total, with up to 5 levels of nesting.
In a schema, total string length of all property names, definition names, enum values, and
const values cannot exceed 120,000 characters.
A schema may have up to 1000 enum values across all enum properties.
For a single enum property with string values, the total string length of all enum values cannot
exceed 15,000 characters when there are more than 250 enum values.
https://platform.openai.com/docs/guides/structured-outputs?api-mode=chat&example=chain-of-thought&type-restrictions=s… 15/24
26/07/2025, 08:12 Structured Outputs - OpenAI API
Structured Outputs only supports generating specified keys / values, so we require developers
to set additionalProperties: false to opt into Structured Outputs.
json
1 {
2 "name": "get_weather",
3 "description": "Fetches the weather in the given location",
4 "strict": true,
5 "schema": {
6 "type": "object",
7 "properties": {
8 "location": {
9 "type": "string",
10 "description": "The location to get the weather for"
11 },
12 "unit": {
13 "type": "string",
14 "description": "The unit to return the temperature in",
15 "enum": ["F", "C"]
16 }
17 },
18 "additionalProperties": false,
19 "required": [
20 "location", "unit"
21 ]
22 }
23 }
Key ordering
When using Structured Outputs, outputs will be produced in the same order as the ordering of
keys in the schema.
https://platform.openai.com/docs/guides/structured-outputs?api-mode=chat&example=chain-of-thought&type-restrictions=s… 16/24
26/07/2025, 08:12 Structured Outputs - OpenAI API
If you turn on Structured Outputs by supplying strict: true and call the API with an
unsupported JSON Schema, you will receive an error.
For anyOf, the nested schemas must each be a valid JSON Schema per this subset
json
1 {
2 "type": "object",
3 "properties": {
4 "item": {
5 "anyOf": [
6 {
7 "type": "object",
8 "description": "The user object to insert into the datab
9 "properties": {
10 "name": {
11 "type": "string",
12 "description": "The name of the user"
13 },
14 "age": {
15 "type": "number",
16 "description": "The age of the user"
17 }
18 },
19 "additionalProperties": false,
20 "required": [
21 "name",
22 "age"
23 ]
24 },
25 {
26 "type": "object",
27 "description": "The address object to insert into the da
28 "properties": {
29 "number": {
30 "type": "string",
31 "description": "The number of the address. Eg. f
32 },
33 "street": {
34 "type": "string",
35 "description": "The street name. Eg. for 123 mai
36 },
37 "city": {
38 "type": "string",
39 "description": "The city of the address"
40 }
41 },
https://platform.openai.com/docs/guides/structured-outputs?api-mode=chat&example=chain-of-thought&type-restrictions=s… 17/24
26/07/2025, 08:12 Structured Outputs - OpenAI API
42 "additionalProperties": false,
43 "required": [
44 "number",
45 "street",
46 "city"
47 ]
48 }
49 ]
50 }
51 },
52 "additionalProperties": false,
53 "required": [
54 "item"
55 ]
56 }
You can use definitions to define subschemas which are referenced throughout your schema.
The following is a simple example.
json
1 {
2 "type": "object",
3 "properties": {
4 "steps": {
5 "type": "array",
6 "items": {
7 "$ref": "#/$defs/step"
8 }
9 },
10 "final_answer": {
11 "type": "string"
12 }
13 },
14 "$defs": {
15 "step": {
16 "type": "object",
17 "properties": {
18 "explanation": {
19 "type": "string"
20 },
21 "output": {
22 "type": "string"
23 }
24 },
25 "required": [
26 "explanation",
https://platform.openai.com/docs/guides/structured-outputs?api-mode=chat&example=chain-of-thought&type-restrictions=s… 18/24
26/07/2025, 08:12 Structured Outputs - OpenAI API
27 "output"
28 ],
29 "additionalProperties": false
30 }
31 },
32 "required": [
33 "steps",
34 "final_answer"
35 ],
36 "additionalProperties": false
37 }
json
1 {
2 "name": "ui",
3 "description": "Dynamically generated UI",
4 "strict": true,
5 "schema": {
6 "type": "object",
7 "properties": {
8 "type": {
9 "type": "string",
10 "description": "The type of the UI component",
11 "enum": ["div", "button", "header", "section", "field", "for
12 },
13 "label": {
14 "type": "string",
15 "description": "The label of the UI component, used for butt
16 },
17 "children": {
18 "type": "array",
19 "description": "Nested UI components",
20 "items": {
21 "$ref": "#"
22 }
23 },
24 "attributes": {
25 "type": "array",
26 "description": "Arbitrary attributes for the UI component, s
27 "items": {
28 "type": "object",
29 "properties": {
30 "name": {
31 "type": "string",
32 "description": "The name of the attribute, for e
https://platform.openai.com/docs/guides/structured-outputs?api-mode=chat&example=chain-of-thought&type-restrictions=s… 19/24
26/07/2025, 08:12 Structured Outputs - OpenAI API
33 },
34 "value": {
35 "type": "string",
36 "description": "The value of the attribute"
37 }
38 },
39 "additionalProperties": false,
40 "required": ["name", "value"]
41 }
42 }
43 },
44 "required": ["type", "label", "children", "attributes"],
45 "additionalProperties": false
46 }
47 }
json
1 {
2 "type": "object",
3 "properties": {
4 "linked_list": {
5 "$ref": "#/$defs/linked_list_node"
6 }
7 },
8 "$defs": {
9 "linked_list_node": {
10 "type": "object",
11 "properties": {
12 "value": {
13 "type": "number"
14 },
15 "next": {
16 "anyOf": [
17 {
18 "$ref": "#/$defs/linked_list_node"
19 },
20 {
21 "type": "null"
22 }
23 ]
24 }
25 },
26 "additionalProperties": false,
27 "required": [
28 "next",
29 "value"
30 ]
https://platform.openai.com/docs/guides/structured-outputs?api-mode=chat&example=chain-of-thought&type-restrictions=s… 20/24
26/07/2025, 08:12 Structured Outputs - OpenAI API
31 }
32 },
33 "additionalProperties": false,
34 "required": [
35 "linked_list"
36 ]
37 }
JSON mode
JSON mode is a more basic version of the Structured Outputs feature. While JSON mode ensures
that model output is valid JSON, Structured Outputs reliably matches the model's output to
the schema you specify. We recommend you use Structured Outputs if it is supported for your
use case.
When JSON mode is turned on, the model's output is ensured to be valid JSON, except for in
some edge cases that you should detect and handle appropriately.
To turn on JSON mode with the Chat Completions or Assistants API you can set the
response_format to { "type": "json_object" } . If you are using function calling, JSON
mode is always turned on.
Important notes:
When using JSON mode, you must always instruct the model to produce JSON via some
message in the conversation, for example via your system message. If you don't include
an explicit instruction to generate JSON, the model may generate an unending stream of
whitespace and the request may run continually until it reaches the token limit. To help
ensure you don't forget, the API will throw an error if the string "JSON" does not appear
somewhere in the context.
JSON mode will not guarantee the output matches any specific schema, only that it is
valid and parses without errors. You should use Structured Outputs to ensure it matches
your schema, or if that is not possible, you should use a validation library and potentially
retries to ensure that the output matches your desired schema.
Your application must detect and handle the edge cases that can result in the model
output not being a complete JSON object (see below)
Resources
https://platform.openai.com/docs/guides/structured-outputs?api-mode=chat&example=chain-of-thought&type-restrictions=s… 21/24
26/07/2025, 08:12 Structured Outputs - OpenAI API
To learn more about Structured Outputs, we recommend browsing the following resources:
https://platform.openai.com/docs/guides/structured-outputs?api-mode=chat&example=chain-of-thought&type-restrictions=s… 22/24