Text Models
Structured Outputs
Pass response_format={"type": "json_object"} to force valid JSON output. Describe the shape you want in a system message — the model returns a JSON string you can parse directly. For strict schema enforcement (typed fields, required keys), use Function Calling instead.
structured.py
response = client.chat.completions.create(
model="<model-id>",
messages=[
{#60a5fa]">class="text-emerald-400">"role": class="text-emerald-400">"system", class="text-emerald-400">"content": class="text-emerald-400">"Extract invoice data as JSON."},
{#60a5fa]">class="text-emerald-400">"role": class="text-emerald-400">"user", class="text-emerald-400">"content": class="text-emerald-400">"Invoice #A-2042, total AED 1,250, due 2026-05-01."},
],
response_format={#60a5fa]">class="text-emerald-400">"type": class="text-emerald-400">"json_object"},
)
# response.choices[0].message.content is a JSON string
#60a5fa]">import json
data = json.loads(response.choices[0].message.content)JSON mode support varies by model. If the model doesn't support it, the upstream provider returns an error and you get a 400 back. Check the model's documentation before relying on JSON mode in production.