Metadata-Version: 2.4
Name: temporalio-openai-agents
Version: 2.0.0
Summary: Temporal integration for the OpenAI Agents SDK (temporalio.openai_agents)
Author: Temporal Technologies Inc
Author-email: Temporal Technologies Inc <sdk@temporal.io>
License-Expression: MIT
License-File: LICENSE
Classifier: Development Status :: 5 - Production/Stable
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Typing :: Typed
Requires-Dist: temporalio[opentelemetry,pydantic]>=1.36.0,<2
Requires-Dist: openai-agents>=0.22,<0.25
Requires-Dist: openai>=3,<4
Requires-Dist: mcp>=1.24,<3
Requires-Dist: pydantic>=2,<3
Requires-Dist: typing-extensions>=4.2,<5
Requires-Dist: mcp>=2,<3 ; extra == 'mcp'
Requires-Dist: mcp-types>=2,<3 ; extra == 'mcp'
Requires-Dist: temporalio-mcp>=0.4.0,<0.5 ; extra == 'mcp'
Requires-Dist: openinference-instrumentation-openai-agents>=2,<3 ; extra == 'openinference'
Requires-Dist: opentelemetry-sdk>=1.26,<2 ; extra == 'openinference'
Requires-Python: >=3.11
Project-URL: Homepage, https://github.com/temporalio/ai-integrations/tree/main/python/openai_agents
Project-URL: Repository, https://github.com/temporalio/ai-integrations
Project-URL: Documentation, https://docs.temporal.io/develop/python/integrations/openai-agents
Provides-Extra: mcp
Provides-Extra: openinference
Description-Content-Type: text/markdown

# OpenAI Agents SDK Integration for Temporal

