Structured outputs
Force the model to return JSON with response_format — ideal for extraction, classification
and anything you parse programmatically.
ℹ Note
response_format is forwarded to OpenAI-compatible providers; AIx auto-routes
requests that use it. JSON-schema strictness depends on the underlying model — for
guaranteed schema adherence pick a model that supports json_schema.
JSON object mode
from openai import OpenAI
client = OpenAI(base_url="https://api.aix.theaimart.co/v1", api_key="$AIX_KEY")
resp = client.chat.completions.create(
model="deepseek-ai/DeepSeek-V3.2",
messages=[
{"role": "system", "content": "Extract the fields as JSON."},
{"role": "user", "content": "Invoice #421 from Acme for $1,299 due 2026-07-01"},
],
response_format={"type": "json_object"},
)
import json
data = json.loads(resp.choices[0].message.content)
JSON schema (when supported)
schema = {
"type": "object",
"properties": {
"invoice_no": {"type": "string"},
"vendor": {"type": "string"},
"amount_usd": {"type": "number"},
"due_date": {"type": "string"},
},
"required": ["invoice_no", "vendor", "amount_usd"],
"additionalProperties": False,
}
resp = client.chat.completions.create(
model="deepseek-ai/DeepSeek-V3.2",
messages=[{"role": "user", "content": "Invoice #421 from Acme for $1,299 due 2026-07-01"}],
response_format={"type": "json_schema", "json_schema": {"name": "invoice", "schema": schema}},
)
✦ Tip
Always wrap json.loads in a try/except and retry once on failure — even in JSON mode a
model can occasionally emit invalid JSON. Ask for the schema in the system prompt too.