pii-filter
A Python library that detects and masks PII (Personally Identifiable Information) in text before sending it to LLMs, then reconstructs the original values from LLM responses.
Installation
pip install -e .
For the interactive demo (requires OpenAI):
pip install -e ".[demo]"
How It Works
User text → filter() → sanitized text + session_id
↓
Send to LLM (placeholders preserved)
↓
LLM response → reconstruct(session_id, response) → original PII restored
Placeholders use the format [TYPE_N] — e.g. [PAN_1], [PHONE_2], [EMAIL_3].
Built-in Detectors
| Detector | Label | Accepted Formats | Validation | Spaced |
|---|---|---|---|---|
| Credit Card | CREDIT_CARD |
Visa (4xxx), Mastercard (51xx–55xx), Amex (34xx/37xx), Discover (6011/65xx). 13–19 digits. |
Luhn checksum | 4111 1111 1111 1111, 4111-1111-1111-1111 |
| Phone | PHONE |
Indian: +91 XXXXXXXXXX, 091-XXXXXXXXXX, 0XXXXXXXXXX, or bare 10 digits starting 6–9. International: +CC NNNN... (7–14 digits). |
— | +91 98765 43210, 98765 43210 |
| Aadhaar | AADHAAR |
12 digits, first digit must be 2–9. Standard 4-4-4 grouping. | — | 2345 6789 0123, 2345-6789-0123 |
| PAN Card | PAN |
5 uppercase letters + 4 digits + 1 uppercase letter (AAAAA9999A). |
Word boundary | Not applicable |
| Driving License | DL |
SS RR YYYY NNNNNNN — 2-letter state code, 2-digit RTO, 4-digit year (19xx/20xx), 7-digit serial. |
Year prefix 19/20 | DL 14 2011 0149646, DL-14-2011-0149646 |
EMAIL |
Standard local@domain.tld format. Supports ., +, %, - in local part. |
— | Not applicable |
Usage
Basic — All Detectors
from pii_filter import PIIFilter
pf = PIIFilter()
sanitized, session_id = pf.filter("My PAN is ABCDE1234F, call +919876543210")
# sanitized: "My PAN is [PAN_1], call [PHONE_2]"
restored = pf.reconstruct(session_id, sanitized)
# restored: "My PAN is ABCDE1234F, call +919876543210"
Selective Detectors
Only enable the detectors you need:
pf = PIIFilter(detectors=["PAN", "EMAIL"])
Available labels: CREDIT_CARD, PHONE, AADHAAR, PAN, DL, EMAIL.
Custom Patterns
Add your own regex-based detectors:
pf = PIIFilter()
pf.add_custom_pattern("PASSPORT", r"[A-Z]\d{7}")
sanitized, sid = pf.filter("Passport: A1234567")
# sanitized: "Passport: [PASSPORT_1]"
With a validator function:
pf.add_custom_pattern(
"PASSPORT",
r"[A-Z]\d{7}",
validator=lambda s: len(s) == 8,
)
Appending LLM Instructions
When sending to an LLM, append the placeholder-preservation instruction automatically:
sanitized, sid = pf.filter("My PAN is ABCDE1234F", append_instruction=True)
This appends a block instructing the LLM to preserve all [TYPE_N] placeholders verbatim.
You can also retrieve the instruction text directly:
instruction = PIIFilter.get_prompt_instruction()
Session Cleanup
pf.clear_session(session_id) # remove one session
pf.clear_all_sessions() # remove all sessions
Demo Script
An interactive script that filters user input, sends it to OpenAI, and reconstructs the response:
export OPENAI_API_KEY="sk-..."
python demo.py
Optionally set the model:
export OPENAI_MODEL="gpt-4o"
python demo.py
Development
pip install -e ".[dev]"
pytest --cov=pii_filter
Metadata
Release files for piifilter 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| piifilter-0.1.0.tar.gz | 12.7 kB | Without provenance | Details for piifilter-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| piifilter-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for piifilter-0.1.0-py3-none-any.whl |
Total release size: 25.6 kB
Release files / piifilter-0.1.0.tar.gz
| Download URL | piifilter-0.1.0.tar.gz |
|---|---|
| Size | 12.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3155885836d1e92f85416cb6cdd62f5782c2fd7c110b47da291a5411f435ec84
|
|
BLAKE2b-256 checksum How to use checksums |
d39225a1831a29f4136ab459d16fe946f9ea6196834ac5f1ad72ef23d764cddd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Release files / piifilter-0.1.0-py3-none-any.whl
| Download URL | piifilter-0.1.0-py3-none-any.whl |
|---|---|
| Size | 12.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e713d08feb60856fa6abfb6ad21d611c24eb76b77a6b4a0f5db6d3c5f79b926d
|
|
BLAKE2b-256 checksum How to use checksums |
882a9f321899f104a6da9d5a3a55a61b3155446b431f5c96cb48f3ec43fb9189
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Security for piifilter 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.