openai-agents-sdk

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

OpenAI Agents SDK (Python)

OpenAI Agents SDK(Python)

Use this skill when developing AI agents using OpenAI Agents SDK (
openai-agents
package).
当你使用OpenAI Agents SDK(
openai-agents
包)开发AI代理时,可使用本技能。

Quick Reference

快速参考

Installation

安装

bash
pip install openai-agents
bash
pip install openai-agents

Environment Variables

环境变量

bash
undefined
bash
undefined

OpenAI (direct)

OpenAI(直接调用)

OPENAI_API_KEY=sk-... LLM_PROVIDER=openai
OPENAI_API_KEY=sk-... LLM_PROVIDER=openai

Azure OpenAI (via LiteLLM)

Azure OpenAI(通过LiteLLM)

LLM_PROVIDER=azure AZURE_API_KEY=... AZURE_API_BASE=https://your-resource.openai.azure.com AZURE_API_VERSION=2024-12-01-preview
undefined
LLM_PROVIDER=azure AZURE_API_KEY=... AZURE_API_BASE=https://your-resource.openai.azure.com AZURE_API_VERSION=2024-12-01-preview
undefined

Basic Agent

基础代理

python
from agents import Agent, Runner

agent = Agent(
    name="Assistant",
    instructions="You are a helpful assistant.",
    model="gpt-5.2",  # or "gpt-5", "gpt-5.2-nano"
)
python
from agents import Agent, Runner

agent = Agent(
    name="Assistant",
    instructions="You are a helpful assistant.",
    model="gpt-5.2",  # 或 "gpt-5", "gpt-5.2-nano"
)

Synchronous

同步调用

result = Runner.run_sync(agent, "Tell me a joke") print(result.final_output)
result = Runner.run_sync(agent, "Tell me a joke") print(result.final_output)

Asynchronous

异步调用

result = await Runner.run(agent, "Tell me a joke")
undefined
result = await Runner.run(agent, "Tell me a joke")
undefined

Key Patterns

核心模式

PatternPurpose
Basic AgentSimple Q&A with instructions
Azure/LiteLLMAzure OpenAI integration
AgentOutputSchemaStrict JSON validation with Pydantic
Function ToolsExternal actions (@function_tool)
StreamingReal-time UI (Runner.run_streamed)
HandoffsSpecialized agents, delegation
Agents as ToolsOrchestration (agent.as_tool)
LLM as JudgeIterative improvement loop
GuardrailsInput/output validation
SessionsAutomatic conversation history
Multi-Agent PipelineMulti-step workflows
模式用途
基础代理带指令的简单问答
Azure/LiteLLMAzure OpenAI集成
AgentOutputSchema基于Pydantic的严格JSON验证
函数工具外部操作(@function_tool)
流式传输实时UI(Runner.run_streamed)
任务移交专业代理、任务委派
代理作为工具编排管理(agent.as_tool)
LLM作为评判者迭代改进循环
安全护栏输入/输出验证
会话自动对话历史
多代理流水线多步骤工作流

Reference Documentation

参考文档

For detailed information, see:
  • agents.md - Agent creation, Azure/LiteLLM integration
  • tools.md - Function tools, hosted tools, agents as tools
  • structured-output.md - Pydantic output, AgentOutputSchema
  • streaming.md - Streaming patterns, SSE with FastAPI
  • handoffs.md - Agent delegation
  • guardrails.md - Input/output validation
  • sessions.md - Sessions, conversation history
  • patterns.md - Multi-agent workflows, LLM as judge, tracing
如需详细信息,请查看:
  • agents.md - 代理创建、Azure/LiteLLM集成
  • tools.md - 函数工具、托管工具、代理作为工具
  • structured-output.md - Pydantic输出、AgentOutputSchema
  • streaming.md - 流式传输模式、FastAPI结合SSE
  • handoffs.md - 代理任务委派
  • guardrails.md - 输入/输出验证
  • sessions.md - 会话、对话历史
  • patterns.md - 多代理工作流、LLM作为评判者、追踪

Official Documentation

官方文档