PII Detector
The pii_detector guardrail scans prompts for personally identifiable information (PII) like emails, phone numbers, and social security numbers.
Import
Section titled “Import”from pydantic_ai_guardrails.guardrails.input import pii_detectorBasic Usage
Section titled “Basic Usage”from pydantic_ai_guardrails import GuardedAgentfrom pydantic_ai_guardrails.guardrails.input import pii_detector
guarded_agent = GuardedAgent( agent, input_guardrails=[ pii_detector(), ],)Parameters
Section titled “Parameters”| Parameter | Type | Default | Description |
|---|---|---|---|
detect_types | list[str] | None | All types | PII types to detect |
threshold | float | 0.0 | Minimum confidence threshold |
Detected PII Types
Section titled “Detected PII Types”| Type | Pattern | Example |
|---|---|---|
email | Email addresses | user@example.com |
phone | Phone numbers | 555-123-4567 |
ssn | Social Security Numbers | 123-45-6789 |
credit_card | Credit card numbers | 4111-1111-1111-1111 |
ip_address | IP addresses | 192.168.1.1 |
Examples
Section titled “Examples”Detect All PII
Section titled “Detect All PII”# Default: detect all PII typesguardrail = pii_detector()Specific Types Only
Section titled “Specific Types Only”# Only detect email and phoneguardrail = pii_detector( detect_types=['email', 'phone'],)HIPAA-Focused
Section titled “HIPAA-Focused”# Healthcare-relevant PIIguardrail = pii_detector( detect_types=['ssn', 'phone', 'email'],)Violation Result
Section titled “Violation Result”When triggered, returns:
{ 'tripwire_triggered': True, 'message': 'PII detected in input: email, phone', 'severity': 'high', 'metadata': { 'detected_types': ['email', 'phone'], 'count': 2, },}Use Cases
Section titled “Use Cases”- 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
Advanced: Custom PII Patterns
Section titled “Advanced: Custom PII Patterns”For organization-specific identifiers, create a custom guardrail:
import refrom 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)Related
Section titled “Related”- Input Guardrails Guide
- llm-guard Integration
- Secret Redaction (for output)