Structured Output
When a model's answer needs to be used by software β not just read by a human β free-form text is a liability. Structured output means requiring the model to return data in a fixed shape (usually JSON matching a schema) so your application can validate and rely on it.

Why it matters
Consider generating a quiz from a lesson. If the model returns prose, your code has to parse it,
guess where the questions are, and hope. If it returns JSON matching a schema β
{ questions: [{ stem, options: [{ text, isCorrect }], explanation }] } β your code can:
- Validate it against the schema and reject anything malformed.
- Retry a bad generation instead of storing garbage.
- Enforce invariants in code (exactly one correct option, at least two distractors, no duplicate stems).
A malformed or hijacked generation simply fails validation and never reaches your database. This turns "hope the model behaved" into "the model's output is checked before it counts."
How to get structured output
- Ask for it explicitly in the prompt, and provide the schema or a clear example.
- Use the provider's structured-output mode when available (many can be constrained to emit valid JSON for a given schema).
- Validate on receipt β parse against the schema in code. Never trust the shape just because you asked for it.
- Retry on failure a bounded number of times, then fail closed.
Where to use it
Anywhere the output feeds logic rather than a human reader:
- Extraction (pull fields from a document).
- Classification and routing (return a label, not a paragraph).
- Generation that becomes data (quizzes, review findings, structured plans).
The principle
Structured output is the bridge between a probabilistic model and deterministic software: validation over trust. It's one of the highest-leverage reliability techniques you have, and it costs almost nothing to adopt. LyraLearn uses it for AI-generated quizzes and code-review findings β both are schema-validated, so a bad generation is rejected, not persisted.