
JSON Mode for Reliable Structured Output
If your code needs data, not prose, JSON mode removes the flakiest step in your pipeline: parsing whatever the model decided to write.

How it works ⚙️
Set the response format to JSON and describe the shape you want in the prompt. The model then returns only valid JSON matching your instructions.
A working example 💾
Ask for a structured review: tell the model the keys you need — rating, summary, tags — and JSON mode guarantees the response parses cleanly on the first try.
| Field | Requirement | Result |
|---|---|---|
| rating | Integer 1–5 | Always a number |
| summary | One short paragraph | Always a string |
| tags | Array of strings | Always an array |
Still validate on your side 🧪
JSON mode guarantees valid JSON, not the exact schema you imagined. Validate with a schema or at least check required keys, and default missing ones gracefully.
Frequently asked questions ❓
Is JSON mode more expensive?
Slightly — the model must follow a format — but far cheaper than retrying broken parses.
Can I get nested JSON?
Yes, describe any nested structure in your prompt and the model will mirror it.
What if the model still returns an unexpected field?
Your own validation should ignore unknown fields and only require the ones you truly need.
Comments (0)