Asking a model to "respond only in JSON" works most of the time. That still leaves enough malformed replies to break a pipeline. Structured outputs fix this at the decoding level: you hand the API a JSON Schema, and the reply is constrained to match it.
Claude: output_config.format
{
"model": "claude-opus-5-5",
"max_tokens": 4096,
"output_config": {
"effort": "low",
"format": {
"type": "json_schema",
"schema": {
"type": "object",
"properties": {
"vendor": { "type": "string" },
"total": { "type": "number" },
"due_date": { "type": ["string", "null"] }
},
"required": ["vendor", "total", "due_date"],
"additionalProperties": false
}
}
},
"messages": [{ "role": "user", "content": "Invoice from Northwind, $1,240.50, due 15 Dec 2026" }]
}
- The text block of the response is JSON that matches the schema.
- The older top-level
output_formatparameter is deprecated. Useoutput_config.format. - Prefill tricks (starting the assistant turn with
{) return a 400 on current models. Use this instead. - For tool arguments, set
"strict": trueon the tool definition (withadditionalProperties: false). - Extraction is usually a good fit for
effort: "low".
OpenAI: response_format
{
"model": "gpt-5.6",
"messages": [{ "role": "user", "content": "..." }],
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "invoice",
"strict": true,
"schema": { "type": "object", "properties": { "vendor": { "type": "string" } }, "required": ["vendor"], "additionalProperties": false }
}
}
}
On the Responses API the same schema goes under text.format. In strict mode, OpenAI requires every property to be listed in required. Model optional fields as nullable.
Gemini: responseJsonSchema
{
"generationConfig": {
"responseMimeType": "application/json",
"responseJsonSchema": { "type": "object", "properties": { "vendor": { "type": "string" } }, "required": ["vendor"] }
}
}
Gemini also has an older responseSchema field that takes an OpenAPI-style subset. responseJsonSchema accepts standard JSON Schema.
Schema rules that work on every provider
additionalProperties: falseon every object.- List every field in
required. Make missing facts["string", "null"]rather than optional, so the model says "null" instead of inventing a value. - Write a
descriptionper field. The model reads it as an instruction ("ISO 4217 code, e.g. USD"). - Keep it flat. Deep recursion, huge enums and exotic keywords are where provider support differs.
- Prefer
enumover free text for categories.
Handle the failure cases
The schema guarantees the shape, not the outcome. Before you parse:
- Check the stop reason.
max_tokensmeans the JSON may be truncated. A refusal may carry no JSON at all. - Validate business rules (totals add up, dates are in range) after parsing. The model can still be wrong inside a valid shape.
- Treat extracted values as untrusted input, especially before they reach SQL, shell commands or HTML.
Open the structured output template and ask the workspace to "add a line_items array to the schema" or "convert this to the OpenAI response_format shape", then diff the result.