PII redaction using the openai/privacy-filter token-classification model
Project description
privacy-filter
PII redaction for Python using the OpenAI privacy-filter model. Detect and redact personally identifiable information (PII) from text before sending to LLMs or storing in logs.
Features
- Local PII detection — Uses the
openai/privacy-filterHuggingFace model (runs locally, no API calls) - Reversible redaction — Replace PII with placeholders like
[EMAIL_1],[PERSON_2], then restore original values later - Entity types — Email, phone, address, person names, URLs, dates, account numbers, secrets
- Configurable thresholds — Adjust confidence scores and filter by entity type
- Thread-safe — Lazy-loaded singleton pipeline with double-checked locking
Installation
pip install privacy-filter
Requires Python 3.11+.
Quick Start
from privacy_filter import get_classifier, redact_text, unredact_text, PiiStore
# Load model (first run downloads ~50MB, cached after)
classifier = get_classifier()
# Detect PII
text = "My email is alice@example.com and my name is Alice Smith"
entities = classifier(text)
# Redact
store = PiiStore()
redacted = redact_text(text, entities, store)
print(redacted) # "My email is [EMAIL_1] and my name is [PERSON_1]"
# Unredact (restore original values)
restored = unredact_text(redacted, store)
print(restored) # "My email is alice@example.com and my name is Alice Smith"
API Reference
get_classifier(cache_dir=None)
Returns a HuggingFace token-classification pipeline. First call downloads the model (~50MB). Thread-safe singleton.
redact_text(text, entities, store, min_score=0.8, entity_types=None)
Replaces detected entities with placeholders. Populates store.forward with placeholder → original mappings.
unredact_text(text, store)
Restores original values from placeholders using regex substitution.
PiiStore
Dataclass holding forward (placeholder→value dict) and counters (per-type counters).
Supported Entity Types
| Entity | Placeholder Prefix | Description |
|---|---|---|
private_email |
EMAIL |
Email addresses |
private_person |
PERSON |
Person names |
private_phone |
PHONE |
Phone numbers |
private_address |
ADDRESS |
Physical addresses |
private_url |
URL |
URLs |
private_date |
DATE |
Dates |
account_number |
ACCOUNT |
Account numbers |
secret |
SECRET |
Passwords, API keys, tokens |
Advanced Usage
Filter by entity type
entities = classifier(text)
redacted = redact_text(text, entities, store, entity_types=["private_email", "private_phone"])
Adjust confidence threshold
redacted = redact_text(text, entities, store, min_score=0.95)
Custom cache directory
classifier = get_classifier(cache_dir="/path/to/cache")
Integrations
- NanoBot —
pip install nanobot-privacy-filter-hook - OpenAI Agents SDK —
pip install openai-agents-privacy-filter
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file privacy_filter-0.1.0.tar.gz.
File metadata
- Download URL: privacy_filter-0.1.0.tar.gz
- Upload date:
- Size: 117.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
301409def568387851e30b5a17ed4da99fb16ad9e3a5c429a74ef92a51f6c155
|
|
| MD5 |
644e2d9620c4d6888dc85d494746a8a2
|
|
| BLAKE2b-256 |
461811ae86b8dc5fddc12dc083362a0e9d1912c871b993a17f3dd9b759fe9e4e
|
File details
Details for the file privacy_filter-0.1.0-py3-none-any.whl.
File metadata
- Download URL: privacy_filter-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c938f79680536e5e73d4090ebe4a2d23711297cc9dfaa56983391eb472a9332f
|
|
| MD5 |
ae8125e53b312be1f5ec3e1779c09aec
|
|
| BLAKE2b-256 |
dfa82985e77f09b9eadf9145532f1b23fb66c797ddaad85c77e4b38c69b5eca0
|