Structured outputs

Structured outputs

response_format makes the model answer with JSON instead of prose. Use json_schemawhen you have a shape to enforce, and json_object when you only need valid JSON.

ParameterTypeRequiredDescription
response_formatobjectOptionalThe output contract. Forms:{type:"text"}{type:"json_object"}{type:"json_schema", json_schema:{name, schema, strict}}
import json, os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["OXYY_API_KEY"],
    base_url="https://api.oxyy.ai/v1"
)

response = client.chat.completions.create(
    model="gpt-5.5",
    messages=[{"role": "user", "content": "Extract the invoice fields."}],
    response_format={
        "type": "json_schema",
        "json_schema": {
            "name": "invoice",
            "strict": True,
            "schema": {
                "type": "object",
                "properties": {
                    "total": {"type": "number"},
                    "currency": {"type": "string"},
                },
                "required": ["total", "currency"],
                "additionalProperties": False,
            },
        },
    },
)

data = json.loads(response.choices[0].message.content)
# json_object is the looser mode: valid JSON, no schema enforced.
curl https://api.oxyy.ai/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OXYY_API_KEY" \
  -d '{
    "model": "gpt-5.5",
    "messages": [{"role": "user", "content": "List 3 colours as JSON."}],
    "response_format": {"type": "json_object"}
  }'
Portable by construction. Providers that have no native schema mode are given a forced output tool instead, and the answer is unwrapped for you — including stripping the ```json fence some models add. Either way choices[0].message.content is a JSON document you can parse. strict: true needs a fully closed schema (additionalProperties: false and every property in required); leave it off if yours is not.