Structured Outputs
Structured outputs let you constrain the model to return valid JSON that conforms to a specific schema. This is essential when the model's response needs to be parsed by code rather than read by a human.
JSON Mode
The simplest way to get JSON output is to set response_format to { "type": "json_object" }. This guarantees the model's response is valid JSON.
{
"model": "dos-ai",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant that always responds in JSON format."
},
{
"role": "user",
"content": "List 3 programming languages with their year of creation."
}
],
"response_format": { "type": "json_object" }
}Response:
{
"choices": [
{
"message": {
"role": "assistant",
"content": "{\n \"languages\": [\n {\"name\": \"Python\", \"year\": 1991},\n {\"name\": \"JavaScript\", \"year\": 1995},\n {\"name\": \"Go\", \"year\": 2009}\n ]\n}"
},
"finish_reason": "stop"
}
]
}The content field is a JSON string. Parse it in your code:
Important: When using JSON mode, you must include the word "JSON" somewhere in the system or user message. This is a safety measure to ensure the user intends to receive JSON output.
JSON Schema Enforcement
For stronger guarantees, you can provide a JSON Schema that the model's output must conform to. This uses response_format with type: "json_schema":
Response:
Schema Rules
When using strict: true, the following rules apply:
All fields must be listed in
required.additionalPropertiesmust be set tofalse.Supported types:
string,number,integer,boolean,array,object,null.Use
enumto restrict string values.Nested objects must also follow these rules.
Code Examples
Python
Python with JSON Schema
JavaScript
JavaScript with JSON Schema
cURL
Tips for Reliable JSON Output
1. Always mention JSON in the prompt
When using response_format: { type: "json_object" }, include an explicit instruction about JSON in the system message. Without it, the model may produce an error or unpredictable output.
2. Describe the expected shape
Even with JSON mode, the model needs to know what structure you expect. Be explicit about field names and types:
3. Use JSON Schema for critical applications
For production systems where invalid JSON would cause failures, prefer json_schema with strict: true over plain json_object mode. The schema enforcement happens at the decoding level, guaranteeing structural correctness.
4. Handle parsing errors gracefully
Even with structured outputs, always wrap JSON parsing in error handling:
5. Keep schemas simple
Deeply nested schemas with many optional fields increase the chance of unexpected output. Flatten where possible and use required to ensure critical fields are always present.
Comparison: JSON Mode vs JSON Schema
Setting
{"type": "json_object"}
{"type": "json_schema", "json_schema": {...}}
Guarantee
Valid JSON
Valid JSON conforming to your schema
Field control
Must be described in prompt
Enforced by schema
strict mode
Not applicable
Optional (recommended)
Use case
Simple/flexible JSON responses
Production APIs, data pipelines
Next Steps
Chat Completions -- learn the basics of the API.
Function Calling -- let the model call external tools.
Streaming -- receive tokens in real time.
Last updated