Skip to content

PII Detector

The pii_detector guardrail scans prompts for personally identifiable information (PII) like emails, phone numbers, and social security numbers.

from pydantic_ai_guardrails.guardrails.input import pii_detector
from pydantic_ai_guardrails import GuardedAgent
from pydantic_ai_guardrails.guardrails.input import pii_detector
guarded_agent = GuardedAgent(
agent,
input_guardrails=[
pii_detector(),
],
)
ParameterTypeDefaultDescription
detect_typeslist[str] | NoneAll typesPII types to detect
thresholdfloat0.0Minimum confidence threshold
TypePatternExample
emailEmail addressesuser@example.com
phonePhone numbers555-123-4567
ssnSocial Security Numbers123-45-6789
credit_cardCredit card numbers4111-1111-1111-1111
ip_addressIP addresses192.168.1.1
# Default: detect all PII types
guardrail = pii_detector()
# Only detect email and phone
guardrail = pii_detector(
detect_types=['email', 'phone'],
)
# Healthcare-relevant PII
guardrail = pii_detector(
detect_types=['ssn', 'phone', 'email'],
)

When triggered, returns:

{
'tripwire_triggered': True,
'message': 'PII detected in input: email, phone',
'severity': 'high',
'metadata': {
'detected_types': ['email', 'phone'],
'count': 2,
},
}
  • GDPR compliance: Block prompts containing EU personal data
  • HIPAA compliance: Prevent PHI in healthcare applications
  • Privacy protection: Keep user data out of LLM context
  • Data minimization: Enforce data handling policies

For organization-specific identifiers, create a custom guardrail:

import re
from pydantic_ai_guardrails import GuardrailResult, InputGuardrail
async def custom_pii_detector(prompt: str) -> GuardrailResult:
patterns = {
'employee_id': r'EMP-\d{6}',
'account_number': r'ACC-[A-Z]{2}\d{8}',
}
found = []
for pii_type, pattern in patterns.items():
if re.search(pattern, prompt):
found.append(pii_type)
if found:
return {
'tripwire_triggered': True,
'message': f'Custom PII detected: {found}',
'severity': 'high',
}
return {'tripwire_triggered': False}
guardrail = InputGuardrail(custom_pii_detector)