OpenAIAgents SDKGuardrailsTypeScript
OpenAI Agents SDK v2.0: Major Update with Advanced Guardrails
Overview
OpenAI has released version 2.0 of the Agents SDK, introducing advanced guardrails, improved TypeScript support, and new agent orchestration patterns. This major update addresses feedback from the developer community and adds enterprise-grade features.
New Features
1. Advanced Guardrails System
The new guardrails system provides comprehensive safety controls:
from openai.agents import Agent, Guardrail, GuardrailResult
# Define content guardrails
content_guardrail = Guardrail(
name="content_filter",
check=lambda input: filter_content(input),
on_violation="block" # or "warn" or "modify"
)
# Define output guardrails
output_guardrail = Guardrail(
name="output_validator",
check=lambda output: validate_output(output),
on_violation="retry"
)
agent = Agent(
name="Research Agent",
guardrails=[content_guardrail, output_guardrail]
)
Guardrail Types:
- Content Filtering: Block harmful or inappropriate inputs
- Output Validation: Ensure outputs meet quality standards
- Rate Limiting: Prevent abuse and control costs
- PII Detection: Automatically detect and redact personal information
2. Enhanced TypeScript Support
Full TypeScript support with:
- Complete type inference for agent states
- Generic agent types for type-safe tool parameters
- Zod schema integration for runtime validation
- Better IDE autocomplete and type hints
interface ResearchState {
topic: string;
sources: Source[];
findings: Finding[];
}
const agent = new Agent<ResearchState>({
name: "Research Agent",
tools: [webSearch, documentReader],
initialState: { topic: "", sources: [], findings: [] }
});
3. Agent Orchestration Patterns
New built-in orchestration patterns:
| Pattern | Description | Use Case |
|---|---|---|
| Sequential | Execute agents in order | Pipelines, workflows |
| Parallel | Execute agents concurrently | Independent tasks |
| Hierarchical | Manager-worker hierarchy | Complex multi-step tasks |
| Consensus | Multiple agents vote on output | High-stakes decisions |
| Debate | Agents argue different positions | Analysis, evaluation |
4. Improved Tool Calling
- Automatic Retry: Retry failed tool calls with exponential backoff
- Tool Chaining: Chain multiple tools with automatic state passing
- Tool Caching: Cache tool results for repeated calls
- Parallel Tool Calls: Execute independent tools in parallel
Performance Improvements
- 40% reduction in token usage for common patterns
- 50% faster tool call execution
- 60% smaller bundle size for TypeScript builds
Breaking Changes
Agent.run()now returnsRunResultinstead of raw response- Guardrails must be explicitly enabled (opt-in)
- Deprecated
agent.execute()in favor ofagent.run()
Migration Guide
# Before (v1.x)
result = agent.execute(task="Research AI trends")
# After (v2.0)
result = agent.run(task="Research AI trends")
print(result.output)
print(result.usage) # New: detailed usage tracking
