Structured outputs and validation
- Agents
- Asked in most interview loops
- About 75 min to learn
- 4 vetted resources
What this topic covers
Production code needs the model to return data a program can parse, not prose. There are three escalating guarantees: JSON mode (valid JSON, any shape), schema-constrained decoding (the output must match your JSON Schema), and function calling with strict schemas (the model fills tool arguments). Even with constrained decoding you still validate outside the model with Pydantic because schemas cannot express business rules, and you need a retry loop that feeds validation errors back to the model. Interviewers ask you to pick between the three mechanisms, explain what strict schemas forbid (optional fields, open objects, some type keywords), handle refusals and truncation, and show how you would stream a partially filled object to a UI.
You’re ready when you can
What interviewers check, from the resources below.
- Compare JSON mode, schema-constrained outputs and function calling, and say which guarantees schema adherence
- Decide which mechanism to use when the model must call your code versus answer the user in a fixed shape
- List three JSON Schema features the strict subset forbids and how you work around each
- Explain how a refusal or a length cutoff shows up and how your code should handle it
- Describe how streaming a structured response differs from streaming text
- Explain the difference between constraining the response body and constraining tool inputs
- Explain why a Pydantic model is turned into a schema and what the SDK changes to make it strict
- Name the cases where constrained decoding still yields an unusable result
- Explain why the first call with a new schema is slower than later ones
- Implement a Pydantic model with a field validator and a loop that re-prompts with the validation error up to N times
What to study, in order
Each link opened and checked by hand; the date says when.
Start here
Structured model outputs (opens in a new tab)
OpenAI Developers · OpenAI · 35 min · Free · checked 21 Oct 2026
- Why
- The clearest statement of the three guarantee levels (JSON mode, schema-constrained output, function calling) plus the edge cases interviewers love: refusals, truncation and streaming.
- What to read
- Read “When to use Structured Outputs via function calling vs via text.format”, “Structured Outputs vs JSON mode”, “Step 3: Handle edge cases”, “Refusals with Structured Outputs”, “Streaming”, “Supported schemas” and “JSON mode”.
- What to skip
- The “Examples” gallery (UI generation, moderation) and the per-language SDK snippets beyond the one you use.
Free alternative
Structured outputs (opens in a new tab)
Claude Docs · Anthropic · 25 min · Free · checked 21 Oct 2026
- Why
- Shows the same two-mechanism split (JSON output format vs strict tool use) from a second vendor, with explicit schema limits and the cost of grammar compilation, so your answer is not tied to one API.
- What to read
- Read “Why use structured outputs”, “How it works”, “Usage” (your language tab only), “Strict tool use”, then under “Important considerations” read “Grammar compilation and caching”, “JSON Schema limitations” and “Invalid outputs”.
- What to skip
- “Migrating from the beta”, “Data retention”, “Feature compatibility” and the schema complexity limit tables.
Go deeper
Enhancing AI Validations with Pydantic's Framework (opens in a new tab)
Instructor docs · Jason Liu · 30 min · Free · checked 21 Oct 2026
- Why
- Schemas cannot express business rules; this is the standard pattern for validating in Python and feeding the error back to the model, which is what interviewers mean by “what happens when the output is wrong”.
- What to read
- Read “Validation and Reasking”, “Code-based Validation Example”, “Using Reasking Logic to Correct Outputs” (steps 1 and 2) and “What happens behind the scenes?”. Then open “Streaming Partial Responses” (linked under “See Also”) and read “Understanding Partial Responses”.
- What to skip
- “LLM-Based Validation Example”, “Using Context for Dynamic Validation” and “Optimizing Token usage”.
Go deeper
Outlines (opens in a new tab)
GitHub · dottxt-ai · 45 min · Free · checked 22 Oct 2026
- Why
- Outlines makes the model produce valid structure during decoding by masking logits, rather than parsing and retrying afterwards. It’s the usual reference when an interviewer asks how structured outputs work on open models.
- What to read
- Run the README “Quickstart” with a small transformers model: a Literal classification, an int, then a Pydantic model. Read docs/features/advanced/backends.md (outlines-core, llguidance, xgrammar and which is the default for JSON schema, regex and grammars). Then read src/outlines/backends/outlines_core.py: OutlinesCoreLogitsProcessor.process_logits and the JSON-schema and regex processor builders.
- What to skip
- Skip the long “Real-world examples” section after the first one, and the API-provider integrations (OpenAI, Gemini) where the provider does the constraining.
Companies that test Agents most
Put Structured outputs and validation in your plan
Tell us your interview date and hours a day; it lands on the right day, in the right order.