JSON Mode
JSON mode forces the model to respond strictly in valid JSON format, without any extra text, explanations, or markdown. It ensures that the output is clean, machine-readable, and ready for parsing. Ideal for structured workflows and automation.
How does it work
When enabled, JSON mode constrains the model's output generation so that it only returns valid JSON. The model won't include comments, natural language descriptions, or any formatting outside of the JSON structure. This improves reliability and reduces the need for post-processing or regex-based parsing.
When to use JSON mode
- When you need guaranteed machine-readable output
- When integrating with code, APIs, or tools that expect clean JSON
- When working with structured outputs, function calling, or tool use
- When building systems where output validation is critical
How to use JSON mode
- Enable JSON mode. In supported models (like OpenAI GPT-4 Turbo), set
response_format: "json"
in the API call - Prompt the model to return a specific structure. For example: Return your response in this format:
{"summary": string, "action_items": string[] }
- Parse the response. Since JSON mode guarantees well-formed JSON, you can safely use JSON.parse() in your application
- Handle parsing errors. While rare in JSON mode, still validate the output if your application depends on strict schemas
Tips
- JSON mode helps avoid formatting errors caused by markdown or extra natural language
- Pair with temperature = 0 for the most deterministic and consistent output
- Use in combination with structured output prompts to create API-ready responses
- Only available in certain models. Check the documentation for compatibility