Skip to main content
Guardrails help ensure agents operate safely and within defined boundaries. They validate inputs, filter outputs, detect sensitive information, and enforce policies that prevent unwanted or harmful behavior before it causes problems. Use guardrails when handling user data, processing sensitive information, building public-facing agents, or operating in regulated industries.

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 addresses
  • credit_card - Credit card numbers (validated with Luhn algorithm)
  • ssn - Social Security Numbers
  • phone - Phone numbers
  • ip_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 - If True, blocks toxic, harmful, or inappropriate content before it reaches the model (requires OPENAI_API_KEY environment variable)

PII Handling Strategies

Each PII type can use one of these strategies to handle detected information:

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:
Configuration:
  • block_toxic - If True, block toxic or harmful content using OpenAI moderation API (requires OPENAI_API_KEY environment variable)

Custom Patterns

Add custom detection patterns for domain-specific sensitive information:
Custom Pattern Formats:
  • Compiled regex - Uses default “redact” strategy
  • Tuple (detector_func, strategy) - Custom detector function that returns a list of matches, with specified strategy