Deep dive 27 of 27: Output Guardrails: Schema Enforcement
Nov 19, 2026 · Michael Chang · Status in NurseOps Now: ✅ built
Show definitions in ( ) · hover a highlighted word for its meaning, click it for details · open with ?defs=1 to show definitions
Part of: Remembering the right things: state management in NurseOps Now, section Preventing Drift and Hallucinations in Long-Running Agents (7.4) · All 27 solutions · ← Process Control: Self-Correction Loop
Read in this order
- In plain words: what it is, without jargon
- The problem it solves: the problem, and the standard approach in my words
- How to implement it (ServiceApps): scenario, steps, code and real output
- In NurseOps Now: built or not, and where
- Trade-offs and Interview answer
In plain words
Schema enforcement (requiring the AI's answer to match a fixed shape, checked in code) requires the AI's answer to match a fixed structure: specific fields, types and allowed values. Code validates it; if it doesn't fit, it's rejected or retried. Downstream code can then trust the shape of every answer.
In plain terms: a form with drop-down menus: you can't type "urgent!!" where the form needs a priority from 1 to 4.
The problem it solves
Hallucination (The Grounding Failure) (the AI stating made-up facts when the answer isn't in front of it): free-form answers can invent fields, values or formats that break the code that uses them.
The standard approach (in my words): use libraries like Pydantic (a library that checks data fits a fixed form) or Instructor to force the output into a strict structure, and retry when validation fails.
How to implement it (ServiceApps)
Illustrative example: no ServiceApps (a company platform for IT tickets, HR requests and help-center articles) instance is connected, so the scenario and data are made up. The code is a self-contained sketch; it was run on that made-up data on Nov 19, 2026, and the output below is from that run.
The setting. ServiceApps (a company platform for IT tickets, HR requests and help-center articles) Assist, an AI helper inside a company's ServiceApps (a company platform for IT tickets, HR requests and help-center articles) portal. Employees ask it IT and HR questions; it answers from knowledge-base (KB (knowledge base: a library of help articles)) articles, looks up the employee's own tickets (incidents (a ServiceApps ticket for something broken) like INC0012345) and requests, and keeps one conversation per employee and ticket.
The scenario. ServiceApps (a company platform for IT tickets, HR requests and help-center articles) Assist triages tickets into category, priority, KB articles (one help-center article in a knowledge base, with a number like KB0010234) and a reply. Three model outputs are validated.
Steps.
- Define the schema (a fixed form the AI's answer must fit): allowed categories, a priority range, a KB (knowledge base: a library of help articles) number format, reply length.
- Ask the model for JSON (a simple text format for structured data) in that schema (a fixed form the AI's answer must fit) (many APIs can enforce it directly).
- Validate in code; reject or retry on failure.
- Remember: a valid shape isn't a true answer; still check the facts.
The code (Python):
from typing import Annotated, Literal
from pydantic import BaseModel, Field, ValidationError, StringConstraints
KBNumber = Annotated[str, StringConstraints(pattern=r"^KB\d{7}$")]
class TicketTriage(BaseModel):
category: Literal["network", "hardware", "software", "access", "hr"]
priority: int = Field(ge=1, le=4)
kb_articles: list[KBNumber]
reply: str = Field(min_length=10, max_length=400)
outputs = ['{"category": "network", "priority": 2, "kb_articles": ["KB0010234"], "reply": "Avoid guest Wi-Fi and restart the VPN client."}',
'{"category": "urgent!!", "priority": 9, "kb_articles": ["see the VPN doc"], "reply": "ok"}',
'Sure! The category is network and the priority is high.']
for raw in outputs:
try:
t = TicketTriage.model_validate_json(raw)
print("VALID ", t.category, t.priority, t.kb_articles)
except ValidationError as e:
print("INVALID ", e.error_count(), "errors in:", [str(er["loc"][0]) if er["loc"] else "json" for er in e.errors()])
Output (from running the code above):
VALID network 2 ['KB0010234'] INVALID 4 errors in: ['category', 'priority', 'kb_articles', 'reply'] INVALID 1 errors in: ['json']
What to watch for.
- A schema (a fixed form the AI's answer must fit) guarantees shape, not truth; pair it with source checks.
- Prefer the API's built-in structured output over parsing free text.
- Keep allowed values in one place, shared by the schema (a fixed form the AI's answer must fit) and the code.
In plain terms: the assistant must fill in a strict form; anything that doesn't fit the boxes is sent back.
In NurseOps Now
Built: every case review must match the ReviewResult Pydantic (a library that checks data fits a fixed form) schema (a fixed form the AI's answer must fit), requested through the Anthropic SDK's structured output (messages.parse) in backend/app/llm/client.py; a refusal or unparseable answer falls back to the backup model or returns "AI unavailable". See Step 11.
Trade-offs
- Strict schemas (a fixed form the AI's answer must fit) make code simple and safe, but can push the model into filling fields it isn't sure about; allow "unclear".
- Schema (a fixed form the AI's answer must fit) changes need prompt (the instructions and text given to the AI) and code changes together.
Interview answer
"Every structured answer is validated against a Pydantic (a library that checks data fits a fixed form) schema (a fixed form the AI's answer must fit) with allowed values and formats, ideally enforced by the API's structured output, with a retry on failure. It makes the output safe for code, and I'm explicit that it guarantees shape, not truth; grounding checks handle truth."
All data in this post is made up. No real people, tickets or patient data.
← Back to all 27 solutions in the state management blog
Key terms
Every highlighted word in this post links here. Click ↩ Back to return to the spot you came from.
- hallucination (the grounding failure)
- The AI stating made-up facts when the answer isn't in front of it.Used in: The problem it solves↩ Back to where you were
- incident
- A ServiceApps ticket for something broken.Used in: How to implement it (ServiceApps)↩ Back to where you were
- JSON
- A simple text format for structured data.Used in: How to implement it (ServiceApps)↩ Back to where you were
- KB
- Knowledge base: a library of help articles.Used in: How to implement it (ServiceApps)↩ Back to where you were
- KB article
- One help-center article in a knowledge base, with a number like KB0010234.Used in: How to implement it (ServiceApps)↩ Back to where you were
- prompt
- The instructions and text given to the AI.Used in: Trade-offs↩ Back to where you were
- Pydantic
- A library that checks data fits a fixed form.Used in: The problem it solves, In NurseOps Now, Interview answer↩ Back to where you were
- schema
- A fixed form the AI's answer must fit.Used in: How to implement it (ServiceApps), In NurseOps Now, Trade-offs, Interview answer↩ Back to where you were
- schema enforcement
- Requiring the AI's answer to match a fixed shape, checked in code.Used in: In plain words↩ Back to where you were
- ServiceApps
- A company platform for IT tickets, HR requests and help-center articles.Used in: How to implement it (ServiceApps)↩ Back to where you were