Skip to main content

Redact sensitive information (API keys, emails, card info, etc.) from prompts before sending to LLMs

Project description

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. Injectresult.text includes instruction telling LLM to preserve placeholders
  3. Restoreunredact() 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:

  • Emailjohn@example.com[EMAIL_1]
  • Phone555-123-4567[PHONE_1]
  • SSN123-45-6789[SSN_1]
  • Credit Card4111-1111-1111-1111[CREDIT_CARD_1]
  • IP Address192.168.1.1[IP_1]
  • API Keys — OpenAI, Anthropic, AWS, GitHub, Stripe, Slack, Google

NER-based (via spaCy):

  • PersonJohn Smith[PERSON_1]
  • OrganizationAcme Corp[ORG_1]
  • LocationNew 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

redact_prompt-0.1.0.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

redact_prompt-0.1.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file redact_prompt-0.1.0.tar.gz.

File metadata

  • Download URL: redact_prompt-0.1.0.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for redact_prompt-0.1.0.tar.gz
Algorithm Hash digest
SHA256 82a3657cea74ffd6ee325b47c0cd1449aeaa4d5037552345b5a8ee4a1a407db1
MD5 7e7523415b248bc6086cc213fd232048
BLAKE2b-256 845198b46c3c98b1ebb683e5f3c0282578c289b2af895654515fcc4e5c660f58

See more details on using hashes here.

File details

Details for the file redact_prompt-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: redact_prompt-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for redact_prompt-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bc9b1a4b6fee733e6f31f2e9ea4a433d36975c836243776ee1ea7d24fee37775
MD5 f58b90bcbfeb7ba7679ca057d70f835f
BLAKE2b-256 5c87197f2bf84117cf7602dcbb1558aeb127fc295a011cdf24b71b195a98e276

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page