Skip to content

[Draft] OpenAI Agents SDK Integration #2933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
checkpoint
  • Loading branch information
bglick13 committed Aug 5, 2025
commit 67f564da99f0e212e54a2250ba51f81c45ca1864
22 changes: 14 additions & 8 deletions clients/python/tensorzero/agents.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import contextvars
from contextlib import asynccontextmanager
from pathlib import Path
from typing import Any, Optional, TypedDict
from typing import Any, Generic, Optional, TypeVar

import toml
from pydantic import BaseModel
Expand Down Expand Up @@ -173,17 +173,23 @@ def chat(self) -> TensorZeroAgentsChat:
return TensorZeroAgentsChat(self)


class TensorZeroAgentsRunArgs(TypedDict):
system: BaseModel
user: BaseModel
SystemModelT = TypeVar("SystemModelT", bound=BaseModel)
UserModelT = TypeVar("UserModelT", bound=BaseModel)


class TensorZeroAgentsRunner(agents.Runner):
class TensorZeroAgentsRunArgs(BaseModel, Generic[SystemModelT, UserModelT]):
system: SystemModelT
user: UserModelT


class TensorZeroAgentsRunner(agents.Runner, Generic[SystemModelT, UserModelT]):
@classmethod
async def run(
cls,
starting_agent: agents.Agent[agents.TContext],
input: str | list[agents.TResponseInputItem] | TensorZeroAgentsRunArgs,
input: str
| list[agents.TResponseInputItem]
| TensorZeroAgentsRunArgs[SystemModelT, UserModelT],
*,
context: agents.TContext | None = None,
max_turns: int = agents.run.DEFAULT_MAX_TURNS,
Expand All @@ -197,8 +203,8 @@ async def run(
"TensorZeroAgentsRunner must be called within a with_tensorzero_agents_patched context"
)

if isinstance(input, dict):
args = input["system"].model_dump() | input["user"].model_dump()
if isinstance(input, TensorZeroAgentsRunArgs):
args = input.system.model_dump() | input.user.model_dump()
args = {f"tensorzero::arguments::{k}": v for k, v in args.items()}
existing_run_config = run_config or agents.run.RunConfig()
existing_model_settings = (
Expand Down
1 change: 1 addition & 0 deletions clients/python/tensorzero/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ def main(argv: list[str] | None = None) -> None:
# ---------------------------------------------------------------------
identifiers = _collect_function_identifiers(cfg_dict)
identifier_module_src = _render_identifier_module(identifiers)
output_dir.mkdir(parents=True, exist_ok=True)

(output_dir / "__init__.py").touch(exist_ok=True)
types_file = output_dir / "function_identifiers.py"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
from datetime import datetime

import dotenv
from tensorzero.agents import TensorZeroAgentsRunner, with_tensorzero_agents_patched
from tensorzero.agents import (
TensorZeroAgentsRunArgs,
TensorZeroAgentsRunner,
with_tensorzero_agents_patched,
)
from tools import answer_question, load_wikipedia_page, search_wikipedia, think

os.chdir(os.path.dirname(os.path.abspath(__file__)))
Expand Down Expand Up @@ -43,9 +47,12 @@ async def ask_question(question: str, verbose: bool = False) -> str:
# Use the Agents SDK Runner - this handles the entire tool loop automatically
system_model = SystemModel(date=datetime.now().strftime("%Y-%m-%d"))
user_model = UserModel(question=question)
result = await TensorZeroAgentsRunner.run(
result = await TensorZeroAgentsRunner[SystemModel, UserModel].run(
agent,
{"system": system_model, "user": user_model},
TensorZeroAgentsRunArgs(
system=system_model,
user=user_model,
),
max_turns=15,
)
if verbose:
Expand Down