
Anonymize Before You Index: Protecting Personal Data in a Legal RAG System
Legal documents are full of names, phone numbers and IDs. Here is why personal data should be removed before it reaches your search index, with a simple Python example.
Legal records are some of the most sensitive text you can build an AI
system on. Names, addresses, phone numbers and ID numbers appear on
almost every page. When I built NyayaGPT, a multilingual legal assistant
over 34,000+ Indian legal records, PII anonymization became a core part
of the design, not an extra. This article explains why, and shows a
simple way to start.
The risk in one sentence
If personal data reaches your search index, it can be retrieved, shown in
answers, and copied into logs, and removing it afterward is hard.
Why anonymize before indexing
Your RAG pipeline copies text in many places:
- the vector index stores chunks and their embeddings
- the keyword index stores the raw words
- the prompt sends retrieved passages to the language model
- your logs may record questions and answers
If the original text contains personal data, every one of these can hold
a copy. If you anonymize first, none of them ever sees it. That is why
the step belongs at the start of the pipeline:
Documents -> Anonymize -> Chunk -> Embed -> IndexWhat counts as PII in legal text
- Names of parties, witnesses and family members
- Phone numbers and email addresses
- Home addresses
- Government ID numbers, such as Aadhaar or PAN
- Dates of birth and bank account details
Context matters. A judge's name or a public official's name may be part
of the legal meaning, so decide your rules deliberately.
Two approaches
- Pattern rules (regular expressions). Good for things with a fixed format, such as phone numbers, emails and ID numbers. Fast and easy to audit.
- Named entity recognition (NER). A model finds names, places and organizations in free text. Needed for names, which have no fixed pattern. It can make mistakes, so test it on your own language and documents.
Many systems combine both.
A simple starting example
This example uses pattern rules only. It is a teaching example, not a
complete solution, and it will not catch names.
import re
PATTERNS = {
"EMAIL": r"[\w.+-]+@[\w-]+\.[\w.]+",
"PHONE": r"(?<!\d)(?:\+91[\s-]?)?[6-9]\d{9}(?!\d)",
"AADHAAR": r"(?<!\d)\d{4}\s?\d{4}\s?\d{4}(?!\d)",
"PAN": r"\b[A-Z]{5}\d{4}[A-Z]\b",
}
def anonymize(text: str) -> str:
for label, pattern in PATTERNS.items():
text = re.sub(pattern, f"[{label}]", text)
return text
sample = "Contact 9876543210 or ravi@example.com. PAN: ABCDE1234F."
print(anonymize(sample))
# Contact [PHONE] or [EMAIL]. PAN: [PAN].Replacing values with labels such as
[PHONE] keeps the sentence readable
for the model while removing the real value.Test it, do not trust it
Anonymization fails quietly. Build a small check:
- Take 50 real-looking sample passages.
- Run your anonymizer.
- Read the output and search it for anything personal that remains.
- Fix the rules and repeat.
Do this for every language you support. Scripts and number formats differ,
so a rule that works on English text may miss Hindi, Tamil or Telugu
text.
Mistakes to avoid
- Anonymizing after indexing. The data is already copied.
- Forgetting the logs. Questions can contain personal details too.
- Over-trusting one method. Patterns miss names, and NER misses unusual formats.
- Removing too much. If you delete legal meaning, answers get worse. Review samples with both privacy and usefulness in mind.
Takeaways
- Anonymize at the start of the pipeline, before chunking and indexing.
- Combine pattern rules with entity recognition.
- Test per language and on real documents.
- Privacy is a design decision, so make it early.
If you work with sensitive documents in RAG, how do you handle personal
data? I would love to compare approaches in the comments.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article