LLMscore
Structured outputs
Getting JSON that matches a schema every time, not most of the time.
What it is and why it exists
Three levels: ask nicely in the prompt (works often), JSON mode (valid JSON, any shape), and schema-constrained decoding where the server masks out any token that would break the schema — invalid output becomes impossible. Tool calling is the same mechanism with a function signature.
Quick prototype
“Reply in JSON” and json.loads with a try/except.
In production, at scale
Use schema-constrained output where offered, validate anyway with Pydantic or Zod (semantic rules a schema can’t express), and on failure retry once with the validation error in the prompt. Keep schemas flat and small; every field is a chance to hallucinate.
Vendors and tools
| OpenAI Structured Outputs | strict JSON Schema; response_format and strict tool schemas |
|---|---|
| Anthropic tool use / structured outputs | JSON schemas via tools, with strict schema options on newer models |
| Gemini responseSchema | Schema-constrained JSON responses |
| Instructor / Outlines / XGrammar | Validation-retry wrappers and grammar-constrained decoding for open models |
On AWS: Bedrock Converse tool use with a JSON schema; Amazon Bedrock Data Automation for document-to-structure extraction.
How it fails
- Valid JSON with wrong values — constraint guarantees shape, not truth
- Deeply nested optional fields the model fills with invented data
- Retry loops that resend the same failing prompt
What the interviewer asks next
How can constrained decoding guarantee valid JSON?
At each step the server knows which tokens keep the output a valid prefix of the schema’s grammar and sets every other token’s probability to zero before sampling.
When would you still validate?
Always — schemas can’t express “end date after start date” or “ID must exist”. Validation catches semantic errors and gives you a retry signal.