Built-in Guardrails
PII Detection
Guardrails automatically detect and handle various types of personally identifiable information (PII) in both user inputs and agent outputs:email- Email addressescredit_card- Credit card numbers (validated with Luhn algorithm)ssn- Social Security Numbersphone- Phone numbersip_address- IP addresses (validated with stdlib)url- URLs
Content Moderation
Guardrails can detect and block toxic or harmful content using OpenAI’s moderation API:block_toxic- IfTrue, blocks toxic, harmful, or inappropriate content before it reaches the model (requiresOPENAI_API_KEYenvironment variable)
PII Handling Strategies
Each PII type can use one of these strategies to handle detected information:| Strategy | Description | Example |
|---|---|---|
"block" | Raise an error when PII is detected | Error thrown, execution stops |
"redact" | Replace with [REDACTED_<TYPE>] | [REDACTED_EMAIL] |
"mask" | Partially obscure content | ****-****-****-1234 for credit cards, u***@example.com for emails |
"hash" | Replace with deterministic SHA256 hash | <email_hash:a8f5f167...> |
None | Ignore this PII type | No action taken |
Basic Usage
Create a guardrail and add it to your agent. Guardrails automatically process both input and output:Content Moderation
Use OpenAI’s moderation API to detect and block toxic or harmful content:block_toxic- IfTrue, block toxic or harmful content using OpenAI moderation API (requiresOPENAI_API_KEYenvironment variable)
Custom Patterns
Add custom detection patterns for domain-specific sensitive information:- Compiled regex - Uses default “redact” strategy
- Tuple
(detector_func, strategy)- Custom detector function that returns a list of matches, with specified strategy