What is the difference between JSON mode, a strict schema mode, and grammar-constrained decoding?
answer
- three tiers, three different guarantees
- parseable is not the same as conformant
- JSON mode: syntax only, no schema
- strict mode enforces the schema while decoding
- grammar tier: SQL, DSLs, non-JSON formats
basics
~20 sJSON mode guarantees only that the text parses as JSON. A strict schema mode additionally guarantees the object conforms to your schema — required keys, types, enums. A custom grammar constrains any formal language, JSON or otherwise.
solid answer
~50 sThey are three different guarantees, not three names for one feature. A plain **JSON mode** promises syntactic validity: the response parses, but it may have invented keys, missing fields, or a string where you wanted a number. A **strict schema mode** takes your schema and enforces it during generation, so required keys are present, types match, and enum fields hold only listed members — this is what production extraction relies on, and as of mid-2026 it is the default shape across frontier providers, with JSON mode effectively legacy. A **custom grammar** (a context-free grammar rather than a schema) is the most general tier: it can constrain output that is not JSON at all — a SQL dialect, a small DSL, a fixed line format. Two caveats: strict schema modes usually accept only a subset of JSON Schema, and none of the three enforces business rules, so a validation layer still sits on top.
code
ebnf · 4 linesrecord = "{" , "\"solvent\":" , solvent , ",\"yield\":" , number , "}" ;
solvent = "\"DCM\"" | "\"THF\"" | "\"toluene\"" ;
number = digit , { digit } ;
digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;go deeper
Be able to say plainly that JSON mode only promises the text parses, while a schema mode promises the fields and types you asked for. Naming that gap is enough at this level.
Explain that the schema is compiled into a constraint the decoder obeys token by token, and that strict modes accept only a subset of JSON Schema — flat records, required-plus-nullable fields, bounded nesting.
Show that you design the schema for enforceability and keep a business-rule validator above it. Talk about flattening unions, handling truncation, and treating a schema guarantee as covering form only.
Own the contract boundary: which guarantees the platform provides to every team versus what each pipeline validates itself, and whether standardising on a schema mode or a house grammar is the better long-run investment.
## The question behind the question An interviewer asking this wants to know whether you can tell *syntax* from *schema* from *language*. Teams routinely ship a pipeline on a JSON mode, see clean parses in testing, and then discover in production that a third of the rows are missing a field the downstream loader requires. The three tiers give strictly increasing guarantees at strictly increasing cost and rigidity. ## Tier 0 — a prompt instruction only Asking for JSON in the prompt gives you no guarantee at all. The model may wrap the object in prose, open with a markdown fence, or trail an explanation. This tier is a probability, not a contract, and it degrades exactly on the inputs that are hardest — long, ambiguous, or unusual documents. ## Tier 1 — JSON mode: syntax only A JSON mode forces the output to be a parseable JSON value. Under the hood this is already a constraint on the decoder: at each step only tokens that can continue a legal JSON document are allowed, so unbalanced braces, trailing commas, and unquoted keys become impossible. What it does **not** know is your schema. Extracting reaction yield, solvent and temperature from an organic-chemistry abstract, a JSON-mode response can legitimately be `{}`, `{"note": "not reported"}`, or `{"yield": "about 70%"}` — all valid JSON, none loadable into a fixed record. This is why JSON mode is described as legacy: it removes the parse error and leaves the schema error. ## Tier 2 — strict schema mode: conformance Here you supply a schema and the provider compiles it into the decoding constraint. Required keys must appear, a numeric field cannot receive a quoted string, an enum field can only hold a listed member, and unknown keys cannot be opened when the schema forbids them. The practical consequence is that the *shape* of the output stops being a source of production incidents; your failures move from parsing to semantics. The price is expressiveness. Strict modes typically support a subset of JSON Schema, and the limits are worth knowing because they are where deployments trip: - every property may have to be listed as required (optionality expressed as a nullable type instead), - open-ended objects and unconstrained additional properties are often disallowed, - nesting depth, total property count, and schema size are bounded, - arbitrary composition (deeply nested unions, conditional subschemas) is frequently unsupported, and - string `pattern` support varies, so a regex constraint is not universally available. A schema that models a union of eight variants with conditional branches usually has to be flattened — one discriminator enum plus a wide flat record — before it can be enforced at all. ## Tier 3 — a full grammar: any formal language A grammar constraint drops the assumption that the target is JSON. You give a context-free grammar and the decoder is held to it, which lets you generate a restricted SQL subset, a configuration DSL, a strict CSV line, or a domain notation. It also lets you express things a schema cannot: interleaved literal text, an ordering requirement, or a token-level format for a field. The cost is that you now own a grammar — an ambiguous or over-permissive one produces surprising output, and an over-tight one can leave the decoder no legal continuation at some state. ## What none of the tiers gives you All three constrain *form*. None constrains meaning. A schema-valid record can carry a solvent that the abstract never mentions, a yield of 250 percent, or a temperature copied from a different reaction. Range checks, cross-field consistency, referential checks against your own tables, and plausibility rules belong in a validation layer above the decoder, and that layer is not optional at any tier. ## How to choose Default to a strict schema mode with a flat, enum-heavy schema and business-rule validation on top. Drop to a grammar only when the target is not JSON or when you need a token-level format the schema dialect cannot express. Reach for a bare JSON mode essentially never — its only remaining role is compatibility with an endpoint that offers nothing stronger.
- Your schema is a union of eight document variants with conditional branches and a strict mode rejects it. What do you do?Flatten it. Replace the union with one wide, flat record plus a discriminator enum naming the variant, make every field present-but-nullable, and move the 'which fields must be non-null for this variant' rule into a validator that runs after decoding. Strict modes bound nesting, size, and composition, so expressing variance as data rather than as schema structure is what actually gets enforced.
- If the strict mode already guarantees my types, what is left for a validation layer to do?Everything about meaning: numeric ranges, cross-field consistency (a temperature that contradicts the stated solvent's boiling point), referential checks against your own reference tables, duplicate detection, and plausibility. The decoder guarantees the record is well-formed and typed; it has no notion of whether the values are true or mutually coherent.
- Does a strict schema mode remove the need to handle a failed response at all?No. Two failure paths survive: the generation can stop at the token ceiling, leaving a legal-but-incomplete prefix, and the request can be refused or error before any constrained tokens are produced. Both must be handled explicitly — a schema guarantee is conditional on generation actually finishing.
saying these in an interview costs you the question
- Treating JSON mode as a schema guarantee
- Assuming any JSON Schema is accepted by a strict mode
- Claiming strict mode makes validation unnecessary
- Believing a grammar constraint can check business rules
- Thinking optional fields work the same as in ordinary JSON Schema