Structured Output
Two Modes
JSON Mode
Set response_format to { "type": "json_object" }. The model returns valid JSON, but you do not control the schema. You must instruct the model in your prompt to use the structure you want.
config.json
json
{ "model": "openai/gpt-4.1", "messages": [ { "role": "system", "content": "You extract contact info. Return JSON with fields: name, email, phone." }, { "role": "user", "content": "John Smith, john@example.com, (555) 123-4567" } ], "response_format": { "type": "json_object" } }
Response:
config.json
json
{ "name": "John Smith", "email": "john@example.com", "phone": "(555) 123-4567" }
JSON Schema Mode
Set response_format to { "type": "json_schema", "json_schema": { ... } }. The model is forced to produce output that validates against your schema. This gives you guaranteed structure.
config.json
json
{ "model": "openai/gpt-4.1", "messages": [ { "role": "user", "content": "Extract the product info: iPhone 16 Pro, $999, 256GB storage, available in black and white." } ], "response_format": { "type": "json_schema", "json_schema": { "name": "product_info", "strict": true, "schema": { "type": "object", "properties": { "product_name": { "type": "string" }, "price_usd": { "type": "number" }, "storage_gb": { "type": "integer" }, "colors": { "type": "array", "items": { "type": "string" } } }, "required": ["product_name", "price_usd", "storage_gb", "colors"], "additionalProperties": false } } } }
Response:
config.json
json
{ "product_name": "iPhone 16 Pro", "price_usd": 999, "storage_gb": 256, "colors": ["black", "white"] }
Code Examples
terminal
bash
curl -X POST https://api.chuizi.ai/v1/chat/completions \ -H "Authorization: Bearer ck-your-key-here" \ -H "Content-Type: application/json" \ -d '{ "model": "openai/gpt-4.1", "messages": [ {"role": "user", "content": "Extract: Tokyo, population 14 million, country Japan, continent Asia"} ], "response_format": { "type": "json_schema", "json_schema": { "name": "city_info", "strict": true, "schema": { "type": "object", "properties": { "city": {"type": "string"}, "population": {"type": "integer"}, "country": {"type": "string"}, "continent": {"type": "string"} }, "required": ["city", "population", "country", "continent"], "additionalProperties": false } } } }'
Next Steps
- Function Calling — structured tool calls as an alternative to JSON output
- Chat Completions API — full
response_formatparameter reference - Error Handling — handle schema validation failures gracefully