OpenAI provides two powerful approaches for getting structured JSON responses from their models: JSON Mode and Structured Outputs. Both serve different purposes and have distinct implementation methods.
JSON Mode
JSON Mode ensures your responses are valid JSON objects but with less strict enforcement of schema structure.
How to implement JSON Mode:
- Set response_format parameter:
response = client.chat.completions.create(
model="gpt-4o",
response_format={"type": "json_object"},
messages=[
{"role": "system", "content": "You are a helpful assistant designed to output JSON."},
{"role": "user", "content": "Return data about popular games"}
]
)
-
Requirements:
- You must include the word "json" somewhere in your messages[8][2]
- Works with models like gpt-35-turbo, gpt-4, gpt-4o, and gpt-4o-mini[8]
-
Example with specific keys:
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a gaming bot. Respond in JSON format with these keys: 'game_type', 'platform', and 'popularity'."},
{"role": "user", "content": "Tell me about a popular battle royale game."}
],
response_format={"type": "json_object"}
)
- Limitations:
- JSON Mode doesn't guarantee a specific schema, only valid JSON[8]
- When needing arrays, they must be wrapped within an object[10]
- For example, use
{"items": [{"key": "value"}, {"key": "value2"}]} rather than directly returning an array[10]
Structured Outputs
Structured Outputs is a more powerful and newer feature (introduced August 2024) that ensures responses adhere to a specific JSON schema[3][9].
How to implement Structured Outputs:
- Define a JSON schema:
{
"type": "object",
"properties": {
"game_name": {"type": "string"},
"genre": {"type": "string"},
"price": {"type": "number"}
},
"required": ["game_name", "genre", "price"]
}
- Make API request:
response = client.responses.create(
model="gpt-4o-2024-08-06",
input=[
{"role": "system", "content": "Extract the game name, genre, and price of this game."},
{"role": "user", "content": "The game 'Legend of the Arena' is an action RPG and costs $59.99."}
],
text={
"format": {
"type": "json_schema",
"schema": {
"type": "object",
"properties": {
"game_name": {"type": "string"},
"genre": {"type": "string"},
"price": {"type": "number"}
},
"required": ["game_name", "genre", "price"],
"additionalProperties": false
},
"strict": true
}
}
)
- More complex schema example:
{
"type": "object",
"properties": {
"steps": {
"type": "array",
"items": {
"type": "object",
"properties": {
"explanation": { "type": "string" },
"output": { "type": "string" }
},
"required": ["explanation", "output"],
"additionalProperties": false
}
},
"final_answer": { "type": "string" }
},
"required": ["steps", "final_answer"],
"additionalProperties": false
}
- Benefits:
- Guarantees responses match your exact schema[1][3]
- Enforces required fields[1]
- Much more reliable for production applications[9]
- Works with complex nested structures[1][4]
When to use which approach
-
Use JSON Mode when: You need simple structured responses without strict schema requirements, or when working with models that don't support Structured Outputs[9]
-
Use Structured Outputs when: You need guaranteed schema adherence for integration with business systems, APIs, or when working with complex data structures that must follow specific formats[9][3]
Structured Outputs is generally recommended over JSON Mode when possible, as it provides stricter guarantees about the response format and structure[8][9].