Search⌘ K
AI Features

Add a Self-Check That Fails Safe

Explore how to add inline self-checks in AI workflow prompts that detect schema and enum violations, flipping fail-safe flags to ensure outputs remain valid. Learn prompt layout strategies that improve reliability and how to prevent silent failures by forcing human review when outputs are uncertain or invalid. This lesson teaches how to produce JSON outputs that safely guide routing decisions and can be applied across different structured tasks.

Syntactically valid JSON can still paralyze an automated workflow if a single value drifts outside your allowed enums. For instance, if an AI returns category: "payments_issue" instead of the required "billing", your router rules will fail to match any branch, dumping the ticket into a default queue without throwing an explicit error. To prevent these silent failures, your prompt needs an inline self-check that audits draft decisions before finalizing output. In this lesson, we will explore how to write self-checking prompts that flip explicit fail-safe flags whenever uncertainty or schema violations occur.

To see how this failure surfaces, inspect a few triage outputs and note which fields violate the schema and which routing rule would fail to match:

Triage JSON fails schema checks and falls to default routing, until a self-check catches it and forces a safe escalation
Triage JSON fails schema checks and falls to default routing, until a self-check catches it and forces a safe escalation

Start with the raw output a model produced for one ticket:

{
"category": "payments_issue",
"priority": 5,
"next_action": "reply"
}

Note: needs_human is missing entirely.

Running that JSON through the schema catches three separate problems at once:

Schema Check Failures
ViolationFieldDetail

Enum mismatch

category

must be one of: billing, bug, account, how_to, other

Wrong type

priority

must be a string (low, normal, high), not a number

Missing key

needs_human

required (boolean)

Do you find this helpful?

None of those problems throw an error on their own, so the next question is what happens when the router actually tries to act on this JSON. ...

Router Rules Against the Bad JSON
RuleMatch conditionResult

Rule 1: Auto-reply

category in allowed set and needs_human == false

No match

Rule 2: Known priority

priority == "high"

No match

Rule 3: Known category

category in {billing, bug, account, how_to, other}

No match

Do you find this helpful?

...