Schema Design for LLMs
Contracts that generation can keep
LLM-friendly schemas are flat, optional-heavy, and enum-driven. Designing your contract to the strengths of generation — not to the dreams of a relational DBA — raises compliance and unblocks the product.
What you will learn
- Design flat, nullable, enum-driven schemas
- Split over-stubborn schemas into stages
- Measure schema compliance over time
What is this?
Schema design for LLMs is a constraint problem: models comply best with flat objects, bounded enums, few required fields, and clear nullable semantics. Deep nesting and required grids tempt failure. Design starts from what the task needs, then trims to what generation reliably emits.
Why do you need it?
- Compliance is a hidden quality axis — a 'perfect' schema the model can't fill is worthless.
- Flat beats nested for generation; nullable beats pseudo-values.
- Enum discipline gives downstream switch/UI typed branches.
- Measured compliance (validated % of calls) drives schema evolution honestly.
Mental model
A survey that people actually finish
LLM-friendly schema
Length, requiredness and nesting decide completion. A 3-question optional survey gets answered; a 40-question required matrix gets abandoned or, worse, answered with junk. LLM schemas follow the same psychology — keep fields few, optional and clear.
How it works
- Start from the task's true output needs; drop decorative fields.
- Prefer flat objects; nest only where meaning demands it.
- Mark fields optional unless the consumer cannot proceed without them.
- Use enums for closed sets; allow refusals/unknown via 'other'.
- Measure compliance; refactor schema when it strays from realistic output.
Schema design flow.
In production
- Extraction endpoints ship flat optional-rich schemas and exceed compliance budgets.
- Tool-call schemas keep required args minimal for higher tool-invoke success.
- Enums feed typed UIs and routing without runtime string parsing.
Best practices
- Default optional; require only what breaks if missing.
- One level of nesting max for high-compliance tasks.
- Include an explicit unknown/other enum branch.
- Track compliance percent per schema in the eval suite.
Common mistakes
- Mirroring the DB schema verbatim (wide, deep, all-required).
- Rejecting valid model nuances because enums are too narrow.
- Never remeasuring compliance as data evolves.
Common issues
Near-universal enum violationsEnum doesn't cover real outputs
Sample actual outputs and extend/reshape the enum.
Required fields spammed with placeholdersUnfillable requirement
Make it optional and nullable; log absence instead of forcing lies.
Debugging tips
- Compliance drops after data change → enum or required set is stale; revisit.
- Models fill fake values for required fields → they prefer a lie to nothing; make it optional + nullable.
- Nested failures localize → too deep; flatten.
Interview checkpoint
Hands-on lab
- Write a schema as required fields plus types
- Validate outer and nested objects with one checker
- Catch missing keys before type mismatches
Nested Schema Validation by Hand
30 min · intermediate
Quick quiz
Weighing compliance, the better default is…
Models prefer fabricating a required value to leaving a field null.