FastPII Docs
AI Privacy Patterns

Protecting OpenAI Prompts

Detect and anonymize PII before sending prompts to OpenAI models.

Protecting OpenAI Prompts

Prompt payloads often include support tickets, meeting notes, CRM exports, or user chat history. Before you send that text to OpenAI, run FastPII detection and anonymization locally.

Detect before sending

from openai import OpenAI

from fastpii import PrivacyGuard

guard = PrivacyGuard(regions=["cz"])
client = OpenAI()

prompt = "Summarize this case: Jan Novak, rodné číslo 850101/1234, called from Brno."

result = guard.detect(prompt)
safe_prompt = guard.anonymize(prompt)

response = client.responses.create(
    model="gpt-4.1-mini",
    input=safe_prompt,
)

print(len(result.findings))

This pattern keeps the LLM useful while reducing the chance that personal data leaves your boundary.

Block or transform based on findings

from fastpii import PrivacyGuard

guard = PrivacyGuard(regions=["cz"])


def prepare_prompt(text: str) -> str:
    result = guard.detect(text)
    if not result.findings:
        return text
    return guard.anonymize(text)

Use this when you want every prompt to flow through one safety checkpoint before model invocation.

LangChain anonymizer wrapper pattern

If you use LangChain, wrap model-bound text with the provided integration classes.

from fastpii.integrations.langchain import PIIAnonymizer

anonymizer = PIIAnonymizer(regions=["cz"])

safe_prompt = anonymizer.anonymize(
    "Draft a reply to Jan Novak at jan.novak@example.com about invoice 850101/1234."
)

print(safe_prompt)

Practical policy choices

  • Use detect() when you need an audit trail before sending.
  • Use anonymize() when the model still needs semantic structure.
  • Use redact() when the prompt does not need the original value at all.
  • Use remove() for aggressive cleanup in internal automation flows.

Minimal prompt gateway

from openai import OpenAI

from fastpii import PrivacyGuard

guard = PrivacyGuard(regions=["cz"])
client = OpenAI()


def run_llm(prompt: str):
    sanitized = guard.anonymize(prompt)
    return client.responses.create(model="gpt-4.1-mini", input=sanitized)

Centralizing this logic makes it easier to enforce consistent privacy rules across assistants, batch jobs, and internal tools.

On this page