Sub topic 9
Redaction and pseudonymization before you prompt
This chapter assumes you have read the earlier chapters on data classification and data minimization. You should know which fields in your records are sensitive and that the first step is always to send less. Redaction and pseudonymization are what you do with the data that remains.
Two techniques, two purposes
Redaction removes a value and puts a placeholder in its place. The original is gone from the text you send, and you do not plan to get it back. Use it for details the AI tool does not need, such as an email address in a support ticket you want summarized.
Pseudonymization replaces a real value with a consistent token, such as CUSTOMER_1. You keep a mapping between tokens and real values inside your organization. After the AI tool replies, you swap the tokens back. Use it when the AI needs to tell people apart, for example when a thread mentions two customers and the answer must say which one gets the refund.
| Technique | What happens to the original | Reversible | Typical use |
|---|---|---|---|
| Redaction | Removed from the text | No | Contact details the task does not need |
| Pseudonymization | Replaced by a token, mapping kept locally | Yes, with the mapping | Names and IDs the answer must refer to |
Pseudonymized data is still personal data under many laws, because the mapping can re-identify people. Treat the mapping as sensitive. Store it with the same care as the original records, and never send it to the AI tool.
Redacting with patterns
Patterns work well for values with a predictable shape, such as email addresses and phone numbers.
import re
EMAIL = re.compile(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}")
PHONE = re.compile(r"\+?\d[\d\s().-]{7,}\d")
def redact(text: str) -> str:
text = EMAIL.sub("[EMAIL]", text)
return PHONE.sub("[PHONE]", text)
print(redact("Contact Ana at [email protected] or +44 20 7946 0958."))
# Contact Ana at [EMAIL] or [PHONE].Notice what the output still contains: the name "Ana". The patterns cannot recognize names, street addresses, account nicknames, or an order note such as "the woman from the Leeds bakery". Pattern redaction catches the easy cases. A person must still read the text before it goes out.
Pseudonymizing with a local mapping
The second example swaps names for tokens, then restores them after the reply.
def pseudonymize(text: str, names: list[str]) -> tuple[str, dict[str, str]]:
mapping = {}
for i, name in enumerate(names, start=1):
token = f"CUSTOMER_{i}"
mapping[token] = name
text = text.replace(name, token)
return text, mapping
masked, mapping = pseudonymize(
"Ana Diaz reported a billing error. Ana Diaz wants a refund.",
["Ana Diaz"],
)
print(masked)
# CUSTOMER_1 reported a billing error. CUSTOMER_1 wants a refund.
reply = "Please apologize to CUSTOMER_1 and issue the refund."
for token, name in mapping.items():
reply = reply.replace(token, name)
print(reply)
# Please apologize to Ana Diaz and issue the refund.The same name always receives the same token, so the AI can follow the story. The mapping variable never leaves your machine.
Common errors
- Partial matches. The code in the example replaces exact strings. "Ana" or "Ms. Diaz" on its own is not caught, and "ANA DIAZ" is not caught either. List every variant you know of.
- Replacement order. If one name contains another, such as "Ana" and "Ana Diaz", replace the longer name first. Otherwise you leave a fragment behind.
- Indirect identifiers. A job title, a small town, and a date can single out a person even when the name is gone. Remove details the task does not need.
- Tokens that leak meaning. A token like
VIP_CUSTOMER_AT_ACMEreveals what you meant to hide. Use neutral tokens. - Trusting the output blindly. The AI may rewrite or drop a token in its reply. Check that every token restored correctly before you send anything to a customer.
Exercise
A colleague redacts a support ticket with the email and phone patterns above and pastes it into an AI tool. The ticket still reads "Hi, this is Marco Rossi from Rossi Plumbing in Leeds." What went wrong, and what two steps would fix it?
Show answer
The patterns only match emails and phone numbers, so the name, company, and town passed through. First, pseudonymize or remove "Marco Rossi" and "Rossi Plumbing" (and consider dropping "Leeds" if the task does not need it). Second, read the masked text before sending it, because pattern matching cannot find names or indirect identifiers.
Exercise
You pseudonymize a thread with two customers, "Ana Diaz" and "Ana Diaz-Lopez", using the function above with the list ["Ana Diaz", "Ana Diaz-Lopez"]. What goes wrong, and how do you avoid it?
Show answer
Replacing "Ana Diaz" first turns "Ana Diaz-Lopez" into "CUSTOMER_1-Lopez", which leaves a real surname fragment in the text and merges two people into one token. Sort the names from longest to shortest before replacing, so "Ana Diaz-Lopez" is handled first.