Skip to content
AgenticAIWiki
  1. Learn
  2. Part 12
  3. 64. Guardrails in depth
  4. 64.3

Lesson 64.3 · 3 of 11 · 4 min read · Reviewed 24 Nov 2026

Output filtering and validation

By the end, you can

  • Check model outputs before they reach users or systems.
  • Combine schema checks, content checks and grounding checks.

The model’s output is the last thing between your system and the world. Check it.

Structure

Schema validation and business rules (14.4). Totals add up, dates are valid, enums are known.

Content

  • Personal dataPII: Personally identifiable information: data that can identify a person, such as name, email, address or ID numbers. that shouldn’t be shown, such as another customer’s details.
  • Unsafe or off-brand content.
  • Commitments the business must approve: refunds, discounts, legal or medical claims.

Grounding

For answers based on documents, check that claims are supported: ask the model to cite sources, then verify that the cited passages exist and say what’s claimed. A second, cheap model call can grade support (chapter 18).

What to do on a failure

Retry with feedback, fall back to a safe template (“Let me check that with a colleague”), or route to review. Never silently send a reply that failed a check.

Key points

  • Validate structure: does the output match the schema and business rules?
  • Check content: personal data, unsafe content, promises the business can't keep.
  • Check grounding: are claims supported by the sources the model was given?
Hands-on labRuns in the Playground

Validate a reply before sending it

Check a model's reply for structure, content and grounding before it reaches a customer.

You will have: A check_reply() function that labels each problem by type, run over five sample replies.

Steps

  1. Run the starter: all five replies would be sent.
  2. Parse the JSON and check required keys and that the total adds up.
  3. Check that every cited quote really appears in its source.
  4. Hold replies that promise a refund nobody approved.

Done when

  • Only the first reply is sent.
  • Each held reply names at least one problem and its type: structure, content or grounding.

Open in the Playground No API key needed.

Starter code (Python)
import json
SOURCES = {"doc1": "Standard delivery takes 3 to 5 working days.", "doc2": "Returns are free within 30 days."}
REPLIES = [
    '{"text": "Delivery takes 3 to 5 working days.", "citations": [{"doc": "doc1", "quote": "3 to 5 working days"}], "items": [10, 15], "total": 25}',
    '{"text": "Returns are free for 90 days.", "citations": [{"doc": "doc2", "quote": "free within 90 days"}], "items": [], "total": 0}',
    '{"text": "Your order total is 40.", "citations": [], "items": [10, 15], "total": 40}',
    '{"text": "We will refund you double.", "citations": [], "items": [], "total": 0}',
    'Sure! Here is your answer.',
]

def check_reply(raw):
    # TODO: return a list of problems: not JSON, missing keys, total != sum(items),
    #       a citation quote not found in its source, or a promise like "refund" that needs approval
    return []

for r in REPLIES:
    problems = check_reply(r)
    print("SEND" if not problems else "HOLD", problems)
Show a solution
import json
SOURCES = {"doc1": "Standard delivery takes 3 to 5 working days.", "doc2": "Returns are free within 30 days."}
REPLIES = [
    '{"text": "Delivery takes 3 to 5 working days.", "citations": [{"doc": "doc1", "quote": "3 to 5 working days"}], "items": [10, 15], "total": 25}',
    '{"text": "Returns are free for 90 days.", "citations": [{"doc": "doc2", "quote": "free within 90 days"}], "items": [], "total": 0}',
    '{"text": "Your order total is 40.", "citations": [], "items": [10, 15], "total": 40}',
    '{"text": "We will refund you double.", "citations": [], "items": [], "total": 0}',
    'Sure! Here is your answer.',
]
KEYS = {"text", "citations", "items", "total"}

def check_reply(raw):
    try:
        r = json.loads(raw)
    except ValueError:
        return ["structure: not JSON"]
    missing = KEYS - set(r)
    if missing:
        return [f"structure: missing {sorted(missing)}"]
    problems = []
    if sum(r["items"]) != r["total"]:
        problems.append("structure: total does not add up")
    for c in r["citations"]:
        if c["quote"] not in SOURCES.get(c["doc"], ""):
            problems.append(f"grounding: quote not in {c['doc']}")
    if "refund" in r["text"].lower():
        problems.append("content: refund promise needs approval")
    return problems

held = 0
for r in REPLIES:
    problems = check_reply(r)
    held += bool(problems)
    print("SEND" if not problems else "HOLD", problems)
print(f"held {held} of {len(REPLIES)} replies")
Quick checkA support bot sometimes promises refunds that policy doesn't allow. Which output check fits?
How sure are you?
Pick an answer and how sure you are