OpenAI Agents SDK が (swarm の後継として) 3月12日に公開されました。サンプルの動作確認ができましたので、取り敢えず README を翻訳しておきます。
OpenAI Agents SDK : 概要
作成 : クラスキャット・セールスインフォメーション
作成日時 : 03/17/2025
* 本記事は GitHub : openai/openai-agents-python の以下のページを独自に翻訳した上でまとめ直し、補足説明を加えています :
* サンプルコードの動作確認はしておりますが、必要な場合には適宜、追加改変しています。
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。
◆ お問合せ : 下記までお願いします。
- クラスキャット セールス・インフォメーション
- sales-info@classcat.com
- ClassCatJP
OpenAI Agents SDK : 概要
OpenAI Agents SDK はマルチエージェント・ワークフローを構築するための軽量な、しかし強力なフレームワークです。
コアとなるコンセプト :
- Agents : 指示、ツール、ガードレール、ハンドオフで構成された LLM
- Handoffs : エージェントが特定のタスクのために制御を他のエージェントに移すことを可能にします。
- Guardrails : 入出力の検証のための構成可能な安全性チェック
- Tracing : エージェント実行の組み込みトラッキングで、貴方がワークフローを見て、デバッグして最適化することを可能にします。
SDK を実際に確認するには examples ディレクトリを調べてください、そして詳細は ドキュメント を読んでください。
特に、SDK は OpenAI Chat Completions API 形式をサポートするすべてのモデル・プロバイダーと 互換性があります。
Get started
- Python 環境をセットアップします。
python -m venv env source env/bin/activate
- Agents SDK をインストールします。
pip install openai-agents
Hello world サンプル
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.
実行例
Code within the code, Patterns endlessly unfold— Loops in endless dance.
Code calls itself, loops Infinite mirrors within— Logic finds its way.
Handoffs サンプル
from agents import Agent, Runner
import asyncio
spanish_agent = Agent(
name="Spanish agent",
instructions="You only speak Spanish.",
)
english_agent = Agent(
name="English agent",
instructions="You only speak English",
)
triage_agent = Agent(
name="Triage agent",
instructions="Handoff to the appropriate agent based on the language of the request.",
handoffs=[spanish_agent, english_agent],
)
async def main():
result = await Runner.run(triage_agent, input="Hola, ¿cómo estás?")
print(result.final_output)
# ¡Hola! Estoy bien, gracias por preguntar. ¿Y tú, cómo estás?
if __name__ == "__main__":
asyncio.run(main())
¡Hola! Estoy bien, gracias. ¿Y tú, cómo estás?
Functions サンプル
import asyncio
from agents import Agent, Runner, function_tool
@function_tool
def get_weather(city: str) -> str:
return f"The weather in {city} is sunny."
agent = Agent(
name="Hello world",
instructions="You are a helpful agent.",
tools=[get_weather],
)
async def main():
result = await Runner.run(agent, input="What's the weather in Tokyo?")
print(result.final_output)
# The weather in Tokyo is sunny.
if __name__ == "__main__":
asyncio.run(main())
The weather in Tokyo is sunny.
エージェント・ループ
Runner.run() を呼び出すと、最終出力を得るまでループを実行します。
- エージェントのモデルと設定、そしてメッセージ履歴を使用して LLM を呼び出します。
- LLM はレスポンスを返します、これはツール呼び出しを含む場合があります。
- レスポンスが最終出力を持つ場合 (see below for more on this)、それを返してループを終了します。
- レスポンスがハンドオフを持つ場合、エージェントを新しいエージェントに設定して、ステップ 1 に戻ります。
- (もしあれば) ツール呼び出しを処理して、ツール応答メッセージを付け加えます。それからステップ 1 に進みます。
ループの実行回数を制限するために使用できる max_turns パラメータがあります。
最終出力 (Final output)
最終出力はエージェントがループ内で生成する最後のものです。
- エージェントで output_type を設定する場合、最終出力は LLM がそのタイプのものを返したときになります。このために構造化出力 (structured outputs) を使用します。
- output_type がない場合 (i.e. plain テキスト・レスポンス)、ツール呼び出しやハンドオフがない最初の LLM レスポンスが最終出力とみなされます。
その結果、エージェントループのメンタルモデルは以下のようになります :
- 現在のエージェントが output_type を持つ場合、ループはエージェントがそのタイプに一致する構造化出力を生成するまで実行されます。
- 現在のエージェントが output_type を持たない場合、ループは現在のエージェントが (ツール呼び出し/ハンドオフのない) メッセージを生成するまで実行されます。
一般的なエージェント・パターン
Agent SDK は非常に柔軟に設計されていて、決定論的ワークフロー、反復的ループ等を含む、広範囲な LLM ワークフローをモデル化することを可能にします。See examples in examples/agent_patterns.
Tracing
Agents SDK はエージェントの実行を自動的にトレースし、エージェントの動作を追跡してデバッグすることを簡単にします。Tracing は設計上拡張可能で、(分散トレーシングの) カスタムスパンや (Logfire, AgentOps, Braintrust, Scorecard や Keywords AI を含む) 広範囲な外部 destinations をサポートしています。For more details about how to customize or disable tracing, see Tracing.
開発 (only needed if you need to edit the SDK/examples)
- uv がインストールされていることを確認してください。
uv --version
- Install dependencies
make sync
- (After making changes) lint/test
make tests # run tests make mypy # run typechecker make lint # run linter
以上