altrouter.ai
CORE

Structured outputs

Make a model return valid JSON — free-form or strictly matching your JSON schema — via the response_format parameter.

JSON mode

Pass { "type": "json_object" } to guarantee the reply is syntactically valid JSON.

PythonJSON
resp = client.chat.completions.create(
    model="gpt-5.4",
    messages=[{"role": "user", "content": "List 3 primes as JSON"}],
    response_format={"type": "json_object"},
)
print(resp.choices[0].message.content)   # -> {"primes": [2, 3, 5]}

JSON schema

For strict typing, pass a JSON schema — the model returns an object matching it exactly.

PythonJSON
resp = client.chat.completions.create(
    model="gpt-5.4",
    messages=[{"role": "user", "content": "Give a person"}],
    response_format={
        "type": "json_schema",
        "json_schema": {
            "name": "person",
            "strict": True,
            "schema": {
                "type": "object",
                "properties": {
                    "name": {"type": "string"},
                    "age": {"type": "integer"},
                },
                "required": ["name", "age"],
                "additionalProperties": False,
            },
        },
    },
)
print(resp.choices[0].message.content)   # -> {"name": "Alex", "age": 30}

Model support

!
response_format is supported by Gemini and GPT models. Claude models have no native JSON mode — for strict JSON with Claude, use function calling (tools) with a single schema function instead.

Which models support what is summarized in Parameters.

NextImages