How to Intermediate · 3 min read

How to detect PII in LLM responses

Quick answer
Use a dedicated PII detection approach by sending LLM responses to a specialized model or prompt designed to identify personally identifiable information (PII). Implement this with OpenAI or Anthropic SDKs by analyzing the output text for PII entities such as names, emails, or phone numbers.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0

Setup

Install the openai Python package and set your API key as an environment variable for secure authentication.

bash
pip install openai>=1.0

Step by step

This example uses the OpenAI SDK to detect PII in a sample LLM response by prompting the model to identify any PII entities in the text.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

# Sample LLM response containing potential PII
llm_response = "My name is John Doe and my email is john.doe@example.com."

# Prompt the model to detect PII in the response
prompt = f"Identify any personally identifiable information (PII) in the following text:\n\n{llm_response}\n\nList the PII entities found or say 'None'."

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": prompt}]
)

pii_detection = response.choices[0].message.content
print("Detected PII:", pii_detection)
output
Detected PII: Name: John Doe\nEmail: john.doe@example.com

Common variations

  • Use Anthropic SDK with claude-3-5-haiku-20241022 model and the system= parameter for PII detection prompts.
  • Implement asynchronous calls if processing many responses concurrently.
  • Use specialized PII detection models or fine-tune your own for higher accuracy.
python
import os
from anthropic import Anthropic

client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

llm_response = "Contact me at 555-123-4567 or jane.smith@example.com."

system_prompt = "You are a PII detection assistant. Identify any PII in the user message."

message = client.messages.create(
    model="claude-3-5-haiku-20241022",
    max_tokens=256,
    system=system_prompt,
    messages=[{"role": "user", "content": llm_response}]
)

print("Detected PII:", message.content)
output
Detected PII: Phone number: 555-123-4567\nEmail: jane.smith@example.com

Troubleshooting

  • If the model returns vague or no PII detection, refine your prompt to explicitly ask for entity types like names, emails, phone numbers, and addresses.
  • Ensure your API key is correctly set in os.environ to avoid authentication errors.
  • For large-scale detection, consider batching inputs or using streaming to handle long texts efficiently.

Key Takeaways

  • Use targeted prompts with LLMs to identify PII in generated text effectively.
  • Leverage the latest OpenAI or Anthropic SDKs with correct model names for best results.
  • Always secure API keys via environment variables and handle errors gracefully.
  • Customize prompts or fine-tune models for domain-specific PII detection needs.
Verified 2026-04 · gpt-4o-mini, claude-3-5-haiku-20241022
Verify ↗