Presidio Anonymizer
Replace detected PII in text with masked, hashed, encrypted, or redacted values
Repository Health
Technical Analysis
Presidio Anonymizer is the de-identification half of the Presidio data-protection toolkit, taking the spans of personally identifiable information found by an upstream detector (typically Presidio Analyzer) and applying an operator — replace, redact, mask, hash, encrypt, or a custom lambda — to each one. It also ships Deanonymizers that reverse a prior anonymization, such as decrypting text that was encrypted with a known AES key, which lets teams round-trip sensitive data through a pipeline without ever storing the plaintext.
The package is deliberately narrow in scope: it does not detect PII itself, it only transforms already-identified entity spans. That separation keeps it usable as a standalone text-rewriting engine wherever a caller already has offsets and entity types, whether those come from Presidio Analyzer, a third-party NER model, or hand-rolled regex, while still composing cleanly with the rest of the Presidio stack for a full identify-then-anonymize workflow.
What You Get
- An
AnonymizerEnginethat applies per-entity-type operators (replace, redact, mask, hash, encrypt, custom lambda, keep) to a text plus a list of recognizer results - A
DeanonymizeEnginethat reverses encryption-based anonymization given the original key, enabling round-trip workflows - A
BatchAnonymizerEngine/BatchDeanonymizeEnginefor processing dict-shaped batches of text fields in one call - An AES-based Encrypt/Decrypt operator pair plus Hash (SHA-256/SHA-512), Mask, Redact, and Replace operators out of the box
- An optional AHDS Surrogate operator that calls Azure Health Data Services de-identification to generate realistic, format-preserving surrogates for PHI
- Conflict-resolution logic for overlapping PII spans (highest score wins, configurable strategies for equal-score ties)
Common Use Cases
- Anonymizing chat transcripts, support tickets, or logs before sending them to an LLM or third-party analytics service
- Encrypting detected PII in place so it can be securely deanonymized later by an authorized process holding the key
- Masking or redacting sensitive fields in structured records ahead of data sharing or export
- Building a compliance pipeline where Presidio Analyzer finds entities and Presidio Anonymizer enforces the redaction policy per entity type
Under The Hood
Architecture - AnonymizerEngine.anonymize() takes raw text plus a list of RecognizerResult spans, resolves overlapping spans via ConflictResolutionStrategy, and hands each surviving span to the operator registered for its entity type through OperatorsFactory, using a TextReplaceBuilder to rewrite the string span-by-span without corrupting downstream offsets; DeanonymizeEngine mirrors this for reversing Encrypt operations. Tech Stack - pure Python with cryptography for AES encryption/decryption as its only hard dependency; optional extras add Flask/Gunicorn/Waitress for a standalone HTTP server and azure-identity/azure-health-deidentification for the AHDS surrogate operator; packaged with Poetry. Code Quality - a tests/ directory with roughly 32 test modules covering each operator, conflict resolution, and the batch engines individually, plus typed dataclass-style entities (InvalidParamException, RecognizerResult) for input validation with explicit error messages rather than silent failures. API Design - the operator model (OperatorConfig(operator_name, params) keyed per entity type) is consistent across all built-in and custom operators, keeping the public surface small: instantiate an engine, pass text plus analyzer results, get back anonymized text and applied-operator metadata.