> ## Documentation Index
> Fetch the complete documentation index at: https://agno-v2-codex-docs-audit-20260719-0149.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# PII Detection Guardrail

> Detect configured personally identifiable information formats in agent inputs.

The PII Detection Guardrail checks agent input text against built-in and custom regular expressions for personally identifiable information (PII).

Use it as a pre-hook to reject input containing configured patterns or mask matching text before the run continues.

<Note>
  Pattern matching does not establish that an input contains no PII. Add custom patterns or separate controls for formats your application must cover.
</Note>

## Basic Usage

To provide your Agent with the PII Detection Guardrail, you need to import it and pass it to the Agent using the `pre_hooks` parameter:

```python theme={null}
from agno.guardrails import PIIDetectionGuardrail
from agno.agent import Agent
from agno.models.openai import OpenAIResponses

agent = Agent(
    name="Privacy-Protected Agent",
    model=OpenAIResponses(id="gpt-5.2"),
    pre_hooks=[PIIDetectionGuardrail()],
)
```

## PII fields

The built-in checks match these formats:

* US Social Security numbers written as `123-45-6789`
* 16-digit credit card numbers, with optional spaces or hyphens between groups
* Email addresses
* 10-digit phone numbers, with optional spaces, periods, or hyphens

You can also select which specific fields you want to detect. For example, we can disable the Email check by doing this:

```python theme={null}
guardrail = PIIDetectionGuardrail(
    enable_email_check=False,
)
```

## Custom PII fields

You can also extend the list of PII fields handled by the guardrail by adding your own custom PII patterns.

For example, we can add a custom PII pattern for bank account numbers:

```python theme={null}
guardrail = PIIDetectionGuardrail(
    custom_patterns={
        "bank_account_number": r"\b\d{10}\b",
    }
)
```

Notice that providing custom PII patterns via the `custom_patterns` parameter will extend, not override, the default list of PII fields. You can stop checking for default PII fields by setting the `enable_ssn_check`, `enable_credit_card_check`, `enable_email_check`, and `enable_phone_check` parameters to `False`.

## Masking PII

By default, the PII Detection Guardrail will raise an error if it detects any PII in the input.

However, you can mask the PII in the input instead of raising, by setting the `mask_pii` parameter to `True`:

```python theme={null}
guardrail = PIIDetectionGuardrail(
    mask_pii=True,
)
```

This masks text matched by the enabled patterns with asterisk characters. For example, if you are checking for emails, the string `joe@example.com` will be masked as `***************`.

## Developer Resources

* [Examples](/guardrails/usage/agent/pii-detection)
* [Reference](/reference/hooks/pii-guardrail)
