Introduction
Once your prompt contracts (Part 1), decoding (Part 2), style controls (Part 3), and context shaping (Part 4) are in place, the last line of defense is safety enforcement. Validators are the mechanical gatekeepers that guarantee outputs satisfy policy, structure, and jurisdictional constraints before they become user-visible or trigger actions. In this article we define a compact validator architecture, show how to encode policy as data, explain “repair small” strategies that avoid costly retries, and outline the metrics and incident playbooks that keep risk low without slowing teams down.
Why Validators Exist
Generative routes fail in predictable ways: malformed JSON, banned claims (“guaranteed results”), stale citations, locale mix-ups, and—most expensive—implied write actions (“I issued your refund”) with no execution record. Validators reduce these to a handful of deterministic checks. They return machine-readable error codes, enabling targeted repairs instead of whole-document resampling. The net effect: higher first-pass acceptance (CPR), lower latency, and fewer incidents.
The Validator Stack (four layers)
Shape & Schema
Parseability (valid JSON/HTML/Markdown subsets)
Required fields present; enums/ranges respected
Section counts, bullet counts, sentence caps
Lexicon & Tone
Banned terms/phrases; hedge phrases required where uncertainty remains
Brand casing and style constraints (e.g., sentence length ceilings, paragraph counts)
Channel rules (subject/preheader lengths, emoji policy, hashtags)
Evidence & Freshness (when grounded)
Citation coverage for factual sentences (e.g., ≥70%)
Claim freshness within policy window (e.g., ≤540 days unless “evergreen”)
Conflict surfacing when claims disagree (show both with dates or abstain)
Action Safety
Implied-write detector: blocks language asserting state change without a recorded tool result
Tool whitelist, argument validation, jurisdiction/tenant limits
Spend/rate/budget checks; idempotency requirements
Each layer yields specific error codes (e.g., SCHEMA, LEXICON, CITATION, STALE_CLAIM, IMPLIED_WRITE, LOCALE, LENGTH), making downstream remediation simple.
Policy as Data (not prose)
Centralize rules in a policy bundle the validators read and the prompt references. Example (abridged):
{
"contract_version": "brand.v3.2.1",
"ban": ["guarantee", "only solution", "revolutionary"],
"hedge": ["may", "typically", "in many cases"],
"comparatives": { "allow": ["faster than baseline"], "forbid": ["#1","best"] },
"brand_casing": [["Product X","Product X"]],
"citations": { "min_coverage": 0.7, "max_age_days": 540 },
"locale": "en-US",
"channel": { "email_subject_max": 52, "preheader_max": 90 },
"writes": { "forbid_implied_success": true }
}
Because the bundle is versioned, audits can reproduce the exact rule set used for any output, and legal/brand can edit rules without touching code.
Repair Small: Deterministic Fixes Before Resample
Resampling entire outputs is slow and expensive. Prefer section-level and token-light repairs:
Lexicon repair: deterministic substitutions for banned phrases; sentence trimming to caps.
Style repair: enforce casing, inject a required hedge word, split long sentences.
Evidence repair: swap a stale claim for a fresher equivalent; add a citation ID; hedge or remove unsupported numbers.
Action repair: replace implied success with a proposal (“I can issue a $25 credit—shall I proceed?”) or append the recorded tool result ID.
If a section fails the second pass, then tighten decoding for that section only (lower top-p/temperature) and try once more. On a third failure, emit a targeted ASK_FOR_MORE or REFUSE with the specific missing/forbidden condition.
Implementation Pattern
1) Contract hooks
Enumerate required sections/fields and stop tokens.
Point to
policy_versionand requiredcitation_coverage.Forbid success language; require tool proposals with idempotency keys.
2) Validator services
Stateless functions per layer, returning
{ok, errors:[{code, span, details}]}.Compose into a pipeline; stop early on
SCHEMA/IMPLIED_WRITE.
3) Repair engine
Map error codes → ordered rewrite steps (substitute, trim, inject hedge, swap claim).
Re-run only the failing layer(s); re-validate; cap attempts (e.g., 2).
4) Observability

Join the conversation! Your thoughts help the community grow.