Search Tutorials
--Ads--PII Detection and Redaction
Find and mask personal data before indexing and before answering.
Personally identifiable information (PII), such as names, phone numbers, email addresses, government ID numbers and bank details, turns up everywhere in RAG systems: in indexed tickets and emails, in users' questions, in logs and in generated answers. Data protection laws (such as India's DPDP Act, the GDPR in Europe and HIPAA for US health data) restrict how it may be processed and stored. This lesson shows how to detect PII and where to redact it.
Where PII appears
| Location | Example | Typical treatment |
|---|---|---|
| Indexed documents | Support tickets with customer phone numbers | Redact or pseudonymise before indexing, unless the PII is needed |
| User questions | "My PAN is ABCDE1234F, why was my refund delayed?" | Redact before logging; decide whether the LLM needs it |
| Logs and traces | Every question and answer | Redact, restrict access, set retention |
| Answers | An answer quoting another customer's details | Output check before display |
Detecting PII with Microsoft Presidio
Presidio is an open-source toolkit that finds PII with a combination of patterns, checksums and named-entity recognition, and then anonymises it.
pip install presidio-analyzer presidio-anonymizer
python -m spacy download en_core_web_lg
from presidio_analyzer import AnalyzerEngine, Pattern, PatternRecognizer
from presidio_anonymizer import AnonymizerEngine
from presidio_anonymizer.entities import OperatorConfig
analyzer = AnalyzerEngine()
anonymizer = AnonymizerEngine()
# Add recognisers for identifiers your data contains, e.g. Indian PAN numbers
pan = PatternRecognizer(supported_entity="IN_PAN_CUSTOM",
patterns=[Pattern("pan", r"\b[A-Z]{5}[0-9]{4}[A-Z]\b", 0.85)])
analyzer.registry.add_recognizer(pan)
ENTITIES = ["PERSON", "PHONE_NUMBER", "EMAIL_ADDRESS", "CREDIT_CARD", "IBAN_CODE",
"IP_ADDRESS", "IN_PAN_CUSTOM"]
def redact(text: str) -> str:
results = analyzer.analyze(text=text, language="en", entities=ENTITIES)
return anonymizer.anonymize(
text=text, analyzer_results=results,
operators={"DEFAULT": OperatorConfig("replace", {"new_value": "<REDACTED>"}),
"PERSON": OperatorConfig("replace", {"new_value": "<PERSON>"}),
"EMAIL_ADDRESS": OperatorConfig("replace", {"new_value": "<EMAIL>"})},
).text
print(redact("Ticket from Rahul Verma ([email protected], +91 98765 43210): PAN ABCDE1234F"))
Ticket from <PERSON> (<EMAIL>, <REDACTED>): PAN <REDACTED>
Presidio includes recognisers for many countries' identifiers; check the list for your region before writing custom patterns. Detection is never perfect: test it on a sample of your real data and look for both misses and false alarms (product names detected as people, for instance).
Redaction strategies
- Remove or mask (
<REDACTED>): simplest and safest when the value is not needed to answer questions. - Pseudonymise: replace each value with a consistent token (
<PERSON_17>), so documents remain readable ("PERSON_17 escalated the ticket; PERSON_17 later confirmed...") and, if permitted, can be mapped back through a protected lookup table. - Keep but restrict: when PII is the point (an HR assistant answering about an employee's own record), keep it, and rely on strict access control so each user can retrieve only their own data.
Where to redact in the pipeline
def index_ticket(ticket: dict) -> dict:
return {"id": ticket["id"], "text": redact(ticket["body"]), # 1. before indexing
"metadata": {"product": ticket["product"], "date": ticket["date"]}}
def handle_question(user, question: str) -> str:
log_question(redact(question)) # 2. before logging
hits = secure_search(user, embed(question, query=True)[0])
answer = generate(question, hits)
findings = analyzer.analyze(text=answer, language="en", entities=["PHONE_NUMBER", "EMAIL_ADDRESS"])
if findings: # 3. before display
answer = redact(answer)
log_answer(redact(answer))
return answer
Redacting at indexing time is the most robust choice: data that is never stored cannot leak through retrieval, caches, summaries or logs.
Beyond detection
- Data minimisation: do not index sources that are not needed for the assistant's purpose.
- Retention: delete logs and conversation histories after a defined period.
- Right to erasure: be able to find and delete all chunks, cache entries and logs related to a person, which requires good metadata.
- Provider settings: review your LLM and embedding providers' data retention and processing terms, and choose regions and options that meet your obligations.
Key takeaways
- PII appears in documents, questions, logs and answers; protect each location.
- Use a detection toolkit such as Presidio, with custom recognisers for your identifiers, and test it on real data.
- Prefer redacting before indexing; pseudonymise when readability matters.
- Combine redaction with data minimisation, retention limits and erasure procedures.