openai-agents-python README

The project's README from github.com/openai/openai-agents-python. As one markdown file: README.md.

OpenAI Agents SDK PyPI

The OpenAI Agents SDK is a lightweight yet powerful framework for building multi-agent workflows. It is provider-agnostic, supporting the OpenAI Responses and Chat Completions APIs, as well as 100+ other LLMs.

Image of the Agents Tracing UI

[!NOTE] Looking for the JavaScript/TypeScript version? Check out Agents SDK JS/TS.

Core concepts:

  1. Agents: LLMs configured with instructions, tools, guardrails, and handoffs
  2. Sandbox agents: Agents preconfigured to work with a container to perform work over long time horizons.
  3. Realtime agents: Build powerful voice agents with gpt-realtime-2.1 and full agent features
  4. Voice agents: Build voice pipelines that combine speech-to-text, an agent workflow, and text-to-speech
  5. Agents as tools / Handoffs: Delegating to other agents for specific tasks
  6. Tools: Various Tools let agents take actions (functions, MCP, hosted tools)
  7. Guardrails: Configurable safety checks for input and output validation
  8. Human in the loop: Built-in mechanisms for involving humans across agent runs
  9. Sessions: Automatic conversation history management across agent runs
  10. Tracing: Built-in tracking of agent runs, allowing you to view, debug and optimize your workflows

Explore the examples directory to see the SDK in action, and read our documentation for more details.

Get started

To get started, set up your Python environment (Python 3.10 or newer required), and then install OpenAI Agents SDK package.

venv

python -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate
pip install openai-agents

For voice support, install with the optional voice group: pip install 'openai-agents[voice]'. For Redis session support, install with the optional redis group: pip install 'openai-agents[redis]'.

uv

If you're familiar with uv, installing the package would be even easier:

uv init
uv add openai-agents

For voice support, install with the optional voice group: uv add 'openai-agents[voice]'. For Redis session support, install with the optional redis group: uv add 'openai-agents[redis]'.

Run your first agents

The SDK supports four primary ways to run agents. Set the OPENAI_API_KEY environment variable before running any of these examples.

Run a text agent

Use a text Agent for workflows that do not need a persistent realtime connection or a sandbox workspace.

from agents import Agent, Runner

agent = Agent(name="Assistant", instructions="You are a helpful assistant")

result = Runner.run_sync(agent, "Write a haiku about recursion in programming.")
print(result.final_output)

# Code within the code,
# Functions calling themselves,
# Infinite loop's dance.

(For Jupyter notebook users, see hello_world_jupyter.ipynb)

Run a sandbox agent

Use a SandboxAgent when the agent needs to inspect files, run commands, apply patches, or preserve workspace state across longer tasks.

This example uses UnixLocalSandboxClient, which is supported on macOS and Linux. On Windows, use DockerSandboxClient with the openai-agents[docker] extra or a hosted sandbox client instead; see Sandbox clients for setup details.

from agents import Runner
from agents.run import RunConfig
from agents.sandbox import Manifest, SandboxAgent, SandboxRunConfig
from agents.sandbox.entries import GitRepo
from agents.sandbox.sandboxes import UnixLocalSandboxClient

agent = SandboxAgent(
    name="Workspace Assistant",
    instructions="Inspect the sandbox workspace before answering.",
    default_manifest=Manifest(entries={"repo": GitRepo(repo="openai/openai-agents-python", ref="main")}),
)

result = Runner.run_sync(
    agent,
    "Inspect the repo README and summarize what this project does.",
    run_config=RunConfig(sandbox=SandboxRunConfig(client=UnixLocalSandboxClient())),
)
print(result.final_output)

Run a realtime agent

Use a RealtimeAgent for low-latency, server-side voice and multimodal experiences over WebSocket.

import asyncio
from agents.realtime import RealtimeAgent, RealtimeRunner

async def main() -> None:
    agent = RealtimeAgent(name="Assistant", instructions="You are a helpful voice assistant. Keep responses short.")
    runner = RealtimeRunner(starting_agent=agent)
    session = await runner.run()

    async with session:
        await session.send_message("Say hello in one short sentence.")
        async for event in session:
            if event.type == "audio":
                # Forward or play event.audio.data.
                pass
            elif event.type == "history_added":
                print(event.item)
            elif event.type == "agent_end":
                break

if __name__ == "__main__":
    asyncio.run(main())

Run a voice agent

Use a VoicePipeline to turn audio into text, run an agent workflow, and stream generated speech.

import asyncio

import numpy as np

from agents import Agent
from agents.voice import AudioInput, SingleAgentVoiceWorkflow, VoicePipeline


async def main() -> None:
    agent = Agent(name="Assistant", instructions="You are a helpful voice assistant.")
    pipeline = VoicePipeline(workflow=SingleAgentVoiceWorkflow(agent))
    audio_input = AudioInput(buffer=np.zeros(24000 * 3, dtype=np.int16))

    result = await pipeline.run(audio_input)
    async for event in result.stream():
        if event.type == "voice_stream_event_audio":
            # Forward or play event.data.
            pass


if __name__ == "__main__":
    asyncio.run(main())

Explore the examples directory to see the SDK in action, and read our documentation for more details.

Acknowledgements

We'd like to acknowledge the excellent work of the open-source community, especially:

This library has these optional dependencies:

We also rely on the following tools to manage the project:

We're committed to continuing to build the Agents SDK as an open source framework so others in the community can expand on our approach.

openai-agents-python (openai/openai-agents-python)

Searching Database…

这是什么东西?它是 OpenAI Agents SDK:一个轻量但功能强大的 Python 框架,用于构建多智能体工作流。这里的智能体(agent)是指一个配置了指令、工具、防护机制和任务交接的大语言模型。
它是一个需要我写代码调用的框架,还是一个拿过来就能运行的现成应用?它是一个安装到你项目里的 Python SDK:你在代码里定义并运行智能体。SDK 还有一个独立的 JavaScript/TypeScript 版本。
怎么把它加到我的项目里?在 Python 3.10+ 环境中运行 pip install openai-agents(或者用 uv add openai-agents)。像 'openai-agents[voice]' 这样的可选组件可以增加语音支持、Redis 会话或 Docker 沙箱,运行之前你需要先设置 OPENAI_API_KEY 环境变量。
我是不是只能用 OpenAI 的模型?不是——这个 SDK 不绑定任何模型提供商:它支持 OpenAI 的 Responses 和 Chat Completions API,还支持 100 多种其他大语言模型,可选依赖 any-llm 和 LiteLLM。
在 Windows 上是不是所有功能开箱即用?本地沙箱不行:沙箱智能体只能在 macOS 和 Linux 上原生运行,所以在 Windows 上你需要配合 openai-agents[docker] 可选组件使用 DockerSandboxClient,或者使用托管沙箱客户端。
沙箱智能体是干什么用的?适合长时间运行的任务:它们预先配置好,可以在容器内运行,智能体在其中可以查看文件、执行命令、应用补丁,并在较长任务中保留工作区状态。
智能体之间可以互相委托任务吗?可以——你可以把一个智能体作为工具传给另一个智能体,或者使用 handoffs(任务交接),让工作流把每个任务分派给专门负责它的智能体。
运行过程中我怎么查看智能体做了什么?追踪功能是内置的:智能体的每次运行都会被自动记录,方便你查看、调试和优化工作流。
有 JavaScript 或 TypeScript 版本吗?有,README 里链接了一个独立的 Agents SDK JS/TS 包,适合非 Python 项目。
Creator
openai
Size
80.1 MB
Stars
30.1K
Age
1y
Commits · 90 Days