Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: mixed/openai-agents-python
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: main
Choose a base ref
...
head repository: openai/openai-agents-python
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref
Able to merge. These branches can be automatically merged.
Loading
Showing with 9,211 additions and 1,530 deletions.
  1. +1 −1 .github/ISSUE_TEMPLATE/feature_request.md
  2. +1 −1 .github/ISSUE_TEMPLATE/question.md
  3. +1 −2 .github/workflows/tests.yml
  4. +2 −1 .gitignore
  5. +7 −0 .vscode/settings.json
  6. +69 −0 AGENTS.md
  7. +5 −0 Makefile
  8. +1 −3 README.md
  9. +4 −4 docs/agents.md
  10. +1 −1 docs/config.md
  11. +15 −9 docs/examples.md
  12. +151 −0 docs/ja/agents.md
  13. +98 −0 docs/ja/config.md
  14. +81 −0 docs/ja/context.md
  15. +45 −0 docs/ja/examples.md
  16. +158 −0 docs/ja/guardrails.md
  17. +117 −0 docs/ja/handoffs.md
  18. +56 −0 docs/ja/index.md
  19. +65 −0 docs/ja/mcp.md
  20. +106 −0 docs/ja/models.md
  21. +116 −0 docs/ja/models/index.md
  22. +77 −0 docs/ja/models/litellm.md
  23. +41 −0 docs/ja/multi_agent.md
  24. +193 −0 docs/ja/quickstart.md
  25. +56 −0 docs/ja/results.md
  26. +99 −0 docs/ja/running_agents.md
  27. +91 −0 docs/ja/streaming.md
  28. +299 −0 docs/ja/tools.md
  29. +123 −0 docs/ja/tracing.md
  30. +87 −0 docs/ja/visualization.md
  31. +81 −0 docs/ja/voice/pipeline.md
  32. +198 −0 docs/ja/voice/quickstart.md
  33. +18 −0 docs/ja/voice/tracing.md
  34. +4 −3 docs/mcp.md
  35. +51 −13 docs/{models.md → models/index.md}
  36. +73 −0 docs/models/litellm.md
  37. +2 −2 docs/quickstart.md
  38. +3 −0 docs/ref/extensions/litellm.md
  39. +1 −1 docs/running_agents.md
  40. +288 −0 docs/scripts/translate_docs.py
  41. +25 −0 docs/tools.md
  42. +6 −1 docs/tracing.md
  43. +1 −3 docs/visualization.md
  44. +1 −1 examples/agent_patterns/input_guardrails.py
  45. +93 −0 examples/agent_patterns/streaming_guardrails.py
  46. +48 −0 examples/basic/local_image.py
  47. BIN examples/basic/media/image_bison.jpg
  48. +81 −0 examples/basic/non_strict_output_type.py
  49. +66 −0 examples/basic/previous_response_id.py
  50. +31 −0 examples/basic/remote_image.py
  51. +1 −1 examples/financial_research_agent/main.py
  52. +1 −1 examples/financial_research_agent/manager.py
  53. 0 examples/hosted_mcp/__init__.py
  54. +61 −0 examples/hosted_mcp/approvals.py
  55. +47 −0 examples/hosted_mcp/simple.py
  56. +1 −1 examples/mcp/filesystem_example/main.py
  57. +1 −1 examples/mcp/sse_example/README.md
  58. +1 −1 examples/mcp/sse_example/main.py
  59. +13 −0 examples/mcp/streamablehttp_example/README.md
  60. +83 −0 examples/mcp/streamablehttp_example/main.py
  61. +33 −0 examples/mcp/streamablehttp_example/server.py
  62. +41 −0 examples/model_providers/litellm_auto.py
  63. +55 −0 examples/model_providers/litellm_provider.py
  64. +5 −5 examples/research_bot/agents/search_agent.py
  65. +1 −1 examples/research_bot/manager.py
  66. +1 −1 examples/research_bot/sample_outputs/product_recs.txt
  67. +1 −1 examples/research_bot/sample_outputs/vacation.txt
  68. +34 −0 examples/tools/code_interpreter.py
  69. +5 −3 examples/tools/computer_use.py
  70. +54 −0 examples/tools/image_generator.py
  71. +123 −73 mkdocs.yml
  72. +9 −4 pyproject.toml
  73. +24 −1 src/agents/__init__.py
  74. +229 −9 src/agents/_run_impl.py
  75. +26 −6 src/agents/agent.py
  76. +58 −8 src/agents/agent_output.py
  77. +36 −5 src/agents/exceptions.py
  78. 0 src/agents/extensions/models/__init__.py
  79. +393 −0 src/agents/extensions/models/litellm_model.py
  80. +21 −0 src/agents/extensions/models/litellm_provider.py
  81. +36 −19 src/agents/extensions/visualization.py
  82. +1 −1 src/agents/function_schema.py
  83. +1 −1 src/agents/handoffs.py
  84. +60 −4 src/agents/items.py
  85. +4 −0 src/agents/mcp/__init__.py
  86. +125 −14 src/agents/mcp/server.py
  87. +28 −7 src/agents/mcp/util.py
  88. +43 −1 src/agents/model_settings.py
  89. +466 −0 src/agents/models/chatcmpl_converter.py
  90. +37 −0 src/agents/models/chatcmpl_helpers.py
  91. +316 −0 src/agents/models/chatcmpl_stream_handler.py
  92. +11 −3 src/agents/models/interface.py
  93. +144 −0 src/agents/models/multi_provider.py
  94. +83 −761 src/agents/models/openai_chatcompletions.py
  95. +67 −23 src/agents/models/openai_responses.py
  96. +71 −19 src/agents/result.py
  97. +72 −16 src/agents/run.py
  98. +3 −0 src/agents/stream_events.py
  99. +112 −2 src/agents/tool.py
  100. +6 −1 src/agents/tracing/processors.py
  101. +4 −0 src/agents/tracing/scope.py
  102. +3 −0 src/agents/tracing/setup.py
  103. +62 −2 src/agents/tracing/span_data.py
  104. +21 −1 src/agents/usage.py
  105. +12 −0 src/agents/util/_pretty_print.py
  106. +1 −1 src/agents/version.py
  107. +2 −0 src/agents/voice/__init__.py
  108. +3 −4 src/agents/voice/model.py
  109. +38 −8 tests/fake_model.py
  110. 0 tests/fastapi/__init__.py
  111. +30 −0 tests/fastapi/streaming_app.py
  112. +29 −0 tests/fastapi/test_streaming_context.py
  113. +187 −5 tests/mcp/test_mcp_util.py
  114. +1 −1 tests/mcp/test_server_errors.py
  115. +59 −0 tests/model_settings/test_serialization.py
  116. 0 tests/models/__init__.py
  117. +11 −0 tests/models/conftest.py
  118. +298 −0 tests/models/test_litellm_chatcompletions_stream.py
  119. +45 −0 tests/models/test_litellm_extra_body.py
  120. +20 −0 tests/models/test_map.py
  121. +3 −2 tests/test_agent_config.py
  122. +2 −2 tests/test_agent_hooks.py
  123. +84 −3 tests/test_agent_runner.py
  124. +116 −0 tests/test_cancel_streaming.py
  125. +100 −0 tests/test_extra_headers.py
  126. +1 −1 tests/test_function_schema.py
  127. +35 −0 tests/test_function_tool_decorator.py
  128. +6 −6 tests/test_items_helpers.py
  129. +94 −7 tests/test_openai_chatcompletions.py
  130. +38 −38 tests/test_openai_chatcompletions_converter.py
  131. +17 −2 tests/test_openai_chatcompletions_stream.py
  132. +1 −1 tests/test_openai_responses_converter.py
  133. +56 −3 tests/test_output_tool.py
  134. +119 −13 tests/test_responses_tracing.py
  135. +2 −1 tests/test_result_cast.py
  136. +44 −0 tests/test_run_error_details.py
  137. +10 −10 tests/test_run_step_execution.py
  138. +16 −16 tests/test_run_step_processing.py
  139. +5 −5 tests/test_tool_converter.py
  140. +6 −8 tests/test_tracing_errors.py
  141. +0 −4 tests/test_tracing_errors_streamed.py
  142. +52 −0 tests/test_usage.py
  143. +15 −0 tests/test_visualization.py
  144. +4 −7 tests/voice/conftest.py
  145. +9 −3 tests/voice/test_workflow.py
  146. +1,269 −336 uv.lock
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ assignees: ''
### Please read this first

