Skip to main content

Pattern · Serving and cost

Structured output with validation

Make the model return JSON that matches a schema, validate it in code, and retry once with the error.

Say it in the interview

I would use the provider's schema or tool mode, validate every field in code (types, ranges, totals that add up), retry once with the validation error, and send repeat failures to a review queue.

When to use it

  • Extraction from documents or messages
  • Any output another system reads

How it works

  1. Messy text + schema → Model in JSON or tool mode
  2. Model in JSON or tool mode → Validate in code
  3. Validate in code → ok → Save
  4. Validate in code → fails → Retry once with the error
  5. Retry once with the error → fails again → A person reviews

Trade-offs

  • Reliable machine-readable output
  • Strict schemas can reject valid edge cases

Pitfalls interviewers probe

  • Parsing free text with regex
  • Trusting the JSON without checking types and ranges

Practise it on these questions

Today's AI turns are used

Lessons, roadmaps, videos, practice cards and the code playground stay free with your account, with no limit.