Structured Model Outputs - OpenAI API
Structured Model Outputs - OpenAI API
Copy page
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.
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.
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 text.format
https://platform.openai.com/docs/guides/structured-outputs 2/21
10/15/25, 7:46 PM Structured model outputs - OpenAI API
The remainder of this guide will focus on non-function calling use cases in the Responses 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 adherence. 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
You can ask the model to output an answer in a structured, step-by-step way, to guide the user
through the solution.
https://platform.openai.com/docs/guides/structured-outputs 3/21
10/15/25, 7:46 PM Structured model outputs - OpenAI API
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 4/21
10/15/25, 7:46 PM Structured model outputs - OpenAI API
18 },
19 {
20 "explanation": "Simplify the fraction.",
21 "output": "x = -15 / 4"
22 }
23 ],
24 "final_answer": "x = -15 / 4"
25 }
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.
javascript
https://platform.openai.com/docs/guides/structured-outputs 5/21
10/15/25, 7:46 PM Structured model outputs - OpenAI API
14 { role: "system", content: "You are a helpful math tutor. Guide the user thr
15 { role: "user", content: "how can I solve 8x + 7 = -23" },
16 ],
17 response_format: zodResponseFormat(MathReasoning, "math_reasoning"),
18 });
19
20 const math_reasoning = completion.choices[0].message
21
22 // If the model refuses to respond, you will get a refusal message
23 if (math_reasoning.refusal) {
24 console.log(math_reasoning.refusal);
25 } else {
26 console.log(math_reasoning.parsed);
27 }
The API response from a refusal will look something like this:
json
1 {
2 "id": "resp_1234567890",
3 "object": "response",
4 "created_at": 1721596428,
5 "status": "completed",
6 "error": null,
7 "incomplete_details": null,
8 "input": [],
9 "instructions": null,
10 "max_output_tokens": null,
11 "model": "gpt-4o-2024-08-06",
12 "output": [{
13 "id": "msg_1234567890",
14 "type": "message",
15 "role": "assistant",
16 "content": [
17 {
18 "type": "refusal",
19 "refusal": "I'm sorry, I cannot assist with that request."
20 }
21 ]
22 }],
23 "usage": {
24 "input_tokens": 81,
25 "output_tokens": 11,
26 "total_tokens": 92,
27 "output_tokens_details": {
28 "reasoning_tokens": 0,
29 }
30 },
31 }
https://platform.openai.com/docs/guides/structured-outputs 6/21
10/15/25, 7:46 PM Structured model outputs - OpenAI API
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
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.
javascript
https://platform.openai.com/docs/guides/structured-outputs 7/21
10/15/25, 7:46 PM Structured model outputs - OpenAI API
Supported schemas
Structured Outputs supports a subset of the JSON Schema language.
Supported types
String
Number
https://platform.openai.com/docs/guides/structured-outputs 8/21
10/15/25, 7:46 PM Structured model 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
https://platform.openai.com/docs/guides/structured-outputs 9/21
10/15/25, 7:46 PM Structured model outputs - OpenAI API
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
3
4 const BaseResponseSchema = z.object({/* ... */});
5 const UnsuccessfulResponseSchema = z.object({/* ... */});
6
7 const finalSchema = z.discriminatedUnion('status', [
8 BaseResponseSchema,
9 UnsuccessfulResponseSchema,
10 ]);
11
12 // Invalid JSON Schema for Structured Outputs
13 const json = zodResponseFormat(finalSchema, 'final_schema');
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
1 {
2 "name": "get_weather",
3 "description": "Fetches the weather in the given location",
https://platform.openai.com/docs/guides/structured-outputs 11/21
10/15/25, 7:46 PM Structured model outputs - OpenAI API
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 10 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.
Structured Outputs only supports generating specified keys / values, so we require developers
to set additionalProperties: false to opt into Structured Outputs.
json
https://platform.openai.com/docs/guides/structured-outputs 12/21
10/15/25, 7:46 PM Structured model outputs - OpenAI API
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.
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
https://platform.openai.com/docs/guides/structured-outputs 13/21
10/15/25, 7:46 PM Structured model outputs - OpenAI API
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 },
42 "additionalProperties": false,
43 "required": [
44 "number",
45 "street",
46 "city"
47 ]
https://platform.openai.com/docs/guides/structured-outputs 14/21
10/15/25, 7:46 PM Structured model outputs - OpenAI API
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",
27 "output"
28 ],
29 "additionalProperties": false
30 }
31 },
32 "required": [
33 "steps",
https://platform.openai.com/docs/guides/structured-outputs 15/21
10/15/25, 7:46 PM Structured model outputs - OpenAI API
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
33 },
34 "value": {
35 "type": "string",
36 "description": "The value of the attribute"
37 }
38 },
39 "additionalProperties": false,
https://platform.openai.com/docs/guides/structured-outputs 16/21
10/15/25, 7:46 PM Structured model outputs - OpenAI API
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 ]
31 }
32 },
33 "additionalProperties": false,
34 "required": [
35 "linked_list"
36 ]
37 }
https://platform.openai.com/docs/guides/structured-outputs 17/21
10/15/25, 7:46 PM Structured model outputs - OpenAI API
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 Responses API you can set the text.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
To learn more about Structured Outputs, we recommend browsing the following resources:
https://platform.openai.com/docs/guides/structured-outputs 18/21
10/15/25, 7:46 PM Structured model outputs - OpenAI API
https://platform.openai.com/docs/guides/structured-outputs 19/21
10/15/25, 7:46 PM Structured model outputs - OpenAI API
https://platform.openai.com/docs/guides/structured-outputs 20/21
10/15/25, 7:46 PM Structured model outputs - OpenAI API
https://platform.openai.com/docs/guides/structured-outputs 21/21