Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 22 additions & 4 deletions temporalio/contrib/openai_agents/_temporal_openai_agents.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Initialize Temporal OpenAI Agents overrides."""

from contextlib import contextmanager
from contextlib import AbstractAsyncContextManager, asynccontextmanager, contextmanager
from datetime import timedelta
from typing import AsyncIterator, Callable, Optional, Union

Expand Down Expand Up @@ -41,7 +41,13 @@
from temporalio.converter import (
DataConverter,
)
from temporalio.worker import Worker, WorkerConfig
from temporalio.worker import (
Replayer,
ReplayerConfig,
Worker,
WorkerConfig,
WorkflowReplayResult,
)


@contextmanager
Expand Down Expand Up @@ -270,7 +276,8 @@ def configure_worker(self, config: WorkerConfig) -> WorkerConfig:
]
return super().configure_worker(config)

async def run_worker(self, worker: Worker) -> None:
@asynccontextmanager
async def run_worker(self) -> AsyncIterator[None]:
Copy link
Member

@cretz cretz Aug 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

People need to be able to access the worker and its options IMO. Running a replayer is not like running a worker and should not be combined with it IMO. What if it only makes sense for me to do something for activities or Nexus operations? If we want the default implementation of run_worker and run_replayer to delegate to another method called run_worker_or_replayer or something similarly named to represent the combination, we may be able to.

"""Run the worker with OpenAI agents temporal overrides.

This method sets up the necessary runtime overrides for OpenAI agents
Expand All @@ -281,4 +288,15 @@ async def run_worker(self, worker: Worker) -> None:
worker: The worker instance to run.
"""
with set_open_ai_agent_temporal_overrides(self._model_params):
await super().run_worker(worker)
async with super().run_worker():
yield

def configure_replayer(self, config: ReplayerConfig) -> ReplayerConfig:
"""Configure the replayer for OpenAI Agents."""
config["interceptors"] = list(config.get("interceptors") or []) + [
OpenAIAgentsTracingInterceptor()
]
config["data_converter"] = DataConverter(
payload_converter_class=_OpenAIPayloadConverter
)
return config
Loading
Loading