DetectPII
Blocks prompts containing emails, phone numbers, SSNs, credit card numbers or IP addresses. Runs before the model is called.
Parameters
Section titled “Parameters”| Parameter | Type | Default | Description |
|---|---|---|---|
types |
Sequence[str] |
all five | Any of email, phone, ssn, credit_card, ip_address |
backend |
'regex' | 'presidio' |
'regex' |
Detection engine |
score_threshold |
float |
0.5 |
Minimum Presidio confidence, in [0, 1] (Presidio backend only) |
on_fail |
'raise' | 'warn' |
'raise' |
Raise InputBlocked or log and continue |
An unknown name in types, or a score_threshold outside [0, 1] or NaN, raises ValueError at construction, so a typo such as 'credit-card' surfaces at startup.
Examples
Section titled “Examples”from pydantic_ai import Agentfrom pydantic_ai_guardrails import DetectPII, InputBlocked
agent = Agent('openai:gpt-4o', capabilities=[DetectPII(types=['ssn', 'credit_card'])])
try: agent.run_sync('Charge card 4111-1111-1111-1111')except InputBlocked as e: print(e.guardrail, e.reason) # DetectPII detected PII: credit_cardPresidio backend
Section titled “Presidio backend”pip install 'pydantic-ai-guardrails[pii-detection]'from pydantic_ai_guardrails import DetectPII
DetectPII(backend='presidio', types=['email', 'phone'], score_threshold=0.7)Presidio uses NER models, so it catches formats the regex misses. Without the extra installed, construction raises ImportError with the install command.
Failure behaviour
Section titled “Failure behaviour”on_fail='raise': raisesInputBlockedwith.guardrail == 'DetectPII'and.reasonlisting the detected types.on_fail='warn': logs to thepydantic_ai_guardrailslogger and the run continues.