Fast, accurate PII detection for LLM applications. Zero dependencies, works offline.
Project description
pii-guard
Fast, accurate PII detection for LLM applications. Zero dependencies, works 100% offline.
Features
- 50+ PII entity types - SSN, credit cards, emails, phones, crypto addresses, medical records, international IDs, and more
- Zero dependencies - Pure Python stdlib, no heavy ML frameworks
- Works offline - No API calls, no cloud services, runs entirely on your machine
- Fast - ~10ms per document on average
- Multilingual - Supports names/addresses in EN, ES, FR, DE, IT, ZH, JA, HI
- Production-ready - Built-in validators (Luhn, VIN, IBAN, Bitcoin address validation)
Quick Start
Installation
pip install pii-guard
Usage in 3 lines
from pii_guard import scan, redact
# Detect PII
entities = scan("Email me at john@example.com or call 555-123-4567")
print(entities) # [PIIEntity(label='EMAIL', ...), PIIEntity(label='PHONE', ...)]
# Redact PII
clean = redact("My SSN is 123-45-6789")
print(clean) # "My SSN is [SSN:****]"
CLI
# Scan text
pii-guard scan "Contact john@example.com"
# [EMAIL] john@example.com (confidence: 0.99)
# Redact text
pii-guard redact "SSN: 123-45-6789"
# SSN: [SSN:****]
# JSON output
pii-guard scan "test@example.com" --json
# List all supported types
pii-guard entities
Supported Entity Types
Financial
SSN- Social Security NumbersCREDIT_CARD- Credit/debit cards (Visa, MC, Amex, Discover)IBAN- International Bank Account NumbersBITCOIN_ADDRESS- Bitcoin addresses (legacy and bech32)ETHEREUM_ADDRESS- Ethereum addressesROUTING_NUMBER- US bank routing numbersBANK_ACCOUNT- Bank account numbersSWIFT_CODE- SWIFT/BIC codes
Contact
EMAIL- Email addressesPHONE- Phone numbers (US and international)IP_ADDRESS- IPv4 addressesIPV6_ADDRESS- IPv6 addressesMAC_ADDRESS- MAC addresses
Personal
NAME- Person names (multilingual)ADDRESS- Physical addressesDATE_OF_BIRTH- Dates of birthDRIVER_LICENSE- Driver's license numbersPASSPORT- Passport numbers
Healthcare
MEDICAL_RECORD- Medical record numbersMEDICARE- Medicare IDsDEA_NUMBER- DEA registration numbersNPI- National Provider Identifiers
Vehicle
VIN- Vehicle Identification NumbersLICENSE_PLATE- License plate numbers
International IDs
UK_NINO- UK National Insurance NumbersCANADA_SIN- Canadian Social Insurance NumbersFRANCE_INSEE- French INSEE numbersGERMANY_STEUER- German Tax IDsINDIA_AADHAAR- Indian Aadhaar numbersINDIA_PAN- Indian PAN cards
Corporate
EMPLOYEE_ID- Employee IDsTAX_ID- Tax identification numbers (EIN)
Advanced Usage
Using the Detector Class
from pii_guard import PIIDetector
detector = PIIDetector()
# Detect entities
entities = detector.detect("Call me at 555-123-4567")
for entity in entities:
print(f"{entity.label}: {entity.text} (confidence: {entity.confidence:.2f})")
# Get both redacted text and entities
redacted_text, entities = detector.redact("SSN: 123-45-6789")
print(redacted_text) # "SSN: [SSN:****]"
# Get statistics
stats = detector.get_statistics(entities)
print(stats)
Data Anonymization
For database anonymization and GDPR compliance:
from pii_guard import (
DataAnonymizer,
AnonymizationConfig,
AnonymizationMethod,
FieldConfig,
TableConfig,
)
# Configure anonymization
config = AnonymizationConfig(
config_id="demo",
name="User Data Anonymization",
tables=[
TableConfig(
table_name="users",
fields=[
FieldConfig("email", "email", AnonymizationMethod.FAKE),
FieldConfig("phone", "phone", AnonymizationMethod.MASK, {"show_last": 4}),
FieldConfig("ssn", "ssn", AnonymizationMethod.REDACT),
FieldConfig("name", "name", AnonymizationMethod.FAKE),
]
)
],
seed=42, # For reproducible results
)
anonymizer = DataAnonymizer(config)
# Anonymize records
records = [
{"email": "john@company.com", "phone": "555-123-4567", "ssn": "123-45-6789", "name": "John Doe"},
]
anonymized, result = anonymizer.anonymize_records(records, "users")
print(anonymized)
Anonymization Methods
| Method | Description | Example |
|---|---|---|
REDACT |
Replace with placeholder | [EMAIL_REDACTED] |
MASK |
Partial masking | ****4567 |
HASH |
One-way hash | anon_a1b2c3@example.com |
TOKENIZE |
Reversible token | TOK_EMAIL_abc123 |
FAKE |
Realistic fake data | jane.doe@example.com |
GENERALIZE |
Reduce precision | 1990-01-01 → 1990 |
NULL |
Replace with null | null |
PRESERVE |
Keep original | (unchanged) |
Fake Data Generation
from pii_guard import FakeDataGenerator
faker = FakeDataGenerator(seed=42, locale="en_US")
print(faker.full_name()) # "James Williams"
print(faker.email()) # "abcdefgh@example.com"
print(faker.phone()) # "+1-555-123-4567"
print(faker.ssn()) # "456-78-9012"
print(faker.credit_card()) # "4532 0151 1283 0366"
print(faker.address()["full"]) # "123 Main Street, New York, NY 10001"
Integrations
LangChain
from langchain.schema import BaseOutputParser
from pii_guard import redact
class PIIRedactingParser(BaseOutputParser):
def parse(self, text: str) -> str:
return redact(text)
# Use with any LangChain chain
# chain = prompt | llm | PIIRedactingParser()
FastAPI Middleware
from fastapi import FastAPI, Request
from pii_guard import redact
app = FastAPI()
@app.middleware("http")
async def redact_pii(request: Request, call_next):
response = await call_next(request)
# Add PII redaction logic here
return response
Performance
pii-guard is designed for speed:
| Document Size | Detection Time |
|---|---|
| 100 chars | ~2ms |
| 1,000 chars | ~10ms |
| 10,000 chars | ~80ms |
Benchmarked on Apple M1, Python 3.11.
Why pii-guard?
| Feature | pii-guard | Presidio | spaCy NER |
|---|---|---|---|
| Zero dependencies | ✅ | ❌ | ❌ |
| Works offline | ✅ | ✅ | ✅ |
| Entity types | 50+ | 20+ | ~18 |
| Install size | <100KB | >500MB | >200MB |
| Startup time | <50ms | >2s | >1s |
Contributing
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
License
MIT License - see LICENSE for details.
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 pii_guard-0.1.0.tar.gz.
File metadata
- Download URL: pii_guard-0.1.0.tar.gz
- Upload date:
- Size: 34.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9509cd59e2fbfd20b6db8a0539bf3039c7d207ce7ed2dcc05cf03325d1df7a5a
|
|
| MD5 |
a7309bb714a4302ee0f7739a1b5558ac
|
|
| BLAKE2b-256 |
0d1c62a29c4234e34019e89fada9dd54e27ccf0463e083f882321ea27bdf5ff9
|
File details
Details for the file pii_guard-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pii_guard-0.1.0-py3-none-any.whl
- Upload date:
- Size: 29.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
78a1c142c3ac443aee38f5cb37ae3b12e788f60681c019848e4720200f5312ab
|
|
| MD5 |
0e36531229634909f069cb48b6530f55
|
|
| BLAKE2b-256 |
d88b003139a3dc922b8a6cfd7ab9144e1a54ce21ff74dd1a869c2c80517b1a95
|