JSON structured output
Guaranteed valid JSON responses for extraction pipelines, structured data workflows, and API integrations.
Enabling JSON mode
Set the response_format parameter to {"type": "json_object"} to ensure the model outputs valid JSON. Two requirements:
Include "json" in your prompt
The word "json" must appear in the system or user message. Provide an example of the desired JSON structure to guide the model.
Set max_tokens generously
Set max_tokens high enough to avoid truncating the JSON. Truncated JSON is invalid JSON.
Example: entity extraction
Extract structured data from unstructured text. The model returns valid JSON matching your specified schema.
Reliability and validation
JSON mode significantly increases the probability of valid JSON output but is not 100% guaranteed in all edge cases. For production pipelines, implement validation and retry logic.
Best practices
- •Always include a JSON example in your system or user message
- •Set max_tokens high enough that the JSON will not be truncated
- •Validate the parsed JSON against your expected schema
- •Implement retry logic (2-3 attempts) for production pipelines
- •Use specific field names and types in your example to guide the model
- •For complex schemas, consider using tool calling with strict mode instead
Need more structured control?
For guaranteed schema compliance, use tool calling with strict mode. It validates output against your JSON schema at the API level.