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:

1

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.

2

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.

json_extraction.py
import json from openai import OpenAI client = OpenAI( api_key="your_continuum_key", base_url="https://api.continuum.au/v1" ) system_prompt = """ Extract company information from the text and return it as JSON. EXAMPLE OUTPUT: { "company_name": "Example Corp", "ticker": "EXC.AX", "sector": "Technology", "revenue_aud": 150000000 } """ response = client.chat.completions.create( model="deepseek-v4-flash", messages=[ {"role": "system", "content": system_prompt}, {"role": "user", "content": "Commonwealth Bank reported revenue of..."} ], response_format={"type": "json_object"}, max_tokens=1000 ) result = json.loads(response.choices[0].message.content) print(result["company_name"]) # Structured access

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.

json_with_retry.py
import json from openai import OpenAI def get_structured_response(client, messages, max_retries=3): """Request JSON with retry logic for production reliability.""" for attempt in range(max_retries): response = client.chat.completions.create( model="deepseek-v4-flash", messages=messages, response_format={"type": "json_object"}, max_tokens=2000 ) content = response.choices[0].message.content if not content: continue # Retry on empty response try: return json.loads(content) except json.JSONDecodeError: continue # Retry on invalid JSON raise ValueError("Failed to get valid JSON after retries")

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.