OpenAI Agents Python 21 files ยท 0 subfolders
Copy to Workspace 05_GUARDRAILS Shared from "OpenAI Agents Python" on Inkdown
Guardrails - Comprehensive Deep Dive
Overview
Guardrails are safety checks that validate input and output at various points in an agent run. Think of guardrails as "security checkpoints" or "quality gates" that ensure the agent is operating within safe and acceptable boundaries. They can prevent harmful content, validate data formats, enforce business rules, and provide custom validation logic.
Core Concepts
What are Guardrails?
Guardrails are functions that run at specific points during agent execution to:
Validate - Check that data meets certain criteria
Filter - Remove or modify inappropriate content
Block - Prevent execution when safety thresholds are crossed
Transform - Modify content to meet requirements
Types of Guardrails
Input Guardrails - Run before the agent processes input
01_AGENT_SYSTEM.md
Output Guardrails - Run after the agent produces output
Tool Input Guardrails - Run before tool execution
Tool Output Guardrails - Run after tool execution
Guardrail Anatomy
When tripwire_triggered = False - Execution continues normally
When tripwire_triggered = True - Execution is halted with an exception
The output_info can contain details about what was checked
Input Guardrails
Purpose Input guardrails validate or filter input before it reaches the LLM. They run:
Only on the first turn of a run
Only for the starting agent (not for handoffs)
Optionally in parallel with the agent (default) or before the agent starts
Basic Input Guardrail
Input Guardrail with Structured Input
Parallel vs Sequential Execution Input guardrails can run in parallel (default) or sequentially:
Independent checks that don't depend on each other
Faster execution
No order dependency
Guardrails depend on previous guardrail results
Order matters (e.g., check A must pass before check B)
Need to short-circuit on first failure
Async Input Guardrails
Input Guardrail Parameters
name - Custom name (defaults to function name)
run_in_parallel - Whether to run in parallel (default: True)
Output Guardrails
Purpose Output guardrails validate the agent's final output before it's returned. They run:
After the agent produces a final output
For every agent that produces output
Before the result is returned to the user
Basic Output Guardrail
Structured Output Validation
Content Filtering
Business Rule Validation
Tool Input Guardrails
Purpose Tool input guardrails validate arguments before a tool is executed. They run:
After the model calls a tool
Before the tool is actually executed
For each tool call individually
Basic Tool Input Guardrail
Security Validation
Schema Validation
Tool Output Guardrails
Purpose Tool output guardrails validate the results after tool execution. They run:
After the tool completes
Before the result is sent back to the model
For each tool call individually
Basic Tool Output Guardrail
Output Sanitization
Format Validation
Guardrail Execution Flow
Input Guardrail Flow
Output Guardrail Flow
Tool Guardrail Flow
Guardrail Exceptions
InputGuardrailTripwireTriggered Raised when an input guardrail tripwire is triggered:
OutputGuardrailTripwireTriggered Raised when an output guardrail tripwire is triggered:
ToolInputGuardrailTripwireTriggered Raised when a tool input guardrail tripwire is triggered:
ToolOutputGuardrailTripwireTriggered Raised when a tool output guardrail tripwire is triggered:
Guardrail Configuration
Agent-Level Guardrails Guardrails can be set on individual agents:
Run-Level Guardrails Guardrails can be set for an entire run:
Guardrail Priority Guardrails are applied in this priority:
Tool-level guardrails (for tool input/output)
Agent-level guardrails
Run-level guardrails
All guardrails at the same level run (unless configured otherwise).
Guardrail Best Practices
1. Clear Tripwire Conditions Make it clear when a guardrail should trigger:
2. Descriptive Output Info Provide helpful information in output_info:
3. Use Parallel When Possible For independent checks, use parallel execution:
4. Handle Errors Gracefully Guardrails should handle their own errors:
5. Keep Guardrails Focused Each guardrail should have a single responsibility:
Common Guardrail Patterns
1. Content Moderation
2. PII Detection
3. Format Validation
4. Business Rule Enforcement
5. Resource Limits
Guardrail and Tracing
Guardrail Spans Guardrails create trace spans for observability:
Guardrail Metadata Guardrail results are included in traces:
Summary Guardrails provide safety and validation for agent runs. Key takeaways:
Input guardrails validate input before agent processing
Output guardrails validate output after agent processing
Tool input guardrails validate tool arguments
Tool output guardrails validate tool results
Tripwires halt execution when triggered
Parallel execution improves performance for independent checks
Sequential execution for dependent checks
GuardrailFunctionOutput is the standard return type
Exceptions indicate guardrail violations
Agent-level guardrails apply to specific agents
Run-level guardrails apply to entire runs
Priority determines which guardrails apply
Async guardrails support external API calls
Structured validation for complex data
Content filtering for safety
Business rules for domain validation
Resource limits prevent abuse
Error handling should be graceful
Tracing includes guardrail results
Single responsibility keeps guardrails focused
Guardrails are essential for building safe, reliable, and compliant agent systems.