JSON Mode & Enforcement
What the platform guarantees
Platform JSON modes constrain the model: guaranteed parseable JSON, some with schema enforcement. Knowing which guarantee your platform gives (well-formed-only vs schema-valid) decides your validation burden.
What you will learn
- Enable JSON mode correctly
- Know the platform's guarantee level
- Design the fallback for unenforced schemas
What is this?
'JSON mode' varies by provider: some guarantee only well-formed JSON syntax, others guarantee schema-valid JSON via structured decoding. Before relying on it, read the guarantee: syntax-only modes still need schema validation plus a repair path; schema-typed modes still need a sanity check.
Why do you need it?
- Syntax ≠ schema: well-formed JSON can still be the wrong shape.
- Wasted hours come from expecting a guarantee the platform doesn't make.
- Repair loops cost tokens; knowing the guarantee budgets them.
- Every mode's defaults (temperature, prompts) differ — behavior follows.
Mental model
'A parking space' vs 'a disabled-bay space'
JSON mode guarantees
Syntax-mode guarantees just that your car won't be on the road — you may still be in a loading zone. Schema-mode parks you in the exact allotted bay. Same word 'space', different guarantee — read the fine print.
How it works
- Check the provider's docs: does the mode constrain tokens to JSON syntax? To the schema?
- If schema-constrained, pass the schema and trust the parse (still validate).
- If syntax-only, add prompt instruction + validator + repair loop.
- Note interactions: JSON modes may conflict with certain params (e.g. temperature forced).
- Log mode + provider + validation results into traces.
Guarantee ladder.
In production
- Chat-to-JSON apps enable syntax mode and add a schema repair loop.
- High-stakes endpoints use schema-constrained structured output.
- Mixers: schema mode for the core fields plus a free text slot.
Best practices
- Read the guarantee, not the marketing.
- Always validate the result yourself.
- Design the repair path before the first parse failure.
- Pin provider version; mode semantics change between releases.
Common mistakes
- Assuming syntax mode guarantees your shape.
- No repair loop 'because the mode is guaranteed'.
- Forgetting JSON mode forced temperature/casing quirks.
Common issues
Mode produces prose anywayProvider mode off or malformed config
Check parameters, model version, and SDK option names.
Schema mode rejects valid-ish dataEnum too narrow or type mismatch
Inspect actual outputs; widen enums/relax types via repair.
Debugging tips
- Valid JSON, wrong fields → mode is syntax-only; add schema validation.
- Mode 'works' in demo, not prod → params/temperature overrides; replicate.
- Schema edge values missing → schema too strict for real model outputs.
Interview checkpoint
Quick quiz
Syntax-only JSON mode guarantees…
You should still validate schema-constrained mode outputs.