Structured Outputs
Structured outputs let supported chat models return machine-readable JSON through the response_format request field. Capability support varies by model. Applications should always validate model output before using it, even when using json_schema.
Structured-output flow
Format requested
Generation
JSON text returned
Parse & Validate
Use in application
Modes
JSON Object
type: "json_object"Requests JSON-formatted output, but does not enforce an application-defined schema.
Requirement: The prompt should clearly ask for JSON when required by the selected model.
Limitation: The result must still be parsed and validated for structural correctness.
JSON Schema
type: "json_schema"Provides an explicit JSON Schema to constrain output toward the requested structure.
Note: Schema adherence and strictness depend heavily on the selected model and gateway implementation.
Limitation: The application must still validate the final data.
JSON Object
The simplest way to get JSON output. The model returns a valid JSON object, but the application does not explicitly control the shape via schema.
JSON Schema
JSON Schema requests output that follows the provided schema when supported by the selected model. Provide a valid JSON schema containing type, properties, required, and optionally additionalProperties.
"json_schema": {
"strict": true,
"schema": { ... }
Truncation and errors
Output may be incomplete if the request reaches its output-token limit. JSON parsing will fail on incomplete output. Do not silently trust partially generated JSON.
Always inspect the response finish_reason. If it indicates length truncation, retry the request with a higher limit or simplify the schema.
Caveats
Application-side validation is required
Structured output reduces formatting uncertainty but does not replace application-side validation, error handling, or model-capability checks. Handle refusal, truncation, malformed output, and missing fields appropriately.
Best Practices
Model selection: Select a model that explicitly supports the required structured-output mode.
Schema complexity: Keep schemas small and explicit. Mark required fields clearly. Use additionalProperties: false only when appropriate.
Parsing: Always parse and validate the returned JSON text in your application code before using the data.
Context window: Ensure the output-token limit can easily accommodate the expected size of the returned object.