Skip to main content
# pip install "upsonic[safety-engine]"
uv pip install "upsonic[safety-engine]"

What is PII Policy?

PII policies detect and protect personal identifiable information including emails, phone numbers, SSN, addresses, credit cards, driver’s licenses, passports, IP addresses, and other sensitive personal data.

Why its Important?

PII policies are critical for protecting personal identifiable information and ensuring compliance with privacy regulations. These policies prevent sensitive personal data from being sent to LLMs, which helps maintain user privacy, prevent identity theft, and comply with data protection laws.
  • Prevents sending PII to LLM: Blocks emails, addresses, SSN, and other personal data from being processed by language models
  • Protects against identity theft: Prevents sensitive personal information from being exposed or used maliciously
  • Ensures privacy compliance: Helps maintain compliance with GDPR, CCPA, and other data protection regulations

Usage

from upsonic import Agent, Task
from upsonic.safety_engine.policies.pii_policies import PIIAnonymizePolicy

# Create agent with PII anonymization
agent = Agent(
    "anthropic/claude-sonnet-4-5",
    user_policy=PIIAnonymizePolicy,  # Prevents data leak to LLM Providers
    debug=True  # Enable debug to see policy application
)

# User input with PII
task = Task(
    description="My email is john.doe@example.com and phone is 555-1234. What are my email and phone?"
)

# Execute with automatic anonymization and de-anonymization
# What happens under the hood:
# 1. Input: "My email is john.doe@example.com and phone is 555-1234..."
# 2. Anonymized (sent to LLM): "My email is EMAIL_1 and phone is PHONE_1..."
# 3. LLM Response: "Your email is EMAIL_1 and phone is PHONE_1"
# 4. De-anonymized (returned to you): "Your email is john.doe@example.com and phone is 555-1234"
#
# Result: Sensitive data never reaches the cloud, but you get fully functional results!
result = agent.do(task)
print(result)  # Returns: "Your email is john.doe@example.com and phone is 555-1234"

Available Variants

  • PIIBlockPolicy: Blocks any content with PII
  • PIIBlockPolicy_LLM: LLM-powered block messages
  • PIIBlockPolicy_LLM_Finder: LLM detection for better accuracy
  • PIIAnonymizePolicy: Anonymizes PII with unique replacements
  • PIIReplacePolicy: Replaces PII with [PII_REDACTED]
  • PIIRaiseExceptionPolicy: Raises DisallowedOperation exception
  • PIIRaiseExceptionPolicy_LLM: LLM-generated exception messages