Understanding PII Detection Guardrails
Created on December 3|Last edited on December 3
Comment
Why PII Detection Matters
Personal Identifiable Information (PII) in text poses significant privacy and compliance risks. Organizations need reliable ways to:
- Detect sensitive information before it's exposed
- Protect user privacy in data processing
- Comply with regulations like GDPR, HIPAA, and CCPA
- Prevent data leaks in AI system outputs
Example Response

Example input

Example output
Available Detection Approaches
1. Pattern-Based Detection (Regex)

Best for: Quick scanning of structured data patterns
from guardrails_genie.guardrails.entity_recognition import RegexEntityRecognitionGuardrailguardrail = RegexEntityRecognitionGuardrail(should_anonymize=True)result = guardrail.guard("Contact me at john.doe@email.com")
Strengths:
- Fast execution
- No dependencies
- Perfect for well-formatted data (emails, SSNs, phone numbers)
- Easy to customize patterns
Limitations:
- Prone to false positives
- Misses contextual variations
- Limited to exact pattern matches
Recent Benchmark Results:
- High precision on structured data (emails: 1.0, phone numbers: 0.86)
- Lower recall on variations (names: 0.0, addresses: 0.28)
2. Context-Aware Detection (Presidio)

Best for: Production environments needing balanced accuracy (based on default spaCy model)
from guardrails_genie.guardrails.entity_recognition import PresidioEntityRecognitionGuardrailguardrail = PresidioEntityRecognitionGuardrail(should_anonymize=True)result = guardrail.guard("""Patient: John SmithDOB: 05/15/1980SSN: 123-45-6789""")
Strengths:
- Combines patterns with contextual rules
- Supports international formats
- Built-in anonymization
- Extensible with custom recognizers that are regex, spaCy, or Transformers based
Limitations:
- Requires additional dependencies
- Slower than pure regex
- May need tuning for specific use cases
3. ML-Based Detection (Transformers)

Best for: Complex text analysis needing high accuracy
from guardrails_genie.guardrails.entity_recognition import TransformersEntityRecognitionGuardrailguardrail = TransformersEntityRecognitionGuardrail(model_name="iiiorg/piiranha-v1-detect-personal-information",should_anonymize=True)result = guardrail.guard("""Please forward the documents to Sarah Wilsonat her new address: 123 Oak Street, Apt 4B""")
Strengths:
- Best contextual understanding
- Handles informal text well
- Detects subtle PII references
- High recall on names and locations
Limitations:
- Computationally intensive - especially when training
- Requires GPU for optimal performance
- Larger deployment footprint
Real-World Usage Examples
1. API Response Filtering
def get_user_profile(user_id: str) -> Dict:profile = database.get_profile(user_id)# Remove PII before returningguardrail = RegexEntityRecognitionGuardrail(should_anonymize=True)result = guardrail.guard(json.dumps(profile))return json.loads(result.anonymized_text)
2. Document Processing Pipeline
def process_medical_records(documents: List[str]):guardrail = PresidioEntityRecognitionGuardrail(selected_entities=["PERSON", "MEDICAL_LICENSE", "PHONE_NUMBER"],should_anonymize=True)for doc in documents:result = guardrail.guard(doc)if result.contains_entities:logger.warning(f"Found PII: {result.explanation}")doc = result.anonymized_textyield doc
3. Chat Bot Safety
def validate_bot_response(response: str) -> str:guardrail = TransformersEntityRecognitionGuardrail(should_anonymize=True)result = guardrail.guard(response)if result.contains_entities:logger.error(f"Bot leaked PII: {result.detected_entities}")return result.anonymized_textreturn response
Evaluation Results
Our benchmark tested diverse text samples containing various PII types.
AI4Privacy Dataset for PII Detection Benchmarking
The benchmark results were generated using the AI4Privacy PII Masking Dataset, which contains 400,000 text samples with annotated PII entities. This dataset is particularly valuable for evaluating PII detection systems because:
Dataset Characteristics
- Contains real-world text samples with diverse PII types
- Includes multiple languages and locales
- Each sample is professionally annotated with:
- Original source text
- Masked version of the text
- Detailed privacy mask annotations marking PII entities
- Language and locale information
Setup
- Used 100 randomly sampled validation set entries
- Each sample contains multiple PII entities of varying types
- Annotations include entity type, value, and position in text
- Evaluation metrics track precision, recall, and F1 scores at both entity and overall levels
Here's how each approach performed:
Model Performance Summary
| Model | Success Rate | Overall Precision | Overall Recall | Overall F1 |
|---|---|---|---|---|
| Regex | 0.0% | 0.03 | 0.50 | 0.06 |
| Presidio | 12.0% | 0.09 | 0.17 | 0.12 |
| Transformers | 77.0% | 0.81 | 0.83 | 0.82 |
Detailed Entity-Level F1 Scores
| Entity Type | Regex | Presidio | Transformers |
|---|---|---|---|
| 0.93 | 1.00 | 1.00 | |
| SURNAME | 0.05 | 0.00 | 0.86 |
| TELEPHONENUM | 0.00 | 0.13 | 0.82 |
| GIVENNAME | 0.08 | 0.00 | 0.90 |
| CITY | 0.06 | 0.00 | 0.92 |
| DRIVERLICENSENUM | 0.00 | 0.11 | 0.91 |
| STREET | 0.00 | 0.00 | 0.89 |
| TAXNUM | 0.00 | 0.03 | 1.00 |
| USERNAME | 0.00 | 0.00 | 0.75 |
| PASSWORD | 0.00 | 0.00 | 0.75 |
| ZIPCODE | 0.42 | 0.00 | 0.53 |
| ACCOUNTNUM | 0.24 | 0.20 | 0.77 |
| DATEOFBIRTH | 0.00 | 0.00 | 1.00 |
| IDCARDNUM | 0.00 | 0.13 | 0.67 |
| CREDITCARDNUMBER | 0.40 | 0.33 | 0.80 |
| BUILDINGNUM | 0.04 | 0.00 | 0.55 |
| SOCIALNUM | 0.22 | 0.22 | 0.50 |
Key observations:
- The Transformers model significantly outperforms both Regex and Presidio across all metrics
- All models perform well on EMAIL detection (F1 ≥ 0.93)
- Regex and Presidio have many entity types with F1 scores of 0.00
- The Transformers model achieves perfect F1 scores (1.00) for EMAIL, TAXNUM, and DATEOFBIRTH
- The Transformers model shows strong performance (F1 > 0.80) on 9 entity types
Best Practices
- Layer Your Approach
- Use regex for quick filtering
- Apply deeper analysis on flagged content
- Combine methods for critical applications
- Customize for Your Domain
- Add industry-specific patterns
- Train on your data formats
- Tune confidence thresholds
- Monitor and Iterate
- Track false positives/negatives
- Update patterns based on misses
- Regular model retraining
Getting Started
- Choose Your Guardrail:
- Simple patterns → Regex
- Production ready → Presidio
- Complex text → Transformers
- (Coming soon?) Install Dependencies:
pip install guardrails-genie
- Basic Implementation:
from guardrails_genie.guardrails.entity_recognition import RegexEntityRecognitionGuardraildef check_pii(text: str) -> bool:guardrail = RegexEntityRecognitionGuardrail(should_anonymize=True)result = guardrail.guard(text)return result.safe
This implementation provides a robust foundation for PII detection while allowing flexibility in choosing the right approach for your specific needs.
Add a comment