Skip to main content

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-filter HuggingFace 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

Metadata

Release files for privacy-filter 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for privacy-filter 0.1.0
File Size Uploaded
privacy_filter-0.1.0.tar.gz 117.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for privacy-filter 0.1.0
File Interpreter ABI Platform
privacy_filter-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 125.7 kB

Release files / privacy_filter-0.1.0.tar.gz

Download URL privacy_filter-0.1.0.tar.gz
Size 117.3 kB
Tags Source
SHA-256 checksum
How to use checksums
301409def568387851e30b5a17ed4da99fb16ad9e3a5c429a74ef92a51f6c155
BLAKE2b-256 checksum
How to use checksums
461811ae86b8dc5fddc12dc083362a0e9d1912c871b993a17f3dd9b759fe9e4e
Upload date
Uploaded using Trusted Publishing?
What is 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}

Release files / privacy_filter-0.1.0-py3-none-any.whl

Download URL privacy_filter-0.1.0-py3-none-any.whl
Size 8.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c938f79680536e5e73d4090ebe4a2d23711297cc9dfaa56983391eb472a9332f
BLAKE2b-256 checksum
How to use checksums
dfa82985e77f09b9eadf9145532f1b23fb66c797ddaad85c77e4b38c69b5eca0
Upload date
Uploaded using Trusted Publishing?
What is 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}

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page