Structured output makes model responses easier to parse and integrate. Depending on the selected model, use JSON object mode or a strict JSON schema.
Request a JSON object
Tell the model to produce JSON and set response_format. Some models require an explicit instruction containing the word “JSON.”
Use JSON schema
On models that support strict structured output, send response_format.type = json_schema with a named schema:
Validate at the boundary
Even strict output can be interrupted by content filtering, output limits, network failure, or provider-specific behavior. Your application should:
- check the HTTP response and finish status;
- parse JSON with size and depth limits;
- validate against the same schema used in the request;
- reject unknown fields when they are unsafe;
- apply domain validation, such as ranges and cross-field rules;
- handle refusals and truncated output separately from schema failures.
JSON mode versus schema
Never interpolate model-produced JSON into SQL, shell commands, HTML, or authorization rules without context-specific escaping and validation.