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
- Messy text + schema → Model in JSON or tool mode
- Model in JSON or tool mode → Validate in code
- Validate in code → ok → Save
- Validate in code → fails → Retry once with the error
- 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
- Design an automation that reads supplier invoices from email and enters them into accounting software.
- The AI in your invoice automation sometimes reads the total wrong. How do you stop wrong amounts reaching the accounts?
- An API returns JSON in a different shape from what the next app needs. How do you handle it?