JSON mode
A model API feature that guarantees the output is valid JSON by constraining token choices during generation.
Think of it like
Like autocorrect that prevents you from typing invalid JSON.
Example
OpenAI API: set `response_format: { type: "json_object" }` and the output will always parse with `json.loads()`.
How it actually works
Under the hood, JSON mode works by blacklisting tokens that would break JSON syntax at each step. First `{` locks you into an object; once you type a key string, only `:` and certain characters follow. It's elegant and fast. Downside: it doesn't validate against a schema, only that the JSON is well-formed. A nested object might not match your expected structure.
For product teams
Eliminates parsing errors; integrations work seamlessly.
For engineers
Check your API docs. Combine with a schema definition (TypeScript type, Pydantic model) in the prompt. Parse the output confidently with no try/catch.
Related
- Structured output — Implementation of structured output.
- Output schema — Output schema is the broader pattern.
Read anything AI without the jargon
Look up any term in plain English, or save terms as you read with the free Chrome extension.
Open DecoderAdd to Chrome