← Journal
Redact Personal Information Before You Hand It Over: Running Presidio

Redact Personal Information Before You Hand It Over: Running Presidio

I ran Microsoft's Presidio library without network access to detect and anonymize names, email addresses, card numbers, and other personal information in text. I checked how to distinguish harmless error logs emitted by dependency libraries from failed runs.

Repository covered in this article

microsoft/presidio

An open-source framework for detecting, redacting, masking, and anonymizing sensitive data (PII) across text, images, and structured data. Supports NLP, pattern matching, and customizable pipelines.

10k Python MIT License

Many inquiry histories and support chat logs contain names, email addresses, and phone numbers. The analysis does not need the personal information itself. The data you hand over contains it. Presidio is Microsoft's library for detecting passages that may contain personal information in text and redacting them (anonymizing them). I installed Presidio, disconnected the network, and tested detection and anonymization.

Presidio combines rule-based recognizers with NLP models. It detects entities such as email addresses, phone numbers, credit card numbers, and names and anonymizes them. Microsoft publishes the source under MIT License (*1).

1. Install it

Use spaCy's lightweight model for named-entity recognition, including names.

FROM docker.io/library/python:3.12.13-slim
RUN pip install --no-cache-dir --quiet 'presidio-analyzer==2.2.363' 'presidio-anonymizer==2.2.363'
RUN python -m spacy download en_core_web_sm
WORKDIR /work
podman build -t presidio-sandbox .

2. Run detection and anonymization

I disconnected the network and processed dummy text containing a name, email address, phone number, and card number.

from presidio_analyzer import AnalyzerEngine
from presidio_analyzer.nlp_engine import NlpEngineProvider
from presidio_anonymizer import AnonymizerEngine

nlp_config = {
    "nlp_engine_name": "spacy",
    "models": [{"lang_code": "en", "model_name": "en_core_web_sm"}],
}
nlp_engine = NlpEngineProvider(nlp_configuration=nlp_config).create_engine()
analyzer = AnalyzerEngine(nlp_engine=nlp_engine, supported_languages=["en"])
anonymizer = AnonymizerEngine()

text = (
    "Hi, this is John Smith. You can reach me at john.smith@example.com "
    "or call 555-123-4567. My credit card number is 4111-1111-1111-1111."
)

results = analyzer.analyze(text=text, language="en")
anonymized = anonymizer.anonymize(text=text, analyzer_results=results)
print(anonymized.text)
podman run --rm --network none --memory 2g --cpus 2 --pids-limit 512 \
  -v "$(pwd)":/work -w /work presidio-sandbox \
  python pii_demo.py

The -v option gives the container read and write access to the specified directory. Run this command in an empty directory reserved for the experiment. Do not pass real personal information. The email address, phone number, and card number in this example are industry-standard dummy values for testing.

I got this output:

ORIGINAL:  Hi, this is John Smith. You can reach me at john.smith@example.com or call 555-123-4567. My credit card number is 4111-1111-1111-1111.
ANONYMIZED: Hi, this is <PERSON>. You can reach me at <EMAIL_ADDRESS> or call <PHONE_NUMBER>. My credit card number is <CREDIT_CARD>.

Presidio detected all four items and replaced each matching span with its label.

A snag: distinguishing harmless error logs from failures

During the run, I saw several long tracebacks on standard error after connections to publicsuffix.org and raw.githubusercontent.com failed. I got the correct detection and anonymization output despite the tracebacks. I traced the cause to tldextract, the domain-name parsing library that Presidio uses. The library tries to fetch up-to-date data from the network on each run so it can identify a URL's domain with precision. After --network none blocked that request, tldextract fell back to its bundled snapshot data. I got the final result and saw the failed connection attempts in the error logs.

Use the final output to decide whether a run with --network none succeeded, even if error logs appear. The final output in this run is the anonymized text.

Redact data before you hand it over

Add an automated pipeline step to remove personal information from logs or inquiry histories before you feed them to an LLM. You can increase the range of data you analyze by using detection and anonymization instead of redacting each record by hand.

Sources

*1 Presidio (GitHub), https://github.com/microsoft/presidio, MIT License