PII Detection & Handling
Privacy you can prove
Personally identifiable information needs detection, redaction, and data-governance rules: mask on input, strip on output, and keep PII out of model context and training. Detection quality decides whether privacy is real or theater.
What you will learn
- Detect PII classes reliably
- Mask on input, strip on output
- Govern PII in context and logs
What is this?
PII handling means recognizing identifiable data (names, emails, addresses, IDs, phone) with detectors, redacting/masking it before it reaches context or logs, and stripping it from responses. Detection quality is the crux: regex + entity models + feedback loops, measured against labeled test sets.
Why do you need it?
- Leakage to logs or training is a compliance and trust failure.
- Masking keeps context useful while removing identity.
- Stripped output prevents accidental disclosure to users.
- Measured detection keeps the practice honest.
Mental model
A hospital's patient chart system
PII handling
Names appear masked ('Patient #7') in shared rounds; charts travel only with authorization; disposal shreds identity. The system still works clinically (masked context) without leaking who is who. Your LLM pipeline needs those same walls.
How it works
- Detect: regex lists + NER model for PII classes.
- Mask on input: replace tokens with placeholders (preserving structure).
- Context rule: allow masked form only in prompts.
- Strip on output: scan final text; redact or refuse.
- Govern logs: no raw PII; retention and access rules.
PII lifecycle.
In production
- Customer support routes mask names/emails before model calls.
- Healthcare/finance pipelines redact on chat and export.
- Eval datasets scrub PII before they ship.
Best practices
- Detect with layered tools (regex + NER) and label coverage.
- Mask preserving length/structure where the model needs it.
- Strip output regardless of whether input was masked.
- Test detection against labeled PII sets (recall-first).
Common mistakes
- Regex-only detection with 60% recall and quiet confidence.
- Masking input but leaking restored values in the prompt.
- PII parked in logs 'for now'.
Common issues
Model needs unmasked contextStructure lost in masking
Mask to controlled placeholders preserving layout.
PII in eval/training dataDataset not scrubbed
Run scrubbing pipeline + regression cases.
Debugging tips
- Missed detection → label set inspection; add entity patterns.
- Over-masking breaks the task → context-aware rules.
- PII reappears in output → restoration step leaking; strip hard.
Interview checkpoint
Quick quiz
The strongest PII posture is…
Regex alone reliably covers all PII classes.