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
+21-33Lines changed: 21 additions & 33 deletions
Original file line number
Diff line number
Diff line change
@@ -56,11 +56,7 @@ The first file, `hello_world_workflow.py`, defines an OpenAI agent within a Temp
56
56
```python
57
57
# File: hello_world_workflow.py
58
58
from temporalio import workflow
59
-
60
-
# Trusted imports bypass the Temporal sandbox, which otherwise
61
-
# prevents imports which may result in non-deterministic execution.
62
-
with workflow.unsafe.imports_passed_through():
63
-
from agents import Agent, Runner
59
+
from agents import Agent, Runner
64
60
65
61
@workflow.defn
66
62
classHelloWorldAgent:
@@ -80,11 +76,6 @@ We annotate the `HelloWorldAgent` class with `@workflow.defn` to define a workfl
80
76
We use the `Agent` class to define a simple agent, one which always responds with haikus.
81
77
Within the workflow, we start the agent using the `Runner`, as is typical, passing through `prompt` as an argument.
82
78
83
-
Perhaps the most interesting thing about this code is the `workflow.unsafe.imports_passed_through()` context manager that precedes the OpenAI Agents SDK imports.
84
-
This statement tells Temporal to skip sandboxing for these trusted libraries.
85
-
This is important because Python's dynamic nature forces Temporal's Python's sandbox to re-validate imports every time a workflow runs, which comes at a performance cost.
86
-
The OpenAI Agents SDK also contains certain code that Temporal is not able to validate automatically for determinism.
87
-
88
79
The second file, `run_worker.py`, launches a Temporal worker.
89
80
This is a program that connects to the Temporal server and receives work to run, in this case `HelloWorldAgent` invocations.
90
81
@@ -95,14 +86,13 @@ import asyncio
95
86
from datetime import timedelta
96
87
97
88
from temporalio.client import Client
98
-
from temporalio.contrib.openai_agents.invoke_model_activity import ModelActivity
99
-
from temporalio.contrib.openai_agents.model_parameters import ModelActivityParameters
100
-
from temporalio.contrib.openai_agents.open_ai_data_converter import open_ai_data_converter
101
-
from temporalio.contrib.openai_agents.temporal_openai_agents import set_open_ai_agent_temporal_overrides
89
+
from temporalio.contrib.openai_agents import ModelActivity, ModelActivityParameters, set_open_ai_agent_temporal_overrides
90
+
from temporalio.contrib.pydantic import pydantic_data_converter
102
91
from temporalio.worker import Worker
103
92
104
93
from hello_world_workflow import HelloWorldAgent
105
94
95
+
106
96
asyncdefworker_main():
107
97
# Configure the OpenAI Agents SDK to use Temporal activities for LLM API calls
108
98
# and for tool calls.
@@ -114,26 +104,26 @@ async def worker_main():
114
104
# Use the OpenAI data converter to ensure proper serialization/deserialization
We wrap the entire `worker_main` function body in the `set_open_ai_agent_temporal_overrides()` context manager.
134
124
This causes a Temporal activity to be invoked whenever the OpenAI Agents SDK invokes an LLM or calls a tool.
135
-
We also pass the `open_ai_data_converter` to the Temporal Client, which ensures proper serialization of OpenAI Agents SDK data.
136
-
We create a `ModelActivity` which serves as a generic wrapper for LLM calls, and we register this wrapper's invocation point, `model_activity.invoke_model_activity`, with the worker.
125
+
We also pass the `pydantic_data_converter` to the Temporal Client, which ensures proper serialization of pydantic models in OpenAI Agents SDK data.
126
+
We create a `ModelActivity` which serves as a generic wrapper for LLM calls, and we register this wrapper's invocation point, `ModelActivity().invoke_model_activity`, with the worker.
137
127
138
128
In order to launch the agent, use the standard Temporal workflow invocation:
139
129
@@ -144,15 +134,15 @@ import asyncio
144
134
145
135
from temporalio.client import Client
146
136
from temporalio.common import WorkflowIDReusePolicy
147
-
from temporalio.contrib.openai_agents.open_ai_data_converterimportopen_ai_data_converter
137
+
from temporalio.contrib.pydanticimportpydantic_data_converter
148
138
149
139
from hello_world_workflow import HelloWorldAgent
150
140
151
141
asyncdefmain():
152
142
# Create client connected to server at the given address
153
143
client =await Client.connect(
154
144
"localhost:7233",
155
-
data_converter=open_ai_data_converter,
145
+
data_converter=pydantic_data_converter,
156
146
)
157
147
158
148
# Execute a workflow
@@ -171,7 +161,7 @@ if __name__ == "__main__":
171
161
172
162
This launcher script executes the Temporal workflow to start the agent.
173
163
174
-
Note that this basic example works without providing the `open_ai_data_converter` to the Temporal client that executes the workflow, but we include it because more complex uses will generally need it.
164
+
Note that this basic example works without providing the `pydantic_data_converter` to the Temporal client that executes the workflow, but we include it because more complex uses will generally need it.
175
165
176
166
177
167
## Using Temporal Activities as OpenAI Agents Tools
@@ -186,10 +176,8 @@ We then pass this through the `activity_as_tool` helper function to create an Op
186
176
from dataclasses import dataclass
187
177
from datetime import timedelta
188
178
from temporalio import activity, workflow
189
-
from temporalio.contrib.openai_agents.temporal_tools import activity_as_tool
0 commit comments