You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: temporalio/contrib/openai_agents/README.md
+51-60Lines changed: 51 additions & 60 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,6 @@
3
3
⚠️ **Public Preview** - The interface to this module is subject to change prior to General Availability.
4
4
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/).
5
5
6
-
7
6
## Introduction
8
7
9
8
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
14
13
OpenAI Agents SDK offers a lightweight yet powerful framework for defining those agents.
15
14
16
15
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.
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.
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.
25
24
26
25
## Hello World Durable Agent
27
26
@@ -54,7 +53,6 @@ The `@workflow.defn` annotation on the `HelloWorldAgent` indicates that this cla
54
53
We use the `Agent` class from OpenAI Agents SDK to define a simple agent, instructing it to always respond with haikus.
55
54
We then run that agent, using the `Runner` class from OpenAI Agents SDK, passing through `prompt` as an argument.
56
55
57
-
58
56
We will [complete this example below](#full-example).
59
57
Before digging further into the code, we will review some background that will make it easier to understand.
60
58
@@ -70,13 +68,13 @@ In the OpenAI Agents SDK, an agent is an AI model configured with instructions,
70
68
71
69
We describe each of these briefly:
72
70
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.
80
78
81
79
Now, let's see how these components work together.
82
80
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
126
124
127
125
The key to making this work is to separate the applications repeatable (deterministic) and non-repeatable (non-deterministic) parts:
128
126
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.
131
129
132
130
Workflow code can run for extended periods and, if interrupted, resume exactly where it left off.
133
131
Activity code faces no restrictions on I/O or external interactions, but if it fails part-way through it restarts from the beginning.
134
132
135
133
In the AI-agent example above, model invocations and tool calls run inside activities, while the logic that coordinates them lives in the workflow.
136
134
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_.
138
136
139
137
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.
140
138
141
139
The diagram below shows the overall architecture of an agentic application in Temporal.
142
140
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).
143
141
Temporal Server manages data in encrypted form, so all data processing occurs on the Worker, which runs the workflow and activities.
144
142
145
-
146
143
```text
147
144
+---------------------+
148
145
| Temporal Server | (Stores workflow state,
@@ -172,17 +169,14 @@ Temporal Server manages data in encrypted form, so all data processing occurs on
172
169
[External APIs, services, databases, etc.]
173
170
```
174
171
175
-
176
172
See the [Temporal documentation](https://docs.temporal.io/evaluate/understanding-temporal#temporal-application-the-building-blocks) for more information.
177
173
178
-
179
174
## Complete Example
180
175
181
176
To make the [Hello World durable agent](#hello-world-durable-agent) shown earlier available in Temporal, we need to create a worker program.
182
177
To see it run, we also need a client to launch it.
183
178
We show these files below.
184
179
185
-
186
180
### File 2: Launch Worker (`run_worker.py`)
187
181
188
182
```python
@@ -225,12 +219,12 @@ if __name__ == "__main__":
225
219
226
220
We use the `OpenAIAgentsPlugin` to configure Temporal for use with OpenAI Agents SDK.
227
221
The plugin automatically handles several important setup tasks:
222
+
228
223
- Ensures proper serialization of Pydantic types
229
224
- Propagates context for [OpenAI Agents tracing](https://openai.github.io/openai-agents-python/tracing/).
230
225
- Registers an activity for invoking model calls with the Temporal worker.
231
226
- Configures OpenAI Agents SDK to run model calls as Temporal activities.
We also configure the client with the `OpenAIAgentsPlugin` to ensure serialization is compatible with the worker.
270
265
271
-
272
266
To run this example, see the detailed instructions in the [Temporal Python Samples Repository](https://github.com/temporalio/samples-python/tree/main/openai_agents).
273
267
274
268
## Tool Calling
@@ -308,7 +302,7 @@ class WeatherAgent:
308
302
instructions="You are a helpful weather agent.",
309
303
tools=[
310
304
openai_agents.workflow.activity_as_tool(
311
-
get_weather,
305
+
get_weather,
312
306
start_to_close_timeout=timedelta(seconds=10)
313
307
)
314
308
],
@@ -350,7 +344,6 @@ Of course, code running in the workflow can invoke a Temporal activity at any ti
350
344
351
345
Tools that run in the workflow can also update OpenAI Agents context, which is read-only for tools run as Temporal activities.
352
346
353
-
354
347
## MCP Support
355
348
356
349
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:
434
427
asyncdefrun(self, query: str) -> str:
435
428
# Reference the MCP server by name (matches name in worker configuration)
436
429
server = openai_agents.workflow.stateless_mcp_server("FileSystemServer")
437
-
430
+
438
431
agent = Agent(
439
432
name="File Assistant",
440
433
instructions="Use the filesystem tools to read files and answer questions.",
441
434
mcp_servers=[server],
442
435
)
443
-
436
+
444
437
result =await Runner.run(agent, input=query)
445
438
return result.final_output
446
439
```
@@ -467,18 +460,18 @@ Certain tools are not suitable for a distributed computing environment, so these
467
460
### Model Providers
468
461
469
462
| Model Provider | Supported |
470
-
|:--------------|:---------:|
471
-
| OpenAI | Yes |
472
-
| LiteLLM | Yes |
463
+
|:-------------|:-------: |
464
+
| OpenAI | Yes |
465
+
| LiteLLM | Yes |
473
466
474
467
### Model Response format
475
468
476
469
This integration does not presently support streaming.
477
470
478
471
| Model Response | Supported |
479
-
|:--------------|:---------:|
480
-
| Get Response | Yes |
481
-
| Streaming | No |
472
+
|:-------------|:-------: |
473
+
| Get Response | Yes |
474
+
| Streaming | No |
482
475
483
476
### Tools
484
477
@@ -487,7 +480,7 @@ This integration does not presently support streaming.
487
480
`LocalShellTool` and `ComputerTool` are not suited to a distributed computing setting.
488
481
489
482
| Tool Type | Supported |
490
-
|:-------------------|:---------:|
483
+
|:------------------|:-------: |
491
484
| FunctionTool | Yes |
492
485
| LocalShellTool | No |
493
486
| WebSearchTool | Yes |
@@ -501,12 +494,12 @@ This integration does not presently support streaming.
501
494
502
495
As described in [Tool Calling](#tool-calling), context propagation is read-only when Temporal activities are used as tools.
@@ -516,50 +509,48 @@ These wrappers work with all transport varieties.
516
509
517
510
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.
518
511
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 |
524
517
525
518
### Guardrails
526
519
527
520
| Guardrail Type | Supported |
528
-
|:---------------|:---------:|
521
+
|:-------------|:-------: |
529
522
| Code | Yes |
530
523
| Agent | Yes |
531
524
532
525
### Sessions
533
526
534
527
SQLite storage is not suited to a distributed environment.
535
528
536
-
| Feature | Supported |
537
-
|:---------------|:---------:|
538
-
| SQLiteSession | No |
529
+
| Feature | Supported |
530
+
|:------------|:-------: |
531
+
| SQLiteSession | No |
539
532
540
533
### Tracing
541
534
542
535
| Tracing Provider | Supported |
543
-
|:-----------------|:---------:|
536
+
|:---------------|:-------: |
544
537
| OpenAI platform | Yes |
545
538
546
-
### Voice
539
+
### Voice
547
540
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 |
552
545
553
546
### Utilities
554
547
555
548
The REPL utility is not suitable for a distributed setting.
556
549
557
550
| Utility | Supported |
558
-
|:--------|:---------:|
551
+
|:------|:-------: |
559
552
| REPL | No |
560
553
561
-
562
554
## Additional Examples
563
555
564
556
You can find additional examples in the [Temporal Python Samples Repository](https://github.com/temporalio/samples-python/tree/main/openai_agents).
0 commit comments