- **Have you read the docs?**[Agents SDK docs](https://openai.github.io/openai-agents-python/)
- **Have you searched for related issues?** Others may have had similar requesrs
- **Have you searched for related issues?** Others may have had similar requests

### Describe the feature
What is the feature you're requesting? How would it work? Please provide examples and details if possible.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/question.md
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ assignees: ''
### Please read this first

- **Have you read the docs?**[Agents SDK docs](https://openai.github.io/openai-agents-python/)
- **Have you searched for related issues?** Others may have had similar requesrs
- **Have you searched for related issues?** Others may have had similar requests

### Question
Describe your question. Provide details if available.
3 changes: 1 addition & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -5,8 +5,7 @@ on:
branches:
- main
pull_request:
branches:
- main
# All PRs, including stacked PRs

env:
UV_FROZEN: "1"
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -141,4 +141,5 @@ cython_debug/
.ruff_cache/

# PyPI configuration file
.pypirc
.pypirc
.aider*
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"python.testing.pytestArgs": [
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
69 changes: 69 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
Welcome to the OpenAI Agents SDK repository. This file contains the main points for new contributors.

## Repository overview

- **Source code**: `src/agents/` contains the implementation.
- **Tests**: `tests/` with a short guide in `tests/README.md`.
- **Examples**: under `examples/`.
- **Documentation**: markdown pages live in `docs/` with `mkdocs.yml` controlling the site.
- **Utilities**: developer commands are defined in the `Makefile`.
- **PR template**: `.github/PULL_REQUEST_TEMPLATE/pull_request_template.md` describes the information every PR must include.

## Local workflow

1. Format, lint and type‑check your changes:

```bash
make format
make lint
make mypy
```

2. Run the tests:

```bash
make tests
```

To run a single test, use `uv run pytest -s -k <test_name>`.

3. Build the documentation (optional but recommended for docs changes):

```bash
make build-docs
```

Coverage can be generated with `make coverage`.

## Snapshot tests

Some tests rely on inline snapshots. See `tests/README.md` for details on updating them:

```bash
make snapshots-fix # update existing snapshots
make snapshots-create # create new snapshots
```

Run `make tests` again after updating snapshots to ensure they pass.

## Style notes

- Write comments as full sentences and end them with a period.

## Pull request expectations

PRs should use the template located at `.github/PULL_REQUEST_TEMPLATE/pull_request_template.md`. Provide a summary, test plan and issue number if applicable, then check that:

- New tests are added when needed.
- Documentation is updated.
- `make lint` and `make format` have been run.
- The full test suite passes.

Commit messages should be concise and written in the imperative mood. Small, focused commits are preferred.

## What reviewers look for

- Tests covering new behaviour.
- Consistent style: code formatted with `ruff format`, imports sorted, and type hints passing `mypy`.
- Clear documentation for any public API changes.
- Clean history and a helpful PR description.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -42,6 +42,11 @@ old_version_tests:
build-docs:
uv run mkdocs build

.PHONY: build-full-docs
build-full-docs:
uv run docs/scripts/translate_docs.py
uv run mkdocs build

.PHONY: serve-docs
serve-docs:
uv run mkdocs serve
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# OpenAI Agents SDK

The OpenAI Agents SDK is a lightweight yet powerful framework for building multi-agent workflows.
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.

<img src="https://cdn.openai.com/API/docs/images/orchestration.png" alt="Image of the Agents Tracing UI" style="max-height: 803px;">

@@ -13,8 +13,6 @@ The OpenAI Agents SDK is a lightweight yet powerful framework for building multi

Explore the [examples](examples) directory to see the SDK in action, and read our [documentation](https://openai.github.io/openai-agents-python/) for more details.

Notably, our SDK [is compatible](https://openai.github.io/openai-agents-python/models/) with any model providers that support the OpenAI Chat Completions API format.

## Get started

1. Set up your Python environment
8 changes: 4 additions & 4 deletions docs/agents.md
Original file line number Diff line number Diff line change
@@ -32,11 +32,11 @@ Agents are generic on their `context` type. Context is a dependency-injection to
```python
@dataclass
class UserContext:
uid: str
is_pro_user: bool
uid: str
is_pro_user: bool

async def fetch_purchases() -> list[Purchase]:
return ...
async def fetch_purchases() -> list[Purchase]:
return ...

agent = Agent[UserContext](
...,
2 changes: 1 addition & 1 deletion docs/config.md
Original file line number Diff line number Diff line change
@@ -63,7 +63,7 @@ Alternatively, you can customize the logs by adding handlers, filters, formatter
```python
import logging

logger = logging.getLogger("openai.agents") # or openai.agents.tracing for the Tracing logger
logger = logging.getLogger("openai.agents") # or openai.agents.tracing for the Tracing logger

# To make all logs show up
logger.setLevel(logging.DEBUG)
24 changes: 15 additions & 9 deletions docs/examples.md
Original file line number Diff line number Diff line change
@@ -5,32 +5,38 @@ Check out a variety of sample implementations of the SDK in the examples section

## Categories

- **agent_patterns:**
- **[agent_patterns](https://github.com/openai/openai-agents-python/tree/main/examples/agent_patterns):**
Examples in this category illustrate common agent design patterns, such as

- Deterministic workflows
- Agents as tools
- Parallel agent execution

- **basic:**
- **[basic](https://github.com/openai/openai-agents-python/tree/main/examples/basic):**
These examples showcase foundational capabilities of the SDK, such as

- Dynamic system prompts
- Streaming outputs
- Lifecycle events

- **tool examples:**
- **[tool examples](https://github.com/openai/openai-agents-python/tree/main/examples/tools):**
Learn how to implement OAI hosted tools such as web search and file search,
and integrate them into your agents.

- **model providers:**
- **[model providers](https://github.com/openai/openai-agents-python/tree/main/examples/model_providers):**
Explore how to use non-OpenAI models with the SDK.

- **handoffs:**
- **[handoffs](https://github.com/openai/openai-agents-python/tree/main/examples/handoffs):**
See practical examples of agent handoffs.

- **customer_service** and **research_bot:**
- **[mcp](https://github.com/openai/openai-agents-python/tree/main/examples/mcp):**
Learn how to build agents with MCP.

- **[customer_service](https://github.com/openai/openai-agents-python/tree/main/examples/customer_service)** and **[research_bot](https://github.com/openai/openai-agents-python/tree/main/examples/research_bot):**
Two more built-out examples that illustrate real-world applications

- **customer_service**: Example customer service system for an airline.
- **research_bot**: Simple deep research clone.

- **[voice](https://github.com/openai/openai-agents-python/tree/main/examples/voice):**
See examples of voice agents, using our TTS and STT models.
151 changes: 151 additions & 0 deletions docs/ja/agents.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
---
search:
exclude: true
---
# エージェント

エージェントはアプリの主要な構成ブロックです。エージェントは、大規模言語モデル ( LLM ) に instructions と tools を設定したものです。

## 基本設定

エージェントで最も一般的に設定するプロパティは次のとおりです。

- `instructions`: 開発者メッセージまたは system prompt とも呼ばれます。
- `model`: 使用する LLM と、temperature や top_p などのモデル調整パラメーターを指定する任意の `model_settings`
- `tools`: エージェントがタスクを達成するために利用できるツール。

```python
from agents import Agent, ModelSettings, function_tool

@function_tool
def get_weather(city: str) -> str:
return f"The weather in {city} is sunny"

agent = Agent(
name="Haiku agent",
instructions="Always respond in haiku form",
model="o3-mini",
tools=[get_weather],
)
```

## コンテキスト

エージェントはその `context` 型について汎用的です。コンテキストは依存性注入の手段で、`Runner.run()` に渡すオブジェクトです。これはすべてのエージェント、ツール、ハンドオフなどに渡され、エージェント実行時の依存関係や状態をまとめて保持します。任意の Python オブジェクトをコンテキストとして渡せます。

```python
@dataclass
class UserContext:
uid: str
is_pro_user: bool

async def fetch_purchases() -> list[Purchase]:
return ...

agent = Agent[UserContext](
...,
)
```

## 出力タイプ

デフォルトでは、エージェントはプレーンテキスト ( つまり `str` ) を出力します。特定の型で出力させたい場合は `output_type` パラメーターを使用します。一般的には [Pydantic](https://docs.pydantic.dev/) オブジェクトを利用しますが、Pydantic の [TypeAdapter](https://docs.pydantic.dev/latest/api/type_adapter/) でラップ可能な型であれば何でも対応します。たとえば dataclass、list、TypedDict などです。

```python
from pydantic import BaseModel
from agents import Agent


class CalendarEvent(BaseModel):
name: str
date: str
participants: list[str]

agent = Agent(
name="Calendar extractor",
instructions="Extract calendar events from text",
output_type=CalendarEvent,
)
```

!!! note

`output_type` を渡すと、モデルは通常のプレーンテキスト応答の代わりに [structured outputs](https://platform.openai.com/docs/guides/structured-outputs) を使用するよう指示されます。

## ハンドオフ

ハンドオフは、エージェントが委譲できるサブエージェントです。ハンドオフのリストを渡しておくと、エージェントは必要に応じてそれらに処理を委譲できます。これにより、単一のタスクに特化したモジュール式エージェントを編成できる強力なパターンが実現します。詳細は [handoffs](handoffs.md) ドキュメントをご覧ください。

```python
from agents import Agent

booking_agent = Agent(...)
refund_agent = Agent(...)

triage_agent = Agent(
name="Triage agent",
instructions=(
"Help the user with their questions."
"If they ask about booking, handoff to the booking agent."
"If they ask about refunds, handoff to the refund agent."
),
handoffs=[booking_agent, refund_agent],
)
```

## 動的 instructions

通常はエージェント作成時に instructions を指定しますが、関数を介して動的に instructions を提供することもできます。その関数はエージェントとコンテキストを受け取り、プロンプトを返す必要があります。同期関数と `async` 関数の両方に対応しています。

```python
def dynamic_instructions(
context: RunContextWrapper[UserContext], agent: Agent[UserContext]
) -> str:
return f"The user's name is {context.context.name}. Help them with their questions."


agent = Agent[UserContext](
name="Triage agent",
instructions=dynamic_instructions,
)
```

## ライフサイクルイベント (hooks)

場合によっては、エージェントのライフサイクルを観察したいことがあります。たとえば、イベントをログに記録したり、特定のイベント発生時にデータを事前取得したりする場合です。`hooks` プロパティを使ってエージェントのライフサイクルにフックできます。[`AgentHooks`][agents.lifecycle.AgentHooks] クラスをサブクラス化し、関心のあるメソッドをオーバーライドしてください。

## ガードレール

ガードレールを使うと、エージェントの実行と並行してユーザー入力に対するチェックやバリデーションを実行できます。たとえば、ユーザーの入力内容が関連しているかをスクリーニングできます。詳細は [guardrails](guardrails.md) ドキュメントをご覧ください。

## エージェントの複製

`clone()` メソッドを使用すると、エージェントを複製し、必要に応じて任意のプロパティを変更できます。

```python
pirate_agent = Agent(
name="Pirate",
instructions="Write like a pirate",
model="o3-mini",
)

robot_agent = pirate_agent.clone(
name="Robot",
instructions="Write like a robot",
)
```

## ツール使用の強制

ツールの一覧を渡しても、LLM が必ずツールを使用するとは限りません。[`ModelSettings.tool_choice`][agents.model_settings.ModelSettings.tool_choice] を設定することでツール使用を強制できます。有効な値は次のとおりです。

1. `auto` — ツールを使用するかどうかを LLM が判断します。
2. `required` — LLM にツール使用を必須化します ( ただし使用するツールは自動選択 )。
3. `none` — LLM にツールを使用しないことを要求します。
4. 特定の文字列 ( 例: `my_tool` ) — その特定のツールを LLM に使用させます。

!!! note

無限ループを防ぐため、フレームワークはツール呼び出し後に `tool_choice` を自動的に "auto" にリセットします。この動作は [`agent.reset_tool_choice`][agents.agent.Agent.reset_tool_choice] で設定できます。無限ループが起こる理由は、ツールの結果が LLM に送られ、`tool_choice` により再びツール呼び出しが生成される、という流れが繰り返されるからです。

ツール呼び出し後にエージェントを完全に停止させたい場合 ( auto モードで続行させたくない場合 ) は、[`Agent.tool_use_behavior="stop_on_first_tool"`] を設定してください。これにより、ツールの出力を LL M の追加処理なしにそのまま最終応答として返します。
Loading