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
- 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
Masking (Redaction)
Replaces PII with placeholder text:config.yml
Configuration
Complete Configuration
config.yml
Score Threshold
Thescore_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):Output Rails
Detect (Block):Retrieval Rails
Detect (Block):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
- CREDIT_CARD
- IBAN_CODE
- CRYPTO
- US_SSN
- US_PASSPORT
- US_DRIVER_LICENSE
- UK_NHS
- SG_NRIC_FIN
- IP_ADDRESS
- MAC_ADDRESS
- MEDICAL_LICENSE
- US_ITIN
Custom Flows
Create custom PII handling:flows.co
Actions
Two actions are available:DetectSensitiveDataAction
ReturnsTrue if PII is detected:
MaskSensitiveDataAction
Returns masked text:Integration with RAG
Mask PII in retrieved documents:flows.co
Dependencies
Performance Considerations
PII detection adds latency:- spaCy model loading takes time on first run
- Each detection requires NLP processing
- Consider caching results when possible
- Only enable for necessary sources (input/output/retrieval)
- Limit entities to those actually needed
- Adjust score threshold to reduce false positives
- 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
DetectSensitiveDataAction- Returns boolean for presence of PIIMaskSensitiveDataAction- Returns masked text with PII replaced
Best Practices
- Start with detection - Use blocking mode first to understand what PII appears
- Tune threshold - Adjust based on false positive/negative rates
- Use appropriate entities - Only detect PII relevant to your domain
- Different rules per source - Input/output/retrieval may need different configurations
- Test thoroughly - Verify detection works for your specific use cases
- Consider compliance - Ensure your PII handling meets regulatory requirements (GDPR, CCPA, etc.)