Force the model to return well-formed JSON that matches a schema you define.
Structured outputs guarantee the model produces valid JSON that conforms to a schema you supply. Instead of parsing free-form text, your code receives a document it can deserialize directly — no regex, no error handling for malformed output.
There are two ways to request structured output:
response_format: { type: "json_schema", json_schema: { ... } } — strict schema enforcement; the model cannot produce output that violates the schema.response_format: { type: "json_object" } — JSON mode; the model returns valid JSON but without a specific shape enforced.Both require the model to support structured output. Check the capabilities field in GET /api/models/{slug} for json_mode or json_schema support.
response_format with a JSON schema#Pass a JSON Schema object alongside response_format. The schema describes the shape of the object you want back.
The model's message.content will be a JSON string you can parse directly:
Not all models support structured outputs. Use the catalog to find compatible models:
Models that support strict schema enforcement will list json_schema under capabilities. Models with json_mode only will produce valid JSON but cannot guarantee schema conformance.
additionalProperties: false at every object level when using strict: true — this closes the schema so the model has no room to add unexpected keys.required when using strict mode. If a field is truly optional, use a union with null rather than omitting it from required.Structured outputs are compatible with streaming ("stream": true). The JSON is emitted as token deltas exactly like normal streaming — accumulate the full content string across all chunks, then parse once the stream ends with [DONE].
Do not attempt to parse partial JSON mid-stream; wait for the complete payload.
If the model cannot produce output that satisfies the schema, the request fails with a 400 invalid_request error rather than returning malformed JSON. Common causes:
strict: true is set but additionalProperties is not false at every level.Use "type": "json_object" as a fallback when strict enforcement isn't required.