Output Filtering
The last gate before delivery
Output filters police what leaves the system: toxicity, unsafe content, leaked private data, and rule-breaking claims. They run after generation and before delivery — the last line of policy defense.
What you will learn
- Filter toxic and unsafe output
- Detect leaked private data in output
- Handle filtered outputs gracefully
What is this?
Output filtering runs after generation: moderation classifiers for toxicity, scanners for PII and secrets, schema/format validators, and even factuality spot-checks. Filtered content is either reparsed, regenerated, or replaced with a safe fallback rather than delivered raw.
Why do you need it?
- Generation can fail even under good prompts.
- Leaked data leaves the system only at delivery — catch it before.
- Output checks are testable policy, independent of the model.
- Regulatory requirements often call for them explicitly.
Mental model
Loading inspection before shipping
Output filter
Every parcel leaves the warehouse through the scanner: labeled correctly (format), contains no prohibited goods (content), addresses right (PII). A bad parcel gets re-routed or rejected here — nothing ships off-policy.
How it works
- Run moderation classifier on generated text.
- Scan for PII/secrets/patterns on the way out.
- Validate format/schema compliance.
- On failure: sanitize, regenerate once, or fallback.
- Log the decision and outcome.
Post-generation gate.
In production
- Public chat filters profanity and harassment on output.
- Enterprise apps strip PII before export.
- Finance tools reject unsupported compliance claims.
Best practices
- Classify the failure: moderate, sanitize, regenerate, or refuse.
- Design fallbacks so the filter doesn't 'hang' the user.
- Measure false positive + miss rates; tune thresholds.
- Log filter actions for abuse analysis.
Common mistakes
- Delivering flagged content anyway ('it only fired sometimes').
- No fallback — legit queries get blank walls.
- Filtering only the happy path, missing streaming overs.
Common issues
Filtered output blocks legit useThreshold too tight
Tune on traffic; add severity-based handling.
Regeneration loops on filtered outputNo fallback logic
Regen once, then fixed safe reply.
Debugging tips
- Users see barren replies → false-positive falls in the classifier; tune + fallback.
- PII slips once → scanner gap; add rule + regression test.
- Stream shows leaks before filter → check streaming path filters too.
Interview checkpoint
Hands-on lab
- Detect emails and long digit runs in output
- Replace matches with a redaction token
- Verify no raw PII leaks after redaction
Redact Sensitive Output
30 min · intermediate
Quick quiz
Output filtering happens…
A filter should always deliver legally-flagged content.