Skip to main content

redact-prompt

Redact PII from prompts before sending to LLMs. Restore original values in responses.

Install

pip install redact-prompt

The spaCy language model downloads automatically on first use.

How It Works

How redact-prompt works
  1. Redact — Regex + NER detect PII, replace with numbered placeholders
  2. Inject — result.text includes instruction telling LLM to preserve placeholders
  3. Restore — unredact() maps placeholders back to original values

Same values get same placeholders (deterministic), so john@acme.com appearing twice → [EMAIL_1] both times.

Quick Start

from redact_prompt import redact, unredact

result = redact("Email john@acme.com about the project")

# Send result.text to LLM (includes injected instruction to preserve placeholders)

restored = unredact(llm_response)

Examples

# OpenAI
from openai import OpenAI
from redact_prompt import redact, unredact

client = OpenAI()
result = redact("My email is sarah@acme.com and my API key is sk-proj-abc123xyz789def456.")

response = client.responses.create(
    model="gpt-5-mini-2025-08-07",
    input=result.text,
)
print(unredact(response.output_text))
# Anthropic
import anthropic
from redact_prompt import redact, unredact

client = anthropic.Anthropic()
result = redact("Hi, I'm John Smith from Acme Corp. My email is john@acme.com.")

message = client.messages.create(
    model="claude-sonnet-4-5-20250929",
    max_tokens=1024,
    messages=[{"role": "user", "content": result.text}],
)
print(unredact(message.content[0].text))
# Google Gemini
from google import genai
from redact_prompt import redact, unredact

client = genai.Client()
result = redact("Contact me at 555-123-4567 or jane.doe@company.com for details.")

response = client.models.generate_content(
    model="gemini-3.0-pro-preview",
    contents=result.text,
)
print(unredact(response.text))

See examples/ for OpenRouter and more.

What It Detects

Regex-based:

  • Email — john@example.com → [EMAIL_1]
  • Phone — 555-123-4567 → [PHONE_1]
  • SSN — 123-45-6789 → [SSN_1]
  • Credit Card — 4111-1111-1111-1111 → [CREDIT_CARD_1]
  • IP Address — 192.168.1.1 → [IP_1]
  • API Keys — OpenAI, Anthropic, AWS, GitHub, Stripe, Slack, Google

NER-based (via spaCy):

  • Person — John Smith → [PERSON_1]
  • Organization — Acme Corp → [ORG_1]
  • Location — New York → [LOCATION_1]

API

from redact_prompt import redact, unredact, clear

result = redact("text")        # Returns RedactionResult
result.text                    # Redacted + instruction (send to LLM)
result.redacted                # Just redacted text (for debugging)
result.entities                # List of detected entities

unredact(llm_response)         # Restore original values
clear()                        # Reset mappings between conversations

Options

# Exclude specific types
result = redact("text", exclude=["EMAIL", "PHONE"])

# Only include specific types
result = redact("text", include=["SSN", "CREDIT_CARD"])

# Disable NER (faster, regex only)
result = redact("text", use_ner=False)

# Multiple conversations (use class API)
from redact_prompt import Redactor
r = Redactor()
result = r.redact("text")
r.clear()  # Reset for new conversation

Available types: EMAIL, PHONE, SSN, CREDIT_CARD, IP, API_KEY, PERSON, ORG, LOCATION

Contributing

PRs welcome! See GitHub Issues for wanted features.

Adding a new PII pattern:

  1. Add regex to PATTERNS in redact_prompt/__init__.py
  2. Add test in tests/test_redact.py
  3. Update "What It Detects" in README

Wanted patterns: URL, Date of Birth, Address/ZIP, Bank Account, Drivers License, Passport Number, and more. Check issues.

License

MIT

Metadata

Release files for redact-prompt 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 redact-prompt 0.1.0
File Size Uploaded Provenance Actions
redact_prompt-0.1.0.tar.gz 7.6 kB Without provenance Details for redact_prompt-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for redact-prompt 0.1.0
File Interpreter ABI Platform Actions
redact_prompt-0.1.0-py3-none-any.whl Python 3 none any Details for redact_prompt-0.1.0-py3-none-any.whl

Total release size: 14.7 kB

Release files / redact_prompt-0.1.0.tar.gz

Download URL redact_prompt-0.1.0.tar.gz
Size 7.6 kB
Tags Source
SHA-256 checksum
How to use checksums
82a3657cea74ffd6ee325b47c0cd1449aeaa4d5037552345b5a8ee4a1a407db1
BLAKE2b-256 checksum
How to use checksums
845198b46c3c98b1ebb683e5f3c0282578c289b2af895654515fcc4e5c660f58
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.11

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

Download URL redact_prompt-0.1.0-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bc9b1a4b6fee733e6f31f2e9ea4a433d36975c836243776ee1ea7d24fee37775
BLAKE2b-256 checksum
How to use checksums
5c87197f2bf84117cf7602dcbb1558aeb127fc295a011cdf24b71b195a98e276
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.11

Security for redact-prompt 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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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