Skip to content

Commit c5eb6e2

Browse files
authored
Replace WorkflowIdReusePolicy.TerminateExisting by WorkflowIdConflictPolicy (#1243)
1 parent 7cfc65b commit c5eb6e2

File tree

1 file changed

+51
-60
lines changed

1 file changed

+51
-60
lines changed

temporalio/contrib/openai_agents/README.md

Lines changed: 51 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
⚠️ **Public Preview** - The interface to this module is subject to change prior to General Availability.
44
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/).
55

6-
76
## Introduction
87

98
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).
@@ -14,14 +13,14 @@ Temporal provides a crash-proof system foundation, taking care of the distribute
1413
OpenAI Agents SDK offers a lightweight yet powerful framework for defining those agents.
1514

1615
This document is organized as follows:
17-
- **[Hello World Durable Agent](#hello-world-durable-agent).** Your first durable agent example.
18-
- **[Background Concepts](#core-concepts).** Background on durable execution and AI agents.
19-
- **[Full Example](#full-example)** Running the Hello World Durable Agent example.
20-
- **[Tool Calling](#tool-calling).** Calling agent Tools in Temporal.
21-
- **[Feature Support](#feature-support).** Compatibility matrix.
2216

23-
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.
17+
- **[Hello World Durable Agent](#hello-world-durable-agent).** Your first durable agent example.
18+
- **[Background Concepts](#core-concepts).** Background on durable execution and AI agents.
19+
- **[Full Example](#full-example)** Running the Hello World Durable Agent example.
20+
- **[Tool Calling](#tool-calling).** Calling agent Tools in Temporal.
21+
- **[Feature Support](#feature-support).** Compatibility matrix.
2422

23+
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.
2524

2625
## Hello World Durable Agent
2726

@@ -54,7 +53,6 @@ The `@workflow.defn` annotation on the `HelloWorldAgent` indicates that this cla
5453
We use the `Agent` class from OpenAI Agents SDK to define a simple agent, instructing it to always respond with haikus.
5554
We then run that agent, using the `Runner` class from OpenAI Agents SDK, passing through `prompt` as an argument.
5655

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

@@ -70,13 +68,13 @@ In the OpenAI Agents SDK, an agent is an AI model configured with instructions,
7068

7169
We describe each of these briefly:
7270

73-
- *AI model*. An LLM such as OpenAI's GPT, Google's Gemini, or one of many others.
74-
- *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.
75-
- *Tools*. Typically, Python functions that the model may choose to invoke. Tools are functions with text-descriptions that explain their functionality to the model.
76-
- *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.
77-
- *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.
78-
- *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.
79-
- *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.
71+
- _AI model_. An LLM such as OpenAI's GPT, Google's Gemini, or one of many others.
72+
- _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.
73+
- _Tools_. Typically, Python functions that the model may choose to invoke. Tools are functions with text-descriptions that explain their functionality to the model.
74+
- _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.
75+
- _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.
76+
- _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.
77+
- _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.
8078

8179
Now, let's see how these components work together.
8280
In a common pattern, the model first receives user input and then reasons about which tool to invoke.
@@ -126,23 +124,22 @@ As the program makes progress, Temporal saves key inputs and decisions, allowing
126124

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

129-
1. Deterministic pieces, termed *workflows*, execute the same way when re-run with the same inputs.
130-
2. Non-deterministic pieces, termed *activities*, can run arbitrary code, performing I/O and any other operations.
127+
1. Deterministic pieces, termed _workflows_, execute the same way when re-run with the same inputs.
128+
2. Non-deterministic pieces, termed _activities_, can run arbitrary code, performing I/O and any other operations.
131129

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

135133
In the AI-agent example above, model invocations and tool calls run inside activities, while the logic that coordinates them lives in the workflow.
136134
This pattern generalizes to more sophisticated agents.
137-
We refer to that coordinating logic as *agent orchestration*.
135+
We refer to that coordinating logic as _agent orchestration_.
138136

139137
As a general rule, agent orchestration code executes within the Temporal workflow, whereas model calls and any I/O-bound tool invocations execute as Temporal activities.
140138

141139
The diagram below shows the overall architecture of an agentic application in Temporal.
142140
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).
143141
Temporal Server manages data in encrypted form, so all data processing occurs on the Worker, which runs the workflow and activities.
144142

145-
146143
```text
147144
+---------------------+
148145
| Temporal Server | (Stores workflow state,
@@ -172,17 +169,14 @@ Temporal Server manages data in encrypted form, so all data processing occurs on
172169
[External APIs, services, databases, etc.]
173170
```
174171

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

178-
179174
## Complete Example
180175

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

185-
186180
### File 2: Launch Worker (`run_worker.py`)
187181

188182
```python
@@ -225,12 +219,12 @@ if __name__ == "__main__":
225219

226220
We use the `OpenAIAgentsPlugin` to configure Temporal for use with OpenAI Agents SDK.
227221
The plugin automatically handles several important setup tasks:
222+
228223
- Ensures proper serialization of Pydantic types
229224
- Propagates context for [OpenAI Agents tracing](https://openai.github.io/openai-agents-python/tracing/).
230225
- Registers an activity for invoking model calls with the Temporal worker.
231226
- Configures OpenAI Agents SDK to run model calls as Temporal activities.
232227

233-
234228
### File 3: Client Execution (`run_hello_world_workflow.py`)
235229

236230
```python
@@ -257,7 +251,8 @@ async def main():
257251
"Tell me about recursion in programming.",
258252
id="my-workflow-id",
259253
task_queue="my-task-queue",
260-
id_reuse_policy=WorkflowIDReusePolicy.TERMINATE_IF_RUNNING,
254+
id_reuse_policy=WorkflowIDReusePolicy.ALLOW_DUPLICATE,
255+
id_conflict_policy=WorkflowIDConflictPolicy.TERMINATE_EXISTING,
261256
)
262257
print(f"Result: {result}")
263258

@@ -268,7 +263,6 @@ if __name__ == "__main__":
268263
This file is a standard Temporal launch script.
269264
We also configure the client with the `OpenAIAgentsPlugin` to ensure serialization is compatible with the worker.
270265

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

274268
## Tool Calling
@@ -308,7 +302,7 @@ class WeatherAgent:
308302
instructions="You are a helpful weather agent.",
309303
tools=[
310304
openai_agents.workflow.activity_as_tool(
311-
get_weather,
305+
get_weather,
312306
start_to_close_timeout=timedelta(seconds=10)
313307
)
314308
],
@@ -350,7 +344,6 @@ Of course, code running in the workflow can invoke a Temporal activity at any ti
350344

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

353-
354347
## MCP Support
355348

356349
This integration provides support for Model Context Protocol (MCP) servers through two wrapper approaches designed to handle different implications of failures.
@@ -434,13 +427,13 @@ class FileSystemWorkflow:
434427
async def run(self, query: str) -> str:
435428
# Reference the MCP server by name (matches name in worker configuration)
436429
server = openai_agents.workflow.stateless_mcp_server("FileSystemServer")
437-
430+
438431
agent = Agent(
439432
name="File Assistant",
440433
instructions="Use the filesystem tools to read files and answer questions.",
441434
mcp_servers=[server],
442435
)
443-
436+
444437
result = await Runner.run(agent, input=query)
445438
return result.final_output
446439
```
@@ -467,18 +460,18 @@ Certain tools are not suitable for a distributed computing environment, so these
467460
### Model Providers
468461

469462
| Model Provider | Supported |
470-
|:--------------|:---------:|
471-
| OpenAI | Yes |
472-
| LiteLLM | Yes |
463+
| :------------- | :-------: |
464+
| OpenAI | Yes |
465+
| LiteLLM | Yes |
473466

474467
### Model Response format
475468

476469
This integration does not presently support streaming.
477470

478471
| Model Response | Supported |
479-
|:--------------|:---------:|
480-
| Get Response | Yes |
481-
| Streaming | No |
472+
| :------------- | :-------: |
473+
| Get Response | Yes |
474+
| Streaming | No |
482475

483476
### Tools
484477

@@ -487,7 +480,7 @@ This integration does not presently support streaming.
487480
`LocalShellTool` and `ComputerTool` are not suited to a distributed computing setting.
488481

489482
| Tool Type | Supported |
490-
|:-------------------|:---------:|
483+
| :------------------ | :-------: |
491484
| FunctionTool | Yes |
492485
| LocalShellTool | No |
493486
| WebSearchTool | Yes |
@@ -501,12 +494,12 @@ This integration does not presently support streaming.
501494

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

504-
| Context Propagation | Supported |
505-
|:----------------------------------------|:---------:|
506-
| Activity Tool receives copy of context | Yes |
507-
| Activity Tool can update context | No |
508-
| Function Tool received context | Yes |
509-
| Function Tool can update context | Yes |
497+
| Context Propagation | Supported |
498+
| :------------------------------------- | :-------: |
499+
| Activity Tool receives copy of context | Yes |
500+
| Activity Tool can update context | No |
501+
| Function Tool received context | Yes |
502+
| Function Tool can update context | Yes |
510503

511504
### MCP
512505

@@ -516,50 +509,48 @@ These wrappers work with all transport varieties.
516509

517510
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.
518511

519-
| MCP Class | Supported |
520-
|:-----------------------|:---------:|
521-
| MCPServerStdio | Yes |
522-
| MCPServerSse | Yes |
523-
| MCPServerStreamableHttp| Yes |
512+
| MCP Class | Supported |
513+
| :---------------------- | :-------: |
514+
| MCPServerStdio | Yes |
515+
| MCPServerSse | Yes |
516+
| MCPServerStreamableHttp | Yes |
524517

525518
### Guardrails
526519

527520
| Guardrail Type | Supported |
528-
|:---------------|:---------:|
521+
| :------------- | :-------: |
529522
| Code | Yes |
530523
| Agent | Yes |
531524

532525
### Sessions
533526

534527
SQLite storage is not suited to a distributed environment.
535528

536-
| Feature | Supported |
537-
|:---------------|:---------:|
538-
| SQLiteSession | No |
529+
| Feature | Supported |
530+
| :------------ | :-------: |
531+
| SQLiteSession | No |
539532

540533
### Tracing
541534

542535
| Tracing Provider | Supported |
543-
|:-----------------|:---------:|
536+
| :--------------- | :-------: |
544537
| OpenAI platform | Yes |
545538

546-
### Voice
539+
### Voice
547540

548-
| Mode | Supported |
549-
|:------------------------|:---------:|
550-
| Voice agents (pipelines)| No |
551-
| Realtime agents | No |
541+
| Mode | Supported |
542+
| :----------------------- | :-------: |
543+
| Voice agents (pipelines) | No |
544+
| Realtime agents | No |
552545

553546
### Utilities
554547

555548
The REPL utility is not suitable for a distributed setting.
556549

557550
| Utility | Supported |
558-
|:--------|:---------:|
551+
| :------ | :-------: |
559552
| REPL | No |
560553

561-
562554
## Additional Examples
563555

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

0 commit comments

Comments
 (0)