Direct Answer
Legacy json_object mode only guarantees syntactically valid JSON, leaving keys, types, and nesting unverified. Structured Outputs (strict: true via json_schema or Pydantic parsing) compiles the schema into a context-free grammar that masks invalid tokens at decoding time, guaranteeing 100% schema compliance. Schemas must set additionalProperties: false on every object, list every property in required (representing optional fields as nullable unions like ["string", "null"]), and callers must inspect refusal before parsing JSON.
python (openai>=1.x)
import json
from openai import OpenAI
client = OpenAI()
TICKET_SCHEMA = {
"name": "support_triage",
"strict": True,
"schema": {
"type": "object",
"properties": {
"category": {"type": "string", "enum": ["billing", "auth", "bug"]},
"severity": {"type": "integer"},
"assignee_hint": {"type": ["string", "null"]},
},
"required": ["category", "severity", "assignee_hint"],
"additionalProperties": False,
},
}
resp = client.chat.completions.create(
model="gpt-4.1-mini",
messages=[{"role": "user", "content": "SSO login returns 403 for EU tenants."}],
response_format={"type": "json_schema", "json_schema": TICKET_SCHEMA},
)
msg = resp.choices[0].message
if msg.refusal:
raise RuntimeError(f"Model refused request: {msg.refusal}")
payload = json.loads(msg.content)