Sub topic 5
Detecting and Redacting Personal Data
This chapter assumes you can write Python functions and regular expressions, and that you have seen where input and output checks sit around a model call. Here you build a deterministic check for personal data (PII, personally identifiable information) and learn where it breaks.
Why PII needs its own check
Users paste emails, phone numbers and ID numbers into prompts. If that text reaches the model, it may be logged by a provider, stored in your own traces, or repeated in a later answer. Redacting before the model call removes the data from every downstream place. The model rarely needs the real value to do its job. It needs to know that an email was present.
Redact with placeholders
Redaction replaces each match with a typed placeholder such as [EMAIL]. A typed placeholder keeps the sentence readable, so the model can still answer "send the report to [EMAIL]" sensibly.
import re
EMAIL = re.compile(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}")
US_SSN = re.compile(r"\b\d{3}-\d{2}-\d{4}\b")
def redact_pii(text: str) -> str:
text = EMAIL.sub("[EMAIL]", text)
return US_SSN.sub("[SSN]", text)
print(redact_pii("Mail [email protected], SSN 123-45-6789"))
# Mail [EMAIL], SSN [SSN]Each pattern has a job. The email pattern matches a local part, an @, a domain and a top-level domain of at least two letters. The SSN pattern matches three digits, two digits and four digits separated by hyphens. The \b word boundaries stop it from matching inside a longer number such as 9123-45-67890.
Where redaction runs
Redact on the input path to protect what the model and your logs see. Run the same check on the output path as well, because the model can produce PII on its own. It may repeat data from retrieved documents or from earlier turns of the conversation.
Gotchas
Regular expressions find data by shape, not by meaning. That produces errors in both directions.
- False positives: an order number formatted like
123-45-6789is redacted even though it is not an SSN. The model loses a value it may have needed. - False negatives:
jane at example dot comand123 45 6789pass through untouched. Names and street addresses have no fixed shape at all, so these patterns never catch them. - Order of rules: if one pattern's placeholder could match another pattern, results depend on the order. Keep placeholders free of digits and
@. - Format variety: phone numbers differ by country. A pattern that covers one format silently ignores the rest.
Add a validity test where one exists. For card numbers, match 13 to 19 digits and then confirm the Luhn checksum before redacting. This removes most false positives from random digit strings.
For names, addresses and free-form identifiers, a named-entity recognition (NER) model is the usual next step. It is a model-based check, and it costs more time and still makes mistakes. The classifier chapters cover that trade-off.
Redaction versus blocking
Redacting is not always right. If the user's task cannot work without the data, a placeholder produces a useless answer. Then you can block the request with a clear message, or ask the user to confirm. Choose per use case and per data type. The later chapter on failure handling covers this choice.
Never log the original text next to the redacted text. That puts the PII back into your logs and defeats the check.
Exercise
A user writes "My SSN is 123 45 6789 and my email is [email protected]". With the redact_pii function above, what text goes to the model, and what does this show about regex-based redaction?
Show answer
The model receives "My SSN is 123 45 6789 and my email is [EMAIL]". The SSN uses spaces instead of hyphens, so US_SSN does not match it. This shows that pattern-based redaction misses variants of a format (false negatives). Fixes include allowing separators with a pattern like \b\d{3}[- ]\d{2}[- ]\d{4}\b, which in turn raises the false-positive risk, or adding an NER model as another layer.
Exercise
Why should redaction run on the model's output as well as on the user's input?
Show answer
The model can produce personal data that the user never typed, for example by repeating it from retrieved documents or earlier conversation turns. An input-only check would not catch it.