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
+37-29Lines changed: 37 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,30 +1,32 @@
1
1
# OpenAI Agents SDK Integration for Temporal
2
2
3
-
⚠️ **Public Preview** - The interface to this module is subject to change prior to General Availability. We welcome your questions and feedback in the [#python-sdk](https://temporalio.slack.com/archives/CTT84RS0P) Slack channel at [temporalio.slack.com](https://temporalio.slack.com/).
3
+
⚠️ **Public Preview** - The interface to this module is subject to change prior to General Availability.
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/).
4
5
5
6
6
7
## Introduction
7
8
8
9
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).
9
-
It allows you to build AI agents that never lose their progress and handle long-running, asynchronous, and human-in-the-loop workflows with ease.
10
+
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.
10
11
12
+
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.
11
13
Temporal provides a crash-proof system foundation, taking care of the distributed systems challenges inherent to production agentic systems.
12
14
OpenAI Agents SDK offers a lightweight yet powerful framework for defining those agents.
13
-
The combination lets you build reliable agentic systems quickly.
14
15
15
16
This document is organized as follows:
16
17
-**[Hello World Agent](#hello-world-durable-agent).** Your first durable agent example.
17
18
-**[Background Concepts](#core-concepts).** Background on durable execution and AI agents.
The [samples repository](https://github.com/temporalio/samples-python/tree/main/openai_agents) contains a number of examples spanning various use cases.
24
+
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.
23
25
24
26
25
27
## Hello World Durable Agent
26
28
27
-
The code below shows how straightforward it is to wrap an agent wrapped in durable execution.
29
+
The code below shows how to wrap an agent for durable execution.
28
30
29
31
### File 1: Durable Agent (`hello_world.py`)
30
32
@@ -45,20 +47,23 @@ class HelloWorldAgent:
45
47
return result.final_output
46
48
```
47
49
50
+
In this example, Temporal provides the durable execution wrapper: the `HelloWorldAgent.run` method.
51
+
The content of that method, is regular OpenAI Agents SDK code.
52
+
48
53
If you are familiar with Temporal and with Open AI Agents SDK, this code will look very familiar.
49
-
We annotate the `HelloWorldAgent` class with `@workflow.defn` to define a workflow, then use the `@workflow.run` annotation to define the entrypoint.
54
+
We the `@workflow.defn` annotations on the `HelloWorldAgent` indicates that this class will contain durable execution and the `@workflow.run` annotation defines the entrypoint.
55
+
We use the `Agent` class from OpenAI Agents SDK to define a simple agent, instructing it to always responds with haikus.
56
+
We then run that agent, using the `Runner` class from OpenAI Agents SDK, passing through `prompt` as an argument.
50
57
51
-
We use the `Agent` class to define a simple agent, instructing it to always responds with haikus.
52
-
Within the workflow, we start the agent using the `Runner`, as is typical, passing through `prompt` as an argument.
53
58
54
-
We will [complete this example below](#complete-example).
55
-
However, before digging further into the code, it we will share some more background to set the stage.
59
+
We will [complete this example below](#full-example).
60
+
Before digging further into the code, we will review some background that will make it easier to understand.
56
61
57
62
## Background Concepts
58
63
59
-
We encourage you to form a thorough understanding of AI agents and durable execution with Temporal.
60
-
Understanding this will make it easer to design and build durable agents.
61
-
If you are well versed in these topics, you may skim this section or skip ahead.
64
+
We encourage you to review this section thoroughly to gain a solid understanding of AI agents and durable execution with Temporal.
65
+
This knowledge will make it easier to design and build durable agents.
66
+
If you are already well versed in these topics, feel free to skim this section or skip ahead.
62
67
63
68
### AI Agents
64
69
@@ -74,10 +79,9 @@ We describe each of these briefly:
74
79
-*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.
75
80
-*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.
76
81
77
-
78
-
Now, let's look at how these pieces can fit together.
79
-
In one popular pattern, the model receives user input, then performs reasoning to select a tool to call.
80
-
The response from the tool is fed back into the model, which may perform additional tool calls, iterating until the task is complete.
82
+
Now, let's see how these components work together.
83
+
In a common pattern, the model first receives user input and then reasons about which tool to invoke.
84
+
The tool's response is passed back to the model, which may call additional tools, repeating this loop until the task is complete.
81
85
82
86
The diagram below illustrates this flow.
83
87
@@ -109,27 +113,31 @@ The diagram below illustrates this flow.
109
113
again, until task is complete)
110
114
```
111
115
112
-
Even in a simple example like this, there are many places where something can go wrong.
113
-
Tools call APIs that are sometimes down and models have rate limits, requiring retries.
116
+
Even in a simple example like this, there are many places where things can go wrong.
117
+
Tools call APIs that sometimes fail, while models can encounter rate limits, requiring retries.
114
118
The longer the agent runs, the more costly it is to start the job over.
115
-
In the next section, we turn to durable execution, which can handle such failures seamlessly.
119
+
We next describe durable execution, which handles such failures seamlessly.
116
120
117
121
### Durable Execution
118
122
119
123
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.
120
124
121
-
Temporal relies heavily on a replay mechanism to recover from failures.
125
+
Temporal relies primarily on a replay mechanism to recover from failures.
122
126
As the program makes progress, Temporal saves key inputs and decisions, allowing a re-started program to pick up right where it left off.
123
127
124
128
The key to making this work is to separate the applications repeatable (deterministic) and non-repeatable (non-deterministic) parts:
125
129
126
-
1. Deterministic pieces, termed *workflows*, execute the same way if re-run with the same inputs.
127
-
2. Non-deterministic pieces, termed *activities*, have no limitations—they may perform I/O and any other operations.
130
+
1. Deterministic pieces, termed *workflows*, execute the same way when re-run with the same inputs.
131
+
2. Non-deterministic pieces, termed *activities*, can run arbitrary code, performing I/O and any other operations.
132
+
133
+
Workflow code can run for extended periods and, if interrupted, resume exactly where it left off.
134
+
Activity code faces no restrictions on I/O or external interactions, but if it fails part-way through it restarts from the beginning.
128
135
129
-
In the AI agent described in the previous section, model and tool calls run in activities, and the control flow linking them together runs in the workflow.
136
+
In the AI-agent example above, model invocations and tool calls run inside activities, while the logic that coordinates them lives in the workflow.
137
+
This pattern generalizes to more sophisticated agents.
138
+
We refer to that coordinating logic as *agent orchestration*.
130
139
131
-
In more complex examples, the control flow may be described as *agent orchestration*.
132
-
Agent orchestration runs within the Temporal workflow, while model calls and any tool calls involving I/O run in activities.
140
+
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.
133
141
134
142
The diagram below shows the overall architecture of an agentic application in Temporal.
135
143
The Temporal Server is responsible to tracking program execution and making sure associated state is preserved reliably.
0 commit comments