> Release stage: [Generally Available](https://temporal.io/blog/announcing-openai-agents-sdk-integration).

We welcome questions and feedback in the [#python-sdk](https://temporalio.slack.com/archives/CTT84RS0P) Slack channel at [temporalio.slack.com](https://temporalio.slack.com/).

## Install

```bash
uv add temporalio-openai-agents
```

This integration requires Python 3.11+ and Temporal 1.36 or later. The SDK extends its package
search path to support split-directory installations, including editable
installs, separate user and system sites, layered deployments, and `--target`
installs.

LiteLLM models require Python 3.11 through 3.14: LiteLLM's
[`fastuuid`](https://pypi.org/project/fastuuid/) dependency does not yet support
Python 3.15. Other model providers can use Python 3.15.

## Introduction

This integration combines [OpenAI Agents SDK](https://github.com/openai/openai-agents-python) with [Temporal's durable execution](https://docs.temporal.io/evaluate/understanding-temporal#durable-execution).
It allows you to build durable agents that never lose their progress and handle long-running, asynchronous, and human-in-the-loop workflows with production-grade reliability.

Temporal and OpenAI Agents SDK are complementary technologies, both of which contribute to simplifying what it takes to build highly capable, high-quality AI systems.
Temporal provides a crash-proof system foundation, taking care of the distributed systems challenges inherent to production agentic systems.
OpenAI Agents SDK offers a lightweight yet powerful framework for defining those agents.

This document is organized as follows:

- **[Hello World Durable Agent](#hello-world-durable-agent).** Your first durable agent example.
- **[Background Concepts](#core-concepts).** Background on durable execution and AI agents.
- **[Full Example](#full-example)** Running the Hello World Durable Agent example.
- **[Tool Calling](#tool-calling).** Calling agent Tools in Temporal.
- **[Sandbox Support](#sandbox-support).** Running sandbox agents in Temporal.
- **[Feature Support](#feature-support).** Compatibility matrix.

The [samples repository](https://github.com/temporalio/samples-python/tree/main/openai_agents) contains examples including basic usage, common agent patterns, and more complete samples.

## Hello World Durable Agent

The code below shows how to wrap an agent for durable execution.

### File 1: Durable Agent (`hello_world.py`)

```python
from temporalio import workflow
from agents import Agent, Runner

@workflow.defn
class HelloWorldAgent:
    @workflow.run
    async def run(self, prompt: str) -> str:
        agent = Agent(
            name="Assistant",
            instructions="You only respond in haikus.",
        )

        result = await Runner.run(agent, input=prompt)
        return result.final_output
```

In this example, Temporal provides the durable execution wrapper: the `HelloWorldAgent.run` method.
The content of that method, is regular OpenAI Agents SDK code.

If you are familiar with Temporal and with Open AI Agents SDK, this code will look very familiar.
The `@workflow.defn` annotation on the `HelloWorldAgent` indicates that this class will contain durable execution logic. The `@workflow.run` annotation defines the entry point.
We use the `Agent` class from OpenAI Agents SDK to define a simple agent, instructing it to always respond with haikus.
We then run that agent, using the `Runner` class from OpenAI Agents SDK, passing through `prompt` as an argument.

We will [complete this example below](#full-example).
Before digging further into the code, we will review some background that will make it easier to understand.

## Background Concepts

We encourage you to review this section thoroughly to gain a solid understanding of AI agents and durable execution with Temporal.
This knowledge will make it easier to design and build durable agents.
If you are already well versed in these topics, feel free to skim this section or skip ahead.

### AI Agents

In the OpenAI Agents SDK, an agent is an AI model configured with instructions, tools, MCP servers, guardrails, handoffs, context, and more.

We describe each of these briefly:

- _AI model_. An LLM such as OpenAI's GPT, Google's Gemini, or one of many others.
- _Instructions_. Also known as a system prompt, the instructions contain the initial input to the model, which configures it for the job it will do.
- _Tools_. Typically, Python functions that the model may choose to invoke. Tools are functions with text-descriptions that explain their functionality to the model.
- _MCP servers_. Best known for providing tools, MCP offers a pluggable standard for interoperability, including file-like resources, prompt templates, and human approvals. MCP servers may be accessed over the network or run in a local process.
- _Guardrails_. Checks on the input or the output of an agent to ensure compliance or safety. Guardrails may be implemented as regular code or as AI agents.
- _Handoffs_. A handoff occurs when an agent delegates a task to another agent. During a handoff the conversation history remains the same, and passes to a new agent with its own model, instructions, tools.
- _Context_. This is an overloaded term. Here, context refers to a framework object that is shared across tools and other code, but is not passed to the model.

Now, let's see how these components work together.
In a common pattern, the model first receives user input and then reasons about which tool to invoke.
The tool's response is passed back to the model, which may call additional tools, repeating this loop until the task is complete.

The diagram below illustrates this flow.

```text
           +-------------------+
           |     User Input    |
           +-------------------+
                     |
                     v
          +---------------------+
          |  Reasoning (Model)  |  <--+
          +---------------------+     |
                     |                |
           (decides which action)     |
                     v                |
          +---------------------+     |
          |       Action        |     |
          | (e.g., use a Tool)  |     |
          +---------------------+     |
                     |                |
                     v                |
          +---------------------+     |
          |     Observation     |     |
          |    (Tool Output)    |     |
          +---------------------+     |
                     |                |
                     +----------------+
          (loop: uses new info to reason
           again, until task is complete)
```

Even in a simple example like this, there are many places where things can go wrong.
Tools call APIs that sometimes fail, while models can encounter rate limits, requiring retries.
The longer the agent runs, the more costly it is to start the job over.
We next describe durable execution, which handles such failures seamlessly.

### Durable Execution

In Temporal's durable execution implementation, a program that crashes or encounters an exception while interacting with a model or API will retry until it can successfully complete.

Temporal relies primarily on a replay mechanism to recover from failures.
As the program makes progress, Temporal saves key inputs and decisions, allowing a re-started program to pick up right where it left off.

The key to making this work is to separate the applications repeatable (deterministic) and non-repeatable (non-deterministic) parts:

1. Deterministic pieces, termed _workflows_, execute the same way when re-run with the same inputs.
2. Non-deterministic pieces, termed _activities_, can run arbitrary code, performing I/O and any other operations.

Workflow code can run for extended periods and, if interrupted, resume exactly where it left off.
Activity code faces no restrictions on I/O or external interactions, but if it fails part-way through it restarts from the beginning.

In this integration, model invocations are automatically routed through Temporal activities, while the logic that coordinates them lives in the workflow.
Tools that perform I/O or other non-deterministic work should run as Temporal activities, while deterministic, workflow-safe tools can run directly in the workflow.
This pattern generalizes to more sophisticated agents.
We refer to the coordinating logic as _agent orchestration_.

The diagram below shows the overall architecture of an agentic application in Temporal.
The Temporal Server is responsible to tracking program execution and making sure associated state is preserved reliably (i.e., stored to a database, possibly replicated across cloud regions).
Temporal Server manages data in encrypted form, so all data processing occurs on the Worker, which runs the workflow and activities.

```text
            +---------------------+
            |   Temporal Server   |      (Stores workflow state,
            +---------------------+       schedules activities,
                     ^                    persists progress)
                     |
        Save state,  |   Schedule Tasks,
        progress,    |   load state on resume
        timeouts     |
                     |
+------------------------------------------------------+
|                      Worker                          |
|   +----------------------------------------------+   |
|   |              Workflow Code                   |   |
|   |  (Agent orchestration + deterministic tools) |   |
|   +----------------------------------------------+   |
|          |          |                |               |
|          v          v                v               |
|   +-----------+ +-----------+ +-------------+        |
|   | Activity  | | Activity  | |  Activity   |        |
|   | (I/O Tool | | (I/O Tool | | (Model API) |        |
|   |     1)    | |     2)    | |             |        |
|   +-----------+ +-----------+ +-------------+        |
|         |           |                |               |
+------------------------------------------------------+
          |           |                |
          v           v                v
      [External APIs, services, databases, etc.]
```

See the [Temporal documentation](https://docs.temporal.io/evaluate/understanding-temporal#temporal-application-the-building-blocks) for more information.

## Complete Example

To make the [Hello World durable agent](#hello-world-durable-agent) shown earlier available in Temporal, we need to create a worker program.
To see it run, we also need a client to launch it.
We show these files below.

### File 2: Launch Worker (`run_worker.py`)

```python
# File: run_worker.py

import asyncio
from datetime import timedelta

from temporalio.client import Client
from temporalio.openai_agents import OpenAIAgentsPlugin, ModelActivityParameters
from temporalio.worker import Worker

from hello_world_workflow import HelloWorldAgent


async def worker_main():
    # Use the plugin to configure Temporal for use with OpenAI Agents SDK
    client = await Client.connect(
        "localhost:7233",
        plugins=[
            OpenAIAgentsPlugin(
                model_params=ModelActivityParameters(
                    start_to_close_timeout=timedelta(seconds=30)
                )
            ),
        ],
    )

    worker = Worker(
        client,
        task_queue="my-task-queue",
        workflows=[HelloWorldAgent],
    )
    await worker.run()


if __name__ == "__main__":
    asyncio.run(worker_main())
```

We use the `OpenAIAgentsPlugin` to configure Temporal for use with OpenAI Agents SDK.
The plugin automatically handles several important setup tasks:

- Ensures proper serialization of Pydantic types
- Propagates context for [OpenAI Agents tracing](https://openai.github.io/openai-agents-python/tracing/).
- Registers an activity for invoking model calls with the Temporal worker.
- Configures OpenAI Agents SDK to run model calls as Temporal activities.

OpenAI's response models can lag behind the live API, so a value that holds a model response can
fail strict validation when it's decoded. When that happens, the plugin builds the OpenAI models in
it without validating them, like the OpenAI SDK does with live responses. If the value holds one of
your own Pydantic models, Pydantic validates it from the decoded JSON, and the result has to encode
back to the same JSON. If that's not possible, decoding fails with the original validation error.
Before 2.0.0, you got a plain `dict` instead.

**Important:** if you upgrade from 1.x, Workflow code that read such a value as a `dict` fails, and
a Signal whose argument can't be decoded is dropped. Keep runs that started on 1.x on that version
with Worker Versioning, or let them finish first.

### File 3: Client Execution (`run_hello_world_workflow.py`)

```python
# File: run_hello_world_workflow.py

import asyncio

from temporalio.client import Client
from temporalio.common import WorkflowIDReusePolicy
from temporalio.openai_agents import OpenAIAgentsPlugin

from hello_world_workflow import HelloWorldAgent

async def main():
    # Create client connected to server at the given address
    client = await Client.connect(
        "localhost:7233",
        plugins=[OpenAIAgentsPlugin()],
    )

    # Execute a workflow
    result = await client.execute_workflow(
        HelloWorldAgent.run,
        "Tell me about recursion in programming.",
        id="my-workflow-id",
        task_queue="my-task-queue",
        id_reuse_policy=WorkflowIDReusePolicy.ALLOW_DUPLICATE,
        id_conflict_policy=WorkflowIDConflictPolicy.TERMINATE_EXISTING,
    )
    print(f"Result: {result}")

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

This file is a standard Temporal launch script.
We also configure the client with the `OpenAIAgentsPlugin` to ensure serialization is compatible with the worker.

To run this example, see the detailed instructions in the [Temporal Python Samples Repository](https://github.com/temporalio/samples-python/tree/main/openai_agents).

## Tool Calling

Model invocations are automatically routed through Temporal activities.
OpenAI-hosted tools are passed through the model invocation and executed by the model provider.
User-defined `FunctionTool`s, including tools created with `@function_tool`, are not automatically converted into Temporal activities; they execute in the workflow unless explicitly backed by a Temporal activity.
Where a tool executes depends on how it is defined:

| Tool | Execution | Use for |
| --- | --- | --- |
| `activity_as_tool()` | Temporal activity | External I/O and non-deterministic operations |
| `FunctionTool` / `@function_tool` | Workflow | Deterministic, workflow-safe computation |
| OpenAI-hosted tool | Model provider | Provider-hosted features executed as part of the model invocation |

### Temporal Activities as OpenAI Agents Tools

One of the powerful features of this integration is the ability to convert Temporal activities into agent tools using `activity_as_tool`.
This allows your agent to leverage Temporal's durable execution for tool calls.
`activity_as_tool()` creates an OpenAI Agents `FunctionTool` whose invocation schedules the underlying Temporal activity.

In the example below, we apply the `@activity.defn` decorator to the `get_weather` function to create a Temporal activity.
We then pass this through the `activity_as_tool` helper function to create an OpenAI Agents tool that is passed to the `Agent`.

```python
from dataclasses import dataclass
from datetime import timedelta
from temporalio import activity, openai_agents, workflow
from agents import Agent, Runner

@dataclass
class Weather:
    city: str
    temperature_range: str
    conditions: str

@activity.defn
async def get_weather(city: str) -> Weather:
    """Get the weather for a given city."""
    return Weather(city=city, temperature_range="14-20C", conditions="Sunny with wind.")

@workflow.defn
class WeatherAgent:
    @workflow.run
    async def run(self, question: str) -> str:
        agent = Agent(
            name="Weather Assistant",
            instructions="You are a helpful weather agent.",
            tools=[
                openai_agents.workflow.activity_as_tool(
                    get_weather,
                    start_to_close_timeout=timedelta(seconds=10)
                )
            ],
        )
        result = await Runner.run(starting_agent=agent, input=question)
        return result.final_output
```

The activity must also be registered with a Worker.
`activity_as_tool()` controls how the Agent invokes the activity; it does not register the activity with the Worker.

```python
from temporalio.worker import Worker

worker = Worker(
    client,
    task_queue="my-task-queue",
    workflows=[WeatherAgent],
    activities=[get_weather],
)
```

If the activity fails, the agent run fails with a retryable `AgentsWorkflowError`.
This is how plain Temporal treats an activity failure that the workflow does not catch.
So a workflow retry policy reruns the whole agent run, model calls included, even when the activity's error was non-retryable.
To stop the rerun, catch the error around the run and raise a non-retryable `ApplicationError` in its place:

```python
from temporalio.exceptions import ActivityError, ApplicationError, RetryState
from temporalio.openai_agents import AgentsWorkflowError

@workflow.defn
class WeatherAgent:
    @workflow.run
    async def run(self, question: str) -> str:
        agent = ...  # as above
        try:
            result = await Runner.run(starting_agent=agent, input=question)
        except AgentsWorkflowError as e:
            failure = e.__cause__  # the tool's ActivityError
            if (
                isinstance(failure, ActivityError)
                and failure.retry_state == RetryState.NON_RETRYABLE_FAILURE
            ):
                raise ApplicationError(str(failure.cause), non_retryable=True) from e
            raise
        return result.final_output
```

With `Runner.run_streamed`, put the `async for` loop over `result.stream_events()` inside the `try`: the error is raised there.
When a tool's activity fails, the `AgentsWorkflowError`'s `__cause__` is that tool's `ActivityError`.
`RetryState.NON_RETRYABLE_FAILURE` covers an error raised with `non_retryable=True` and one whose type is in the activity retry policy's `non_retryable_error_types`.
The `failure_error_function` of `activity_as_tool()` does not change any of this: it only formats the error returned to the model when the tool's arguments are invalid.

### Calling OpenAI Agents Tools inside Temporal Workflows

For simple computations that don't involve external calls, you can call the tool directly from the workflow by using the standard OpenAI Agents SDK `@function_tool` decorator.

```python
from temporalio import workflow
from agents import Agent, Runner
from agents import function_tool

@function_tool
def calculate_circle_area(radius: float) -> float:
    """Calculate the area of a circle given its radius."""
    import math
    return math.pi * radius ** 2

@workflow.defn
class MathAssistantAgent:
    @workflow.run
    async def run(self, message: str) -> str:
        agent = Agent(
            name="Math Assistant",
            instructions="You are a helpful math assistant. Use the available tools to help with calculations.",
            tools=[calculate_circle_area],
        )
        result = await Runner.run(agent, input=message)
        return result.final_output
```

Use regular `@function_tool` tools only for deterministic, workflow-safe logic.
Do not perform network, database, filesystem, or other external I/O directly from these tools.
Use a Temporal activity with `activity_as_tool()` instead.
Code running in the workflow can also invoke a Temporal activity directly when needed.
Sync tools run directly in the workflow, not in a separate thread as they do outside Temporal.
They run again on every replay, and the Workflow Task waits for them: a tool that takes more than two seconds fails the Workflow Task with a deadlock error.
Use `activity_as_tool()` for slow work.

Tools that run in the workflow can also update OpenAI Agents context, which is read-only for tools run as Temporal activities.

### Resuming a `RunState` after tool approvals

A workflow can pause a run for tool approval (`@function_tool(needs_approval=True)`) and resume it later,
including across `continue_as_new`, by serializing the state with `RunState.to_string()` and restoring it
with the regular Agents SDK API. Model calls from the resumed run still execute as Temporal activities.

```python
result = await Runner.run(agent, "Transfer $5")
state_string = result.to_state().to_string()  # carry this over continue_as_new

# later, possibly in a new run
state = await RunState.from_string(agent, state_string)
for interruption in state.get_interruptions():
    state.approve(interruption)
result = await Runner.run(agent, state)  # or Runner.run_streamed(agent, state)
```

### Carrying input items across `continue_as_new`

A workflow can also carry `result.to_input_list()` across `continue_as_new` and pass the items to
`Runner.run` in the next run. The plugin decodes their `Iterable` fields, such as an assistant
message's `content`, as lists.

To do this, importing `temporalio.openai_agents` changes the `Iterable[X]` fields of the `openai`
input item types (the `TypedDict`s in `TResponseInputItem`, except tool definitions such as
`FileSearchToolParam`) to `list[X]` for the whole process. A type that an item shares with a tool
definition changes for the tool too: with `openai` 3.8.0, that is `LocalEnvironmentParam`, so the
shell tool's local `skills` is also a `list`. Type checkers still see `Iterable`. At
runtime, `typing.get_type_hints()` reports `list`, your own pydantic validation of those types
returns lists, and in strict mode it rejects a tuple or generator there. The `openai` client also
stops turning an iterator in those fields into a list when it builds a request, so
`client.responses.create(...)` with, for example, an assistant message's `content` given as
`iter([...])` raises `TypeError: Object of type list_iterator is not JSON serializable`. Pass lists
there, for example with `list(...)`.

The change reaches only classes built after `temporalio.openai_agents` is imported, so import it
before the modules that define your Workflow inputs, Activity results or other classes that hold
input items. Classes it does not reach, such as a class built earlier or, on Python 3.14, a
`TypedDict` subclass of an item type, rely on the payload converter, which lists the iterators it
finds when it decodes them. Tool definitions inside items, such as a `tool_search_output` item's
tools, also rely on it, so inside a tuple, deque or set they stay one-shot iterators.

The payload converter also decodes your own `Iterable` fields as lists when they are the decoded
value itself, fields of a dataclass or pydantic model (extra fields too), dict values or list items,
so code that calls `next()` on them must use `iter()` first. Inside tuples, deques and sets they
stay pydantic's one-shot iterators.

Earlier versions decoded those fields as one-shot iterators, and a turn on carried items raised
`TypeError: cannot pickle 'pydantic_core._pydantic_core.ValidatorIterator' object`. A run that
caught that error and went on, such as a chat whose Update reports errors to the caller, fails with
`[TMPRL1100]` on its next Workflow Task after you deploy this version. Let those runs finish on the
earlier version first, or keep them there with Worker Versioning. A chat that waits for its next
message may never finish, so use Worker Versioning for it. Do not roll a Worker back to an earlier
version while runs that ran a turn on carried items with this version are open: they fail replay
there.

## MCP Support

The durable MCP integration uses MCP Python SDK v2 and the optional `mcp`
dependencies:

```bash
uv add "temporalio-openai-agents[mcp]"
```

Register named OpenAI MCP server factories on the worker, then reference the
same name from workflow code:

```python
from agents.mcp import MCPServerStreamableHttp

from temporalio.openai_agents import OpenAIAgentsPlugin

plugin = OpenAIAgentsPlugin(
    mcp_servers={
        "weather": lambda: MCPServerStreamableHttp(
            name="weather",
            params={"url": "https://example.com/mcp"},
        ),
    },
)
```

```python
from agents import Agent
from temporalio.openai_agents.workflow import temporal_mcp_server

server = temporal_mcp_server("weather")
agent = Agent(name="weather", mcp_servers=[server])
```

Configure transport, connection, retry, and message-handling behavior on the
worker-side OpenAI `MCPServer`. Configure workflow-facing behavior such as
`tool_filter`, `require_approval`, `failure_error_function`,
`use_structured_content`, metadata resolvers, `custom_data_extractor`, and, from
openai-agents 0.22.3, tool input and output guardrails on
`temporal_mcp_server(...)`, where the OpenAI agent can use it. On the
worker-side server, a static `tool_filter` still filters the listed tools, a
callable `tool_filter`, a `require_approval` policy, a `tool_meta_resolver` and
tool guardrails are rejected as described below, and the other settings are
ignored. Custom `MCPServer` method implementations still execute worker-side.

A callable `tool_filter` on the worker-side server is rejected, because the run
context and agent it receives exist only in the workflow. A callable
`tool_filter` passed to `temporal_mcp_server(...)` therefore executes during
workflow replay and must be deterministic: it must not perform I/O or depend on
the system clock, randomness, mutable global state, or other external state.

A `require_approval` policy on the worker-side server is also rejected: without
a workflow-side policy, its tools would run without approval. Only a policy the
Agents SDK normalizes to no approval is allowed there: `"never"`, `False`, or a
mapping or tool list with only `"never"` entries. A callable policy is always
rejected, because it cannot be evaluated on the worker.

A `tool_meta_resolver` on the worker-side server is rejected too, because the
run context it receives exists only in the workflow. Pass it to
`temporal_mcp_server(tool_meta_resolver=...)` instead, where it runs in the
workflow and must be deterministic. Its result is recorded in workflow history,
so it must not hold secrets.

Tool input and output guardrails on the worker-side server are rejected as well:
the agent never reads that copy, so they would never run. Pass them to
`temporal_mcp_server(tool_input_guardrails=..., tool_output_guardrails=...)`
instead, where they run in the workflow and must be deterministic.

These checks run in every MCP Activity. A run that already holds its cached tool
list, the default, first meets them when it calls a tool: the agent gets the
error as the tool's output, and the run can complete without the tool. Let open
runs finish before you upgrade or add such a setting to the factory's server.
A resolver or guardrail there never had an effect, so removing it from the
factory's server in the same deploy is enough.
Moving the policy into `temporal_mcp_server(require_approval=...)` is also a
workflow change: an open run that already called one of that server's tools
fails replay with a nondeterminism error. Moving a resolver or guardrail there
is a workflow change too, so guard it with `workflow.patched()` or let open runs
finish first.

Every MCP operation is a Temporal Activity. The workflow-side tool list is
cached by default; pass `cache_tools_list=False` to refresh it on every Agents
SDK listing. A parameterless factory's connection is also reused for up to five
idle minutes when it negotiates the modern, sessionless protocol. Set
`mcp_connection_idle_timeout=None` to keep cached modern connections until
plugin shutdown, or `timedelta(0)` to close them as soon as they become idle.
Connections that negotiate a legacy handshake are closed after the current
Activity and are never shared across workflows.

`OpenAIAgentsPlugin(mcp_servers={"x": ...})` and
`MCPPlugin(clients={"x": ...})` both register Activities under
`temporalio.mcp.x.*`. Do not register the same MCP server name through
both plugins on one worker; Temporal rejects the duplicate Activity types.

A factory that returns something other than an `agents.mcp.MCPServer` fails
the Activity with a non-retryable `ApplicationError` of type
`MCPServerFactoryTypeError`. A factory that isn't callable makes
`OpenAIAgentsPlugin(...)` raise `TypeError`, so the Worker doesn't start, even
when no workflow uses that server.

An optional `factory_argument` can select worker-side configuration such as a
tenant endpoint:

```python
server = temporal_mcp_server(
    "weather",
    factory_argument={"tenant": "acme"},
)
```

A non-`None` argument creates a fresh client for each Activity. Omitting the
argument or passing `None` uses the zero-argument factory and permits connection
caching. Caching by only the registered name could otherwise
reuse one tenant's endpoint or authorization for another tenant; caching by the
argument itself is unsafe because arguments may be unhashable, high-cardinality,
or refer to configuration that changes worker-side. The argument is recorded in
workflow history, so it must be a non-secret stable identifier. Resolve secrets
inside the factory.

MCP v2 uses `httpx2`, not legacy `httpx`. OpenAI Agents owns the custom HTTP
client lifecycle supplied through its MCP server parameters:

```python
import httpx2
from agents.mcp import MCPServerStreamableHttp


def http_client_factory(headers=None, timeout=None, auth=None):
    return httpx2.AsyncClient(
        headers={**(headers or {}), "X-Client": "temporal"},
        timeout=timeout,
        auth=auth,
    )


def weather_server() -> MCPServerStreamableHttp:
    return MCPServerStreamableHttp(
        name="weather",
        params={
            "url": "https://example.com/mcp",
            "httpx_client_factory": http_client_factory,
        },
    )
```

`StatelessMCPServerProvider`, `StatefulMCPServerProvider`, the plugin's
`mcp_server_providers` option, `stateless_mcp_server()`, and
`stateful_mcp_server()` are deprecated. They remain supported for source and
workflow-history compatibility and can still run with MCP Python SDK v1 when
the `mcp` extra is not installed. New integrations should use `mcp_servers` and
`temporal_mcp_server()`, which require the `mcp` extra and MCP Python SDK v2.
The legacy stateful path retains its dedicated per-workflow worker and
persistent-session behavior. Both providers raise `TypeError` for a factory
that isn't callable, so the Worker doesn't start.
To require tool approval on these servers, pass
`require_approval` to `stateless_mcp_server()` or `stateful_mcp_server()`;
as with `temporal_mcp_server()`, a policy on the factory's server is rejected
unless it never asks for approval. So are a callable `tool_filter`, a
`tool_meta_resolver` and tool guardrails there, in every MCP operation, direct
`call_tool()` and prompt calls included; a static filter still works. On these
routes, set a resolver or guardrails on the server that `stateless_mcp_server()`
or `stateful_mcp_server()` gives the workflow (`server.tool_meta_resolver = ...`,
`server.tool_input_guardrails = [...]`); a resolver then gets
`<name>-stateless` or `<name>-stateful` as `server_name`. As with
`temporal_mcp_server()`, they run in the workflow and must be deterministic, a
resolver's result is recorded in workflow history, and adding them to a workflow
with open runs needs `workflow.patched()` or those runs to finish first. Remove
such a filter, resolver or guardrail from the factory in the same deploy, or let
open runs finish first.

Temporal cannot recreate a stateful server's session on another worker. If its
`-server-session` activity is retried after the server connected, for example
after a graceful worker shutdown, a lost host or the session activity's
start-to-close timeout, the retry fails with a non-retryable
`ApplicationError(type="DedicatedWorkerFailure")` instead of connecting a new
server with an empty session. Leaving the `async with` block then raises
`ApplicationError(type="DedicatedWorkerFailure")`, whose cause is the session
activity's `ActivityError` (none if the block's last timer fired in the
workflow task that brought the session's end, ahead of it, since the SDK then
drops it). To recover, catch it around the block and open a
new block, which connects a new, empty server (but see the note on same-name
blocks below). A retry after a failed factory call, a failed first `connect()`,
or a worker shutdown before the server connected connects again. A retry after a lost host or a
timed-out attempt fails even when the server never connected: the worker sends
heartbeats later, and the plugin cannot wait for one, so every heartbeat says a
session may be open until the attempt ends without one. Before it connects, the
session activity reads its workflow's description (`DescribeWorkflowExecution`)
until the Temporal server shows its first heartbeat, so the worker's client
needs permission to describe the workflow; without it, the session activity
fails with a non-retryable `DedicatedWorkerFailure` that says so. Other describe
errors are retried, and the session activity's last failure carries them.

Some consequences:

- The session activity's `start_to_close_timeout` in `server_session_config`,
  one hour by default, is the longest a block can stay open, since the attempt
  then times out and its retry is refused. Raise it for long blocks, such as
  ones that wait for a person, and set a `heartbeat_timeout` to notice a lost
  worker sooner. The session activity heartbeats every 30 seconds, or every
  half `heartbeat_timeout` if that is shorter.
- An activity reset or unpause with `reset_heartbeat=True` clears the session's
  heartbeat details, so the next attempt connects a new, empty server. A
  workflow reset to a point inside an open block also gives the new run a new,
  empty server, with no error.
- Sessions started on an earlier version left no such heartbeat details, so the
  deploy of this version still gives each open session a new, empty server one
  last time.
- Code that caught `ActivityError` around a block, which earlier versions
  raised when the session failed, must also catch
  `ApplicationError(type="DedicatedWorkerFailure")`, which the block now raises
  after a refusal. A session that fails for another reason, such as one with a
  one-attempt `server_session_config` retry policy, or one whose attempts all
  fail with an error before connecting (a factory call or first `connect()`
  that raises), still makes the block raise `ActivityError`, unless the block's
  last timer fired in the workflow task that brought the session's end, ahead
  of it: then the block raises `DedicatedWorkerFailure` with no cause, where
  earlier versions raised a bare `asyncio.CancelledError`.
- The session activity's heartbeats go through your payload codec, so its
  `heartbeat_timeout` must comfortably exceed a heartbeat's encoding time. An
  attempt that connected no server waits at most 10 seconds for its heartbeats
  to be encoded, then cancels the ones still being encoded and clears its
  heartbeat details. Encoding time also delays each block's first operation,
  so keep it well under the 30 seconds the session activity waits for its first
  heartbeat, and under the operations' `schedule_to_start_timeout`.
- Blocks with the same name in one run use the same task queue. A later block
  refuses any operation the server scheduled for an earlier block, but until
  the earlier block's session activity notices its cancellation, which can take
  one heartbeat interval, it serves the later block's operations with the
  earlier block's server. Don't rely on a new block being isolated from the
  previous one during that time. Likewise, after a refusal caused by a
  start-to-close timeout on a live worker, the timed-out attempt keeps serving
  the queue with its server until its worker cancels it, so the refused block's
  own operations, and a recovery block's, can still reach that server. Don't
  open two blocks with the same name at the same time: one block's operations
  can then fail with an `ActivityError` whose cause is `DedicatedWorkerFailure`,
  or both blocks use one server.

Only the session activity's worker serves the server's operations. An operation
whose `config` has a `schedule_to_start_timeout`, as the default `config` does,
or a `schedule_to_close_timeout` fails when that timeout expires. One with
neither fails with `DedicatedWorkerFailure` as soon as the session activity has
ended, with the session's failure as its cause, and an operation still waiting
then is cancelled. With `cancellation_type=WAIT_CANCELLATION_COMPLETED` that
operation is not cancelled, because no worker may be left to acknowledge it. It
stays on the session's task queue until the run ends, and a later
`stateful_mcp_server()` block with the same name refuses it. The caller of such
an operation waits for the operation or the session, whichever ends first, so
the operation resolves one step after other activities that resolve in the same
workflow task, and if another branch cancels the caller in the workflow task
that brings the operation's result, the caller gets the cancellation. A
caller's own cancellation still cancels the operation, and a cancellation that
leaves the block wins over the block's `DedicatedWorkerFailure`, though not over
an `ActivityError` from a session that failed for another reason.

Runs recorded on 1.x schedule such operations without watching the session, and
some of them fail replay on this version: a run in which an operation with
`cancellation_type=WAIT_CANCELLATION_COMPLETED` finished in the same workflow
task as another activity, and two more cases. A block whose last timer fired
ahead of its session's end now raises `DedicatedWorkerFailure` with no cause,
as above, so a run that hit this and caught the bare `CancelledError`, or that
failed with it although it catches `ApplicationError` or `Exception` around the
block, schedules different commands on this version. The same applies to a 1.x
run that raised `DedicatedWorkerFailure` in place of a cancellation when its
session ended in the same workflow task. Let such open runs finish before
upgrading, and expect replays of such closed runs (queries, replay tests) to
fail. A run whose session failed with `DedicatedWorkerFailure` (a refused retry
or a refused describe) and that then went on can fail replay on an earlier
version, whether it caught the block's failure or a cancellation, and so can
any run that caught a `DedicatedWorkerFailure` with no cause and went on, so do
not roll back while such runs are open. Drain the earlier version's workers
before starting this one: while workers of both versions run, a run whose
session gets a refusal can fail replay when it moves from one to the other.

### Hosted MCP Tool

For network-accessible MCP servers, you can also use `HostedMCPTool` from the OpenAI Agents SDK, which uses an MCP client hosted by OpenAI.

## Secrets from the Worker's Environment

⚠️ **Experimental** - This functionality is subject to change prior to General Availability.

A credential an agent needs can stay in the worker process's environment instead of being written into your workflow. Where the value would otherwise go, you name the environment variable that holds it, and the worker reads that variable when the value is actually needed.

There are two forms, and which one you use follows from where the value goes:

- For a hosted tool credential, use `temporal_worker_env_ref()`. It is substituted only in the fields listed under [Hosted Tool Credentials](#hosted-tool-credentials).
- For a sandbox environment variable, use `TemporalWorkerEnvValue`.

Both are gated by `resolvable_worker_env_vars`, an allowlist of the variable names a worker is willing to read. On every worker that runs model or sandbox activities, set the variable and add its name to that list:

```python
plugin = OpenAIAgentsPlugin(resolvable_worker_env_vars=["MY_MCP_TOKEN"])
```

Names are matched exactly, with no globbing. Passing `AllowAllWorkerEnvVars()` in place of the list makes every environment variable on the worker resolvable, so a workflow-authored sandbox manifest can name any variable on the worker and have its value land inside the container.

```python
from temporalio.openai_agents import AllowAllWorkerEnvVars

plugin = OpenAIAgentsPlugin(resolvable_worker_env_vars=AllowAllWorkerEnvVars())
```

The reference form never raises. A name the worker does not allow is sent on as the reference string, and a name it allows resolves to whatever the variable holds — an empty string when that variable is unset or empty.

### Hosted Tool Credentials

Pass `temporal_worker_env_ref()` the *name of an environment variable*, in place of the credential itself:

```python
from agents import HostedMCPTool
from temporalio.openai_agents import temporal_worker_env_ref

tool = HostedMCPTool(
    tool_config={
        "type": "mcp",
        "server_label": "my_server",
        "server_url": "https://example.com/mcp",
        "authorization": temporal_worker_env_ref("MY_MCP_TOKEN"),
    }
)
```

A reference can sit inside a larger value: in `"Bearer " + temporal_worker_env_ref("MY_MCP_TOKEN")`, the reference is replaced in place and the rest of the string is sent unchanged.

The environment variable's value is substituted in these fields and no others:

- `authorization`, and the value of each entry in `headers`, in a `HostedMCPTool`'s `tool_config`
- `value` in each entry of `network_policy.domain_secrets` under a hosted `ShellTool`'s `environment`
- `value` in each entry of `network_policy.domain_secrets` under the `container` in a `CodeInterpreterTool`'s `tool_config`

### Sandbox Environment Variables

Put a `TemporalWorkerEnvValue` in the environment of a [sandbox](#sandbox-support) manifest, in place of the value itself:

```python
from agents.sandbox import Manifest
from agents.sandbox.manifest import Environment

from temporalio.openai_agents import TemporalWorkerEnvValue

manifest = Manifest(
    environment=Environment(
        value={
            "OPENAI_API_KEY": TemporalWorkerEnvValue(name="PROD_OPENAI_KEY"),
            "REGION": "us-west-2",
        }
    )
)
```

Pass that manifest to `SandboxRunConfig(manifest=...)`. This reads `PROD_OPENAI_KEY` on the worker and sets `OPENAI_API_KEY` inside the sandbox, so the two names need not match.

## Sandbox Support

⚠️ **Pre-release** - This functionality is subject to change prior to General Availability.

The sandbox integration lets `SandboxAgent` from the OpenAI Agents SDK execute inside a sandbox, such as one hosted by Daytona, E2B or Modal, while keeping all coordination durable in Temporal.

Every sandbox operation — creating a session, running commands, reading/writing files, PTY interactions — is dispatched as a Temporal activity. This means sandbox work is fully observable, retryable, and recoverable like any other activity, and sandbox session state is serialized with the workflow. With a hosted backend, any worker can reconnect to the sandbox from that state, so a session survives worker restarts.

Register a hosted backend with `SandboxClientProvider`. The Docker and Unix-local backends run the sandbox on the worker's own machine, where other workers can't reach it, so register them with `OnHostSandboxClientProvider` instead, with one worker or several. `SandboxClientProvider` refuses them.

Changing a sandbox agent's configuration (environment, folder grants, mounts) while runs that use it are paused for approval isn't supported, with either provider.

### Architecture

```text
Workflow Code
  ↓
temporal_sandbox_client("daytona")   [returns TemporalSandboxClient]
  ↓
SandboxAgent.run(run_config=RunConfig(sandbox=SandboxRunConfig(client=...)))
  ↓
sandbox agent calls session.exec / session.read / session.write / …
  ↓
TemporalSandboxSession routes each call as a Temporal activity
("daytona-sandbox_session_exec", "daytona-sandbox_session_read", …)
  ↓
SandboxClientProvider or OnHostSandboxClientProvider activities on the worker call the real sandbox client
  ↓
Actual sandbox backend (Daytona, Docker, local, …)
```

### Worker Configuration

Register one or more sandbox client providers with the plugin. Each provider pairs a unique name with a real `BaseSandboxClient` implementation. The plugin automatically registers all required activities on the worker.

```python
import asyncio
from datetime import timedelta
from temporalio.client import Client
from temporalio.worker import Worker
from temporalio.openai_agents import (
    ModelActivityParameters,
    OnHostSandboxClientProvider,
    OpenAIAgentsPlugin,
    SandboxClientProvider,
)
from agents.extensions.sandbox.daytona import DaytonaSandboxClient
from agents.sandbox.sandboxes.unix_local import UnixLocalSandboxClient

async def main():
    client = await Client.connect(
        "localhost:7233",
        plugins=[
            OpenAIAgentsPlugin(
                model_params=ModelActivityParameters(
                    start_to_close_timeout=timedelta(seconds=30)
                ),
                sandbox_clients=[
                    SandboxClientProvider("daytona", DaytonaSandboxClient()),
                    OnHostSandboxClientProvider(
                        "local", UnixLocalSandboxClient(), worker_name="build-box-1"
                    ),
                ],
            ),
        ],
    )

    worker = Worker(
        client,
        task_queue="my-task-queue",
        workflows=[MyWorkflow],
    )
    await worker.run()
```

Provider names must be unique. Each name becomes the prefix for that backend's activities, allowing multiple backends to coexist on a single worker.

### Workflow Usage

In the workflow, use `temporal_sandbox_client()` to create a reference to a registered backend by name. Pass it to `SandboxRunConfig` inside `RunConfig`:

```python
from temporalio import workflow
from temporalio.openai_agents.workflow import temporal_sandbox_client
from agents import Runner
from agents.sandbox import SandboxAgent, SandboxRunConfig
from agents.run import RunConfig
from agents.extensions.sandbox.daytona import DaytonaSandboxClientOptions

@workflow.defn
class MyWorkflow:
    @workflow.run
    async def run(self, prompt: str) -> str:
        agent = SandboxAgent(
            name="Coding Assistant",
            instructions="You are a helpful coding assistant with access to a sandbox.",
        )

        result = await Runner.run(
            agent,
            prompt,
            run_config=RunConfig(
                sandbox=SandboxRunConfig(
                    client=temporal_sandbox_client("daytona"),
                    options=DaytonaSandboxClientOptions(pause_on_exit=False),
                ),
            ),
        )
        return result.final_output
```

The name passed to `temporal_sandbox_client()` must exactly match the name of the provider on the worker.

### Docker and Unix-local Sandboxes

`OnHostSandboxClientProvider` pins each sandbox to the worker process that created it. Any worker can create a sandbox, and every later operation on it goes to the task queue `<task queue>/<provider name>@<worker_name>`, where `<task queue>` is the queue of the worker that created it. The plugin polls that queue with an extra worker beside each of your workers, started and stopped with it, and the extra worker takes your worker's activity concurrency, rate limits and graceful shutdown timeout. `worker_name` defaults to the machine's hostname.

Workers in one process can share a `worker_name`, but processes on one machine that serve the same task queue each need their own, and the old process must exit before its restart starts the new one.

A run paused for approval, or continued as new, comes back to its sandbox on the worker with the same name on the same machine, even after that worker restarts: the worker rebuilds the sandbox with the backend's own `resume()`. A restart in the middle of a step still loses the sandbox, and the step's next operation fails with the non-retryable `ApplicationError` type `SandboxLost`. The plugin doesn't start a fresh sandbox in its place or clean up the containers and workspaces left behind. If a worker never comes back, its runs' operations wait on its queue; set `schedule_to_start_timeout` in the config you pass to `temporal_sandbox_client()` to bound that wait.

Before any worker switches a provider to `OnHostSandboxClientProvider`, every worker running these workflows must run a plugin release that has it, so that each workflow task sends operations to the sandbox's queue. Switching a provider loses its open runs' sandboxes.

### Multiple Backends

A single workflow can target different backends by name. Register all backends on the worker and reference each by name in the workflow:

```python
# Run a task on the "daytona" backend
result = await Runner.run(
    agent, prompt,
    run_config=RunConfig(sandbox=SandboxRunConfig(
        client=temporal_sandbox_client("daytona"),
        options=DaytonaSandboxClientOptions(pause_on_exit=False),
    )),
)

# Run a different task on the "local" backend
result = await Runner.run(
    agent, prompt,
    run_config=RunConfig(sandbox=SandboxRunConfig(
        client=temporal_sandbox_client("local"),
        options=UnixLocalSandboxClientOptions(),
    )),
)
```

## Streaming

⚠️ **Experimental** - This functionality is subject to change prior to General Availability.

The integration supports streaming model responses via the SDK-native
`Runner.run_streamed` API. Inside a workflow, model calls execute as a
streaming activity (`invoke_model_activity_streaming`) that consumes
`Model.stream_response` and returns the collected list of native OpenAI
response events. The workflow surfaces those events to the caller
through `RunResultStreaming.stream_events()`, which wraps them in the
agents-SDK `StreamEvent` union (so raw model events arrive as
`RawResponsesStreamEvent.data`).

External consumers (UIs, tracing pipelines, etc.) observe events as
they arrive by hosting a [`WorkflowStream`](https://github.com/temporalio/sdk-python/blob/1.36.0/temporalio/contrib/workflow_streams/README.md)
in the workflow and subscribing with `WorkflowStreamClient`. The
streaming activity publishes each event to the topic configured on
`ModelActivityParameters.streaming_topic`. The topic is required
when using `Runner.run_streamed`; calling it without a configured topic
raises before any activity is scheduled.

Example workflow consuming events via `stream_events()` while the
streaming activity publishes to the `"events"` topic:

```python
from agents import Agent, Runner
from agents.stream_events import RawResponsesStreamEvent

from temporalio import workflow

@workflow.defn
class MyAgent:
    @workflow.run
    async def run(self, prompt: str) -> str:
        agent = Agent(name="Assistant", instructions="...")
        result = Runner.run_streamed(agent, prompt)
        async for event in result.stream_events():
            if isinstance(event, RawResponsesStreamEvent):
                raw_event = event.data  # native OpenAI ResponseStreamEvent
                ...
        return result.final_output
```

To publish raw model events to external subscribers, host a
`WorkflowStream` in the workflow and configure
`OpenAIAgentsPlugin(model_params=ModelActivityParameters(streaming_topic="events"))`. See [`temporalio.contrib.workflow_streams`](https://github.com/temporalio/sdk-python/blob/1.36.0/temporalio/contrib/workflow_streams/README.md) for the
publisher and subscriber API.

`RunResultStreaming.stream_events()` yields the agents-SDK
`StreamEvent` union (`RawResponsesStreamEvent`, `RunItemStreamEvent`,
`AgentUpdatedStreamEvent`); native OpenAI response events arrive
wrapped as `RawResponsesStreamEvent.data`. Workflow-stream subscribers,
by contrast, receive the unwrapped native events directly because the
streaming activity publishes them straight from `Model.stream_response`.

Streaming is incompatible with `use_local_activity` because local
activities support neither activity heartbeats nor the workflow stream
signal channel.

Activity retries surface to workflow-stream subscribers but not to
`RunResultStreaming.stream_events()`. Events are published to the
stream as `Model.stream_response` produces them, so a partial attempt
that fails mid-response leaves its emitted events on the stream and the
retry attempt publishes a second sequence. `stream_events()` only sees
the final successful attempt's collected events because it consumes the
activity's return value. Workflow-stream subscribers should treat
retries the same way as any other workflow_streams publisher — see
[Delivery semantics](https://github.com/temporalio/sdk-python/blob/1.36.0/temporalio/contrib/workflow_streams/README.md) for the trade and
the conventional `RETRY` event pattern for surfacing the transition to
consumers.

Streamed results are decoded with the full `TResponseStreamEvent` type,
so an event that fails the openai SDK's strict validation keeps its
class in every Worker process. Earlier versions decoded such an event
as another class in a process that had not parsed one itself. A
streamed run recorded on an earlier version that decoded a result that
way can take another path on replay, whether or not it raised: it may
have caught `ModelBehaviorError`, dropped an output item, or checked an
event's class. Let such runs finish on the earlier version, or use
Worker Versioning. Their closed Histories, also of runs that failed
with `ModelBehaviorError`, no longer replay. Runs that were stuck on
that decode continue after the upgrade.

Input guardrails run in parallel with the model call by default. When one
trips while the model is streaming, the plugin cancels the streaming
activity, and the run raises `InputGuardrailTripwireTriggered` as
`stream_events()` does. When the guardrail has already tripped, the
plugin does not start the model call.

The cancel reaches the activity through its heartbeat, so it needs a
`heartbeat_timeout`, which is 10 seconds by default. The activity then
stops and publishes an `error` event with the code `cancelled` to the
topic. Subscribers can still get the events the model sent before that,
as a native `stream_events()` consumer can. If the model has already
finished but the activity is still publishing its events when the cancel
arrives, the `error` event comes after `response.completed`.

Subscribers still get the whole answer and `response.completed`, with no
`error` event, when:

- `heartbeat_timeout` is `None`: the activity is not told about the
  cancel;
- `cancellation_type` is `ABANDON`: no cancel is sent;
- the activity has already returned.

If the guardrail trips while the activity waits to retry a failed
attempt, Temporal cancels it without another attempt. The topic then
ends with the failed attempt's events and no `error` event.

The `error` event ends one model call, not the run. The activity
publishes it for any cancel the workflow requests while the call runs: a
guardrail trip, `RunResultStreaming.cancel()`, or a
`ModelSettings.timeout`. So more events can follow it, from a
`ModelSettings.retry` or from the run's next model call. Its `param` is
the ID from that call's `response.created` event, so subscribers can
tell which response it ends. `param` is empty if the cancel came before
`response.created`, and Chat Completions and LiteLLM models send the same
placeholder ID, `__fake_id__`, for every call. Cancels that Temporal
makes on its own, such as a Worker shutdown or a heartbeat timeout,
publish no `error` event, because the activity's retry can still answer.

With the default `cancellation_type` (`TRY_CANCEL`), the run raises
without waiting for the activity to stop. A workflow that completes
right after the trip can close before the activity publishes the event,
and the stream then ends without it. If the workflow streams more on
the same topic, the event can land among those events. Set
`cancellation_type=ActivityCancellationType.WAIT_CANCELLATION_COMPLETED`
to make the run raise only after the activity has published it, which
keeps the event ahead of the next model call's events.

## Feature Support

This integration is presently subject to certain limitations.
Realtime agents are not supported. Streaming is supported via
`Runner.run_streamed` — see [Streaming](#streaming) above.
Certain tools are not suitable for a distributed computing environment, so these have been disabled as well.

### Model Providers

| Model Provider                                              | Supported |
| :---------------------------------------------------------- | :-------: |
| OpenAI                                                      |    Yes    |
| LiteLLM                                                     |    Yes    |
| Any OpenAI-compatible API, through a custom `ModelProvider` |    Yes    |

`RunConfig.model` overrides every agent's own model, as in the Python Agents SDK. The
TypeScript plugin follows the JS SDK, where an agent's own model wins.

Pass a custom provider with `OpenAIAgentsPlugin(model_provider=...)`. When an agent names no
model, the provider's `get_model(None)` chooses it. The Agents SDK gives such an agent the
default settings for the SDK's own default model, which include a reasoning effort and
`verbosity`; the model activity replaces them with the SDK's defaults for the model the
provider resolved, as the SDK itself does for a named model, so an endpoint that rejects
unknown fields never sees them. The workflow decides whether the settings were those
implicit defaults, so an activity worker with a different `OPENAI_DEFAULT_MODEL` gets the
same result, and `RunConfig.model_settings` is re-applied after the replacement. Settings
the agent sets explicitly are sent unchanged.

Model calls run in an Activity on the Worker, so they use the provider set on the Worker with
`OpenAIAgentsPlugin(model_provider=...)`. To use several providers, set a `MultiProvider` with a
prefix map there and choose a provider by model name. A Workflow cannot set its own provider:
`Runner.run` raises `ValueError` when `RunConfig.model_provider` is anything other than the default.
A run recorded on 1.x that passed its own provider fails replay on this version.

Without `model_provider`, the plugin uses an `OpenAIProvider` on the client set with the framework's
`set_default_openai_client`, as it is: give that client `max_retries=0` to leave retries to Temporal.
Without one, it uses the key set with `set_default_openai_key`, or else `OPENAI_API_KEY`, with the
client's own retries turned off. Set them before you create the Worker.

### Model Response format

| Model Response | Supported            |
| :------------- | :------------------: |
| Get Response   |         Yes          |
| Streaming      | Yes (experimental)   |

### Tools

#### Tool Type

`LocalShellTool` and `ComputerTool` are not suited to a distributed computing setting.

| Tool Type           | Supported |
| :------------------ | :-------: |
| FunctionTool        |    Yes    |
| LocalShellTool      |    No     |
| WebSearchTool       |    Yes    |
| FileSearchTool      |    Yes    |
| HostedMCPTool       |    Yes    |
| ImageGenerationTool |    Yes    |
| CodeInterpreterTool |    Yes    |
| ShellTool           |    Yes    |
| ComputerTool        |    No     |

#### Tool Context

As described in [Tool Calling](#tool-calling), context propagation is read-only when Temporal activities are used as tools.

| Context Propagation                    | Supported |
| :------------------------------------- | :-------: |
| Activity Tool receives copy of context |    Yes    |
| Activity Tool can update context       |    No     |
| Function Tool received context         |    Yes    |
| Function Tool can update context       |    Yes    |

### MCP

The integration supports MCP Python SDK v2 clients. Modern MCP protocol
connections are sessionless; transport connections may still be reused as an
optimization. The OpenAI MCP server factory can use stdio, streamable HTTP, an
in-process server, or a custom v2 transport.

Note that when using network-accessible MCP servers, you also can also use the tool `HostedMCPTool`, which is part of the OpenAI Responses API and uses an MCP client hosted by OpenAI.

| MCP v2 client transport | Supported |
| :---------------------- | :-------: |
| Stdio                   |    Yes    |
| Streamable HTTP         |    Yes    |
| In-process server       |    Yes    |
| Custom transport        |    Yes    |

### Guardrails

| Guardrail Type | Supported |
| :------------- | :-------: |
| Code           |    Yes    |
| Agent          |    Yes    |

### Sessions

Use `WorkflowSession` for conversation history across multiple agent runs in a
single workflow execution:

```python
from agents import Agent, Runner
from temporalio import workflow
from temporalio.openai_agents import WorkflowSession

@workflow.defn
class Conversation:
    @workflow.run
    async def run(self) -> str:
        agent = Agent(name="Assistant")
        session = WorkflowSession("conversation")
        await Runner.run(agent, "My name is Ada.", session=session)
        result = await Runner.run(agent, "What is my name?", session=session)
        return result.final_output
```

Keep the same session instance for each turn. The items live in workflow state
and are reconstructed on replay after a worker restart. A second workflow
execution, even one that uses the same `session_id`, starts with an empty session.
SQLite storage is not suited to a distributed workflow environment.

| Feature         | Supported |
| :-------------- | :-------: |
| WorkflowSession |    Yes    |
| SQLiteSession   |    No     |

### Tracing

| Tracing Provider | Supported |
| :--------------- | :-------: |
| OpenAI platform  |    Yes    |

A trace's own tracing key, set with `RunConfig(tracing={"api_key": ...})` or
`trace(..., tracing={"api_key": ...})`, is not supported. The plugin passes traces to Workflows and
Activities in headers, which are stored in Event History, so it cannot pass the key, and their spans
would be exported with another key. In Workflow code, `Runner.run`, `Runner.run_streamed` and
`trace()` raise `ValueError` when given such a key. Starting, signaling, querying or updating a
Workflow, or scheduling an Activity or child Workflow, under such a trace raises it too. Set the
tracing key on every client and Worker process instead, with `set_tracing_export_api_key` or
`OPENAI_API_KEY`. An Activity called under a trace runs in the trace the plugin restored there: an
agent run joins it and ignores the key in its `RunConfig`, so its spans use the Worker's default
key. When the caller had no trace, the run starts its own trace and keeps that key. A trace that the
Activity starts itself, with `trace(..., tracing={"api_key": ...})`, keeps its key in both cases; it
is separate from the Workflow's trace, and Temporal calls under it raise. A run that used such a key
in Workflow code before you deploy this version gets the error when it replays: let those runs
finish first, or use Worker Versioning.

## OpenTelemetry Integration

⚠️ **Public Preview** - This functionality is subject to change prior to General Availability.

This integration provides seamless export of OpenAI agent telemetry to OpenTelemetry (OTEL) endpoints for observability and monitoring. The integration automatically handles workflow replay semantics, ensuring spans are only exported when workflows actually complete.

### Quick Start

To enable OTEL telemetry export, you need to set up a global `ReplaySafeTracerProvider` and enable the integration in the `OpenAIAgentsPlugin`:

```python
from datetime import timedelta
from temporalio.client import Client
from temporalio.openai_agents import OpenAIAgentsPlugin, ModelActivityParameters
from temporalio.contrib.opentelemetry import create_tracer_provider
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry import trace
from opentelemetry.sdk.trace.export import SimpleSpanProcessor

# Configure your OTEL exporters

# Set up the global tracer provider
tracer_provider = create_tracer_provider()
tracer_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter(endpoint="http://localhost:4317")))
trace.set_tracer_provider(tracer_provider)

client = await Client.connect(
    "localhost:7233",
    plugins=[
        OpenAIAgentsPlugin(
            use_otel_instrumentation=True,  # Enable OTEL integration
            model_params=ModelActivityParameters(
                start_to_close_timeout=timedelta(seconds=30)
            )
        ),
    ],
)
```

### Features

- **Multiple Exporters**: Send telemetry to multiple OTEL endpoints simultaneously via the global tracer provider
- **Replay-Safe**: Spans are only exported when workflows actually complete, not during replays
- **Deterministic IDs**: Consistent span IDs across workflow replays for reliable correlation
- **Automatic Setup**: No manual instrumentation required - just enable the flag and set up the global tracer provider
- **Graceful Degradation**: Works seamlessly whether OTEL dependencies are installed or not

### Dependencies

OTEL integration requires additional dependencies:

```bash
pip install openinference-instrumentation-openai-agents opentelemetry-sdk
```

Choose the appropriate OTEL exporter for your monitoring system:

```bash
# For OTLP (works with most OTEL collectors and monitoring systems)
pip install opentelemetry-exporter-otlp

# Other exporters available for specific systems
pip install opentelemetry-exporter-<your-system>
```

`ConsoleSpanExporter` (development/debugging) ships with `opentelemetry-sdk`, so it needs no extra package.

### Example: Multiple Exporters

```python
from temporalio.contrib.opentelemetry import create_tracer_provider
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter
from opentelemetry import trace

exporters = [
    # Production monitoring system
    OTLPSpanExporter(
        endpoint="https://your-monitoring-system:4317",
        headers={"api-key": "your-api-key"}
    ),
    
    # Secondary monitoring endpoint
    OTLPSpanExporter(endpoint="https://backup-collector:4317"),
    
    # Development debugging
    ConsoleSpanExporter(),
]

# Set up the global tracer provider with one span processor per exporter
tracer_provider = create_tracer_provider()
for exporter in exporters:
    tracer_provider.add_span_processor(BatchSpanProcessor(exporter))
trace.set_tracer_provider(tracer_provider)

plugin = OpenAIAgentsPlugin(use_otel_instrumentation=True)
```

### Error Handling

If you enable OTEL instrumentation but the required dependencies are not installed, you'll receive a clear error message:

```
ImportError: OTEL dependencies not available. Install with: pip install openinference-instrumentation-openai-agents opentelemetry-sdk
```

If you enable OTEL instrumentation but don't have a proper global tracer provider set up, you'll get:

```
ValueError: Global tracer provider must a ReplaySafeTracerProvider. Use temporalio.contrib.opentelemtry.create_trace_provider to create one.
```

### Direct OpenTelemetry API Calls in Workflows

When using direct OpenTelemetry API calls within workflows (e.g., `opentelemetry.trace.get_tracer(__name__).start_as_current_span()`), you need to ensure proper context bridging and sandbox configuration.

#### Sandbox Configuration

Workflows run in a sandbox that restricts module access. To use direct OTEL API calls, you must explicitly allow OpenTelemetry passthrough:

```python
from temporalio.worker import Worker
from temporalio.worker.workflow_sandbox import SandboxedWorkflowRunner, SandboxRestrictions

# Configure worker with OpenTelemetry passthrough
worker = Worker(
    client,
    task_queue="my-task-queue",
    workflows=[MyWorkflow],
    workflow_runner=SandboxedWorkflowRunner(
        SandboxRestrictions.default.with_passthrough_modules("opentelemetry")
    )
)
```

#### Context Bridging Pattern

Direct OTEL spans must be created within an active OpenAI Agents SDK span to ensure proper parenting:

```python
import opentelemetry.trace
from agents import custom_span
from temporalio import workflow

@workflow.defn
class MyWorkflow:
    @workflow.run
    async def run(self) -> str:
        # Start an SDK span first to establish OTEL context bridge
        with custom_span("Workflow coordination"):
            # Now direct OTEL spans will be properly parented
            tracer = opentelemetry.trace.get_tracer(__name__)
            with tracer.start_as_current_span("Custom workflow span"):
                # Your workflow logic here
                result = await self.do_work()
                return result
```

#### Why This Pattern is Required

- **OpenInference instrumentation** bridges OpenAI Agents SDK spans to OpenTelemetry context
- **Direct OTEL API calls** without an active SDK span become root spans with no parent
- **SDK spans** (`custom_span()`) establish the context bridge that allows subsequent direct OTEL spans to inherit proper trace parenting

#### Complete Example

```python
import opentelemetry.trace
from agents import custom_span
from temporalio import workflow
from temporalio.worker import Worker
from temporalio.worker.workflow_sandbox import SandboxedWorkflowRunner, SandboxRestrictions

@workflow.defn
class TracedWorkflow:
    @workflow.run
    async def run(self) -> str:
        # Establish OTEL context with SDK span
        with custom_span("Main workflow"):
            # Create direct OTEL spans for fine-grained tracing
            tracer = opentelemetry.trace.get_tracer(__name__)
            
            with tracer.start_as_current_span("Data processing"):
                data = await self.process_data()
                
            with tracer.start_as_current_span("Business logic"):
                result = await self.execute_business_logic(data)
                
            return result

# Worker configuration
worker = Worker(
    client,
    task_queue="traced-workflows",
    workflows=[TracedWorkflow],
    workflow_runner=SandboxedWorkflowRunner(
        SandboxRestrictions.default.with_passthrough_modules("opentelemetry")
    )
)
```

This ensures your direct OTEL spans are properly parented within the trace hierarchy initiated by your client SDK traces.

### Client-Side Trace Initialization

You can also start an Agents SDK trace on the client side before executing a workflow. This is useful when you want the entire workflow execution to be part of a larger trace context:

```python
from agents import trace, custom_span
from temporalio.openai_agents import OpenAIAgentsPlugin

# Set up the plugin with OTEL integration
plugin = OpenAIAgentsPlugin(use_otel_instrumentation=True)

# Client setup
client = await Client.connect(
    "localhost:7233",
    plugins=[plugin]
)

# Start a trace on the client side
with plugin.tracing_context():
    with trace("Customer support workflow"):
        with custom_span("Workflow execution"):
            # Execute workflow within the trace context
            result = await client.execute_workflow(
                CustomerSupportAgent.run,
                "Help me with my order",
                id="customer-support-123",
                task_queue="my-task-queue",
            )
            print(f"Result: {result}")
```

The `plugin.tracing_context()` is required when starting traces outside of a worker context. This ensures proper instrumentation setup and trace propagation into the workflow execution.

Headers containing only OpenTelemetry IDs preserve the remote OTel parent without
reconstructing an Agents SDK trace. Start an Agents SDK `trace()` in the worker
before creating Agents SDK spans when the caller supplies no Agents trace ID.

If OTEL instrumentation is not enabled, the integration works normally without any OTEL setup.

### Voice

| Mode                     | Supported     |
| :----------------------- | :-----------: |
| Voice agents (pipelines) | Yes [^voice]  |
| Realtime agents          |      No       |

[^voice]: `VoicePipeline` runs in your process and delegates the agent
    step (`VoiceWorkflowBase.run`) to a Temporal workflow that uses
    `Runner.run` or `Runner.run_streamed`. STT and TTS run outside
    Temporal; the agent loop is durable.

### Utilities

The REPL utility is not suitable for a distributed setting.

| Utility | Supported |
| :------ | :-------: |
| REPL    |    No     |

## Additional Examples

You can find additional examples in the [Temporal Python Samples Repository](https://github.com/temporalio/samples-python/tree/main/openai_agents).
