Skip to main content
PII (Personally Identifiable Information) detection helps protect user privacy by identifying and optionally masking sensitive data.

Overview

The sensitive data detection guardrail uses Microsoft Presidio to:
  • Detect PII in user inputs, bot outputs, and retrieved documents
  • Mask or block detected sensitive information
  • Support custom entity recognizers
  • Configure different rules for input, output, and retrieval
Supported entity types:
  • PERSON (names)
  • EMAIL_ADDRESS
  • PHONE_NUMBER
  • CREDIT_CARD
  • US_SSN (Social Security Numbers)
  • LOCATION
  • IP_ADDRESS
  • IBAN_CODE
  • And many more…

Quick Start

1

Install dependencies

Install Presidio and spaCy:
2

Configure PII detection

Define which entities to detect:
config.yml
3

Enable detection flows

Choose between detection (blocking) or masking:
config.yml

Detection vs Masking

Detection (Blocking)

Blocks requests containing PII:
config.yml
When PII is found, the bot responds with “I don’t know the answer to that” and aborts.

Masking (Redaction)

Replaces PII with placeholder text:
config.yml
Example:

Configuration

Complete Configuration

config.yml

Score Threshold

The score_threshold controls detection sensitivity:
  • 0.0 - Detect everything (high false positives)
  • 0.4 - Balanced (recommended default)
  • 1.0 - Only very confident matches (may miss some PII)

Separate Configurations

Configure different rules for input, output, and retrieval:

Available Flows

Input Rails

Detect (Block):
Mask (Redact):

Output Rails

Detect (Block):
Mask (Redact):

Retrieval Rails

Detect (Block):
Mask (Redact):

Custom Entity Recognizers

Add custom patterns for domain-specific PII:
config.yml

Supported Entities

Presidio supports many built-in entity types: Personal Information:
  • PERSON
  • EMAIL_ADDRESS
  • PHONE_NUMBER
  • LOCATION
  • DATE_TIME
  • URL
Financial:
  • CREDIT_CARD
  • IBAN_CODE
  • CRYPTO
Identification:
  • US_SSN
  • US_PASSPORT
  • US_DRIVER_LICENSE
  • UK_NHS
  • SG_NRIC_FIN
Technical:
  • IP_ADDRESS
  • MAC_ADDRESS
Medical:
  • MEDICAL_LICENSE
  • US_ITIN
See Presidio documentation for the complete list.

Custom Flows

Create custom PII handling:
flows.co

Actions

Two actions are available:

DetectSensitiveDataAction

Returns True if PII is detected:

MaskSensitiveDataAction

Returns masked text:

Integration with RAG

Mask PII in retrieved documents:
flows.co

Dependencies

PII detection requires additional packages that must be installed separately.
If these are not installed, you’ll see:

Performance Considerations

PII detection adds latency:
  • spaCy model loading takes time on first run
  • Each detection requires NLP processing
  • Consider caching results when possible
Optimization tips:
  1. Only enable for necessary sources (input/output/retrieval)
  2. Limit entities to those actually needed
  3. Adjust score threshold to reduce false positives
  4. Use masking instead of detection when appropriate

Implementation Details

The PII detection flows are defined in:
  • /nemoguardrails/library/sensitive_data_detection/flows.co
  • /nemoguardrails/library/sensitive_data_detection/actions.py
Actions:
  • DetectSensitiveDataAction - Returns boolean for presence of PII
  • MaskSensitiveDataAction - Returns masked text with PII replaced

Best Practices

  1. Start with detection - Use blocking mode first to understand what PII appears
  2. Tune threshold - Adjust based on false positive/negative rates
  3. Use appropriate entities - Only detect PII relevant to your domain
  4. Different rules per source - Input/output/retrieval may need different configurations
  5. Test thoroughly - Verify detection works for your specific use cases
  6. Consider compliance - Ensure your PII handling meets regulatory requirements (GDPR, CCPA, etc.)

See Also