JSON Mode & Schemas
JSON
Part of: Structured Outputs
Add one parameter to your API call and the model can no longer answer in a chatty sentence. It must return valid JSON . That single flag turns the unreliable-prose problem from lesson 1 into a solved one, most of the time. The rest of the work is telling the model exactly what shape you want. What it is JSON (JavaScript Object Notation) is a simple, universal format for structured data: keys mapped to values, with strings, numbers, booleans, lists, and nested objects. Every language can parse it in one line. It is the lingua franca between an LLM and your code. JSON mode is a setting on modern model APIs that constrains the output so it is always syntactically valid JSON, no prose wrapper, no trailing commentary, no broken brackets. A schema goes further: it specifies the exact keys, their types, and which are required, so you get not just valid JSON but the right JSON. How it works There are two levels of control, and you usually want both: 1. Turn on JSON mode. You pass something like response format={"type": "json object"}. The model's decoding is constrained so the output parses. 2. Describe the shape in the prompt. JSON mode alone guarantees valid JSON, not the keys you want.
Challenge: Validate required keys