Structured Outputs
JSON Mode
{
"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" }
}{
"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"
}
]
}JSON Schema Enforcement
Schema 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
2. Describe the expected shape
3. Use JSON Schema for critical applications
4. Handle parsing errors gracefully
5. Keep schemas simple
Comparison: JSON Mode vs JSON Schema
Feature
JSON Mode
JSON Schema
Next Steps
Last updated