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:

PatternDescriptionUse Case
SequentialExecute agents in orderPipelines, workflows
ParallelExecute agents concurrentlyIndependent tasks
HierarchicalManager-worker hierarchyComplex multi-step tasks
ConsensusMultiple agents vote on outputHigh-stakes decisions
DebateAgents argue different positionsAnalysis, 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 returns RunResult instead of raw response
  • Guardrails must be explicitly enabled (opt-in)
  • Deprecated agent.execute() in favor of agent.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

Resources