▶ Sign up free for the video, slides & prompts
The lesson below is open to all. Enter your email once to unlock every video, slide deck, prompt and code example on this device.
Checking…
AI Agents in Production · Lesson 4 of 14 · 1 min read · 5 min video
Structured outputs and guardrails
Force JSON that validates, and check inputs and outputs before anything acts.
📄 Download slides (PDF)· free sign-upFree text is fine for a human to read, but code can't trust it. When an agent's output drives an action, you need structured outputs you can check, and guardrails that run before anything happens.
Structured outputs
Ask the model for JSON that matches a schema, then validate it in code:
If validation fails, send the error back to the model and let it try again, once or twice. Then fail clearly.
Guardrails in layers
Check at three points:
- Input: is the request in scope? Strip obviously malicious content and block personal data you shouldn't process
- Action: before a tool runs, check business rules in code: refund ≤ ₹5,000, only the customer's own orders, no emails to external domains
- Output: before replying, check for leaked secrets, made-up order numbers and promises the business can't keep
Rules belong in code, not prompts
"Never refund more than ₹5,000" in a prompt is a polite request. The same rule in code is a guarantee. Use prompts to guide the model and code to enforce limits.
if statement, not a sentence in the prompt.In one line
Make the model return validated structures, and enforce every important rule in code at the input, action and output, because a prompt is a request but code is a guarantee.
Share with a friend