Module 1.3 - Output guardrails and validation
You have just taught the model to return what you expect. This module is the other half: defending against the times it does not. In production it will sometimes return malformed JSON, a field you did not ask for, an out-of-range value, or content that is unsafe to pass downstream. Guardrails are the checks that sit between the model's output and everything that consumes it.
The layers of validation
Think in three layers. Schema validation: does the output match the shape you required? Semantic validation: is the content within the bounds that make sense (a confidence score between 0 and 1, a category from your allowed set)? Safety validation: is this output safe to act on (no leaked secrets, no injected instructions, nothing that would harm a user if displayed or executed)?
The retry pattern
When validation fails, you do not crash and you do not pass the bad output through. You handle it: retry the call (often with the error fed back to the model so it can correct), fall back to a safe default, or fail loudly in a controlled way. A production endpoint has a plan for the bad case, always. The naive version passes whatever the model said straight into the next step; the engineered version treats every model output as untrusted until checked.
The same muscle, both directions
Structured outputs and guardrails are two sides of one skill. One shapes the output you want; the other defends against the output you get. Strong systems do both, because even constrained outputs can surprise you - and the cost of an unchecked model response flowing into code that acts on it is exactly the kind of failure that does not show up in a demo and does show up in production.
The bridge to Week 3
This is introduced now as a day-one fundamental, but it becomes genuinely dangerous once your model can use tools. In Week 3, a malicious instruction hidden in a document the agent reads can hijack it - that is prompt injection - and the validation habits you build here are what defend against it. Learn the reflex now, on a single endpoint, so it is automatic when the stakes rise.
Watch out
Common mistakes
- Trusting a model response because it "looked fine in testing."
- Validating shape but not meaning.
- Having no plan for the failure case.
- Passing model output into a consequential action without a check.
Go deeper (optional)