Skip to main content
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.
Pattern matching does not establish that an input contains no PII. Add custom patterns or separate controls for formats your application must cover.

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:

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:

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:
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:
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