Skip to content

Commit ea1cf9f

Browse files
committed
porting basic examples from OpenAI Agents Examples
1 parent 396fb5c commit ea1cf9f

21 files changed

+842
-5
lines changed

openai_agents/basic/README.md

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Simple examples to get started with OpenAI Agents SDK integrated with Temporal w
44

55
*Adapted from [OpenAI Agents SDK basic examples](https://github.com/openai/openai-agents-python/tree/main/examples/basic)*
66

7+
Before running these examples, be sure to review the [prerequisites and background on the integration](../README.md).
8+
79
## Running the Examples
810

911
First, start the worker (supports all basic examples):
@@ -14,12 +16,64 @@ uv run openai_agents/basic/run_worker.py
1416
Then run individual examples in separate terminals:
1517

1618
### Hello World Agent
19+
Basic agent that only responds in haikus:
1720
```bash
1821
uv run openai_agents/basic/run_hello_world_workflow.py
1922
```
2023

2124
### Tools Agent
22-
Agent with access to external tools (weather API):
25+
Agent with access to external tools (simulated weather API):
2326
```bash
2427
uv run openai_agents/basic/run_tools_workflow.py
25-
```
28+
```
29+
30+
### Agent Lifecycle with Hooks
31+
Demonstrates agent lifecycle events and handoffs between agents:
32+
```bash
33+
uv run openai_agents/basic/run_agent_lifecycle_workflow.py
34+
```
35+
36+
### Lifecycle with Usage Tracking
37+
Shows detailed usage tracking with RunHooks (requests, tokens, etc.):
38+
```bash
39+
uv run openai_agents/basic/run_lifecycle_workflow.py
40+
```
41+
42+
### Dynamic System Prompts
43+
Agent with dynamic instruction generation based on context (haiku/pirate/robot):
44+
```bash
45+
uv run openai_agents/basic/run_dynamic_system_prompt_workflow.py
46+
```
47+
48+
### Non-Strict Output Types
49+
Demonstrates different JSON schema validation approaches:
50+
```bash
51+
uv run openai_agents/basic/run_non_strict_output_workflow.py
52+
```
53+
54+
Note: `CustomOutputSchema` is not supported by the Temporal OpenAI Agents SDK integration and is omitted in this example.
55+
56+
### Image Processing - Local
57+
Process local image files with AI vision:
58+
```bash
59+
uv run openai_agents/basic/run_local_image_workflow.py
60+
```
61+
62+
### Image Processing - Remote
63+
Process remote image URLs with AI vision:
64+
```bash
65+
uv run openai_agents/basic/run_remote_image_workflow.py
66+
```
67+
68+
### Previous Response ID
69+
Demonstrates conversation continuity using response IDs:
70+
```bash
71+
uv run openai_agents/basic/run_previous_response_id_workflow.py
72+
```
73+
74+
## Omitted Examples
75+
76+
The following examples from the [reference repository](https://github.com/openai/openai-agents-python/tree/main/examples/basic) are not included in this Temporal adaptation:
77+
78+
- **Session** - Stores state in local SQLite database, not appropriate for distributed workflows
79+
- **Stream Items/Stream Text** - Streaming is not supported in Temporal OpenAI Agents SDK integration
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import base64
2+
3+
from temporalio import activity
4+
5+
6+
@activity.defn
7+
async def read_image_as_base64(image_path: str) -> str:
8+
"""
9+
Read an image file and convert it to base64 string.
10+
11+
Args:
12+
image_path: Path to the image file
13+
14+
Returns:
15+
Base64 encoded string of the image
16+
"""
17+
with open(image_path, "rb") as image_file:
18+
encoded_string = base64.b64encode(image_file.read()).decode("utf-8")
19+
return encoded_string
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import random
2+
3+
from temporalio import activity
4+
5+
6+
@activity.defn
7+
async def random_number(max_value: int) -> int:
8+
"""Generate a random number up to the provided maximum."""
9+
return random.randint(0, max_value)
10+
11+
12+
@activity.defn
13+
async def multiply_by_two(x: int) -> int:
14+
"""Simple multiplication by two."""
15+
return x * 2
230 KB
Loading
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import asyncio
2+
3+
from temporalio.client import Client
4+
5+
from openai_agents.basic.workflows.agent_lifecycle_workflow import (
6+
AgentLifecycleWorkflow,
7+
)
8+
9+
10+
async def main() -> None:
11+
client = await Client.connect("localhost:7233")
12+
13+
user_input = input("Enter a max number: ")
14+
max_number = int(user_input)
15+
16+
result = await client.execute_workflow(
17+
AgentLifecycleWorkflow.run,
18+
max_number,
19+
id="agent-lifecycle-workflow",
20+
task_queue="openai-agents-basic-task-queue",
21+
)
22+
23+
print(f"Final result: {result}")
24+
print("Done!")
25+
26+
27+
if __name__ == "__main__":
28+
asyncio.run(main())
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import asyncio
2+
3+
from temporalio.client import Client
4+
from temporalio.contrib.openai_agents import OpenAIAgentsPlugin
5+
6+
from openai_agents.basic.workflows.dynamic_system_prompt_workflow import (
7+
DynamicSystemPromptWorkflow,
8+
)
9+
10+
11+
async def main():
12+
client = await Client.connect(
13+
"localhost:7233",
14+
plugins=[
15+
OpenAIAgentsPlugin(),
16+
],
17+
)
18+
19+
user_message = "Tell me a joke."
20+
21+
result = await client.execute_workflow(
22+
DynamicSystemPromptWorkflow.run,
23+
user_message,
24+
id="dynamic-prompt-workflow",
25+
task_queue="openai-agents-basic-task-queue",
26+
)
27+
print(result)
28+
print()
29+
30+
# Run with specific style
31+
result = await client.execute_workflow(
32+
DynamicSystemPromptWorkflow.run,
33+
args=[user_message, "pirate"],
34+
id="dynamic-prompt-pirate-workflow",
35+
task_queue="openai-agents-basic-task-queue",
36+
)
37+
print(result)
38+
39+
40+
if __name__ == "__main__":
41+
asyncio.run(main())

openai_agents/basic/run_hello_world_workflow.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ async def main():
2020
HelloWorldAgent.run,
2121
"Tell me about recursion in programming.",
2222
id="my-workflow-id",
23-
task_queue="openai-agents-task-queue",
23+
task_queue="openai-agents-basic-task-queue",
2424
)
2525
print(f"Result: {result}")
2626

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import asyncio
2+
3+
from temporalio.client import Client
4+
from temporalio.contrib.openai_agents import OpenAIAgentsPlugin
5+
6+
from openai_agents.basic.workflows.lifecycle_workflow import LifecycleWorkflow
7+
8+
9+
async def main():
10+
client = await Client.connect(
11+
"localhost:7233",
12+
plugins=[
13+
OpenAIAgentsPlugin(),
14+
],
15+
)
16+
17+
user_input = input("Enter a max number: ")
18+
max_number = int(user_input)
19+
20+
result = await client.execute_workflow(
21+
LifecycleWorkflow.run,
22+
max_number,
23+
id="lifecycle-workflow",
24+
task_queue="openai-agents-basic-task-queue",
25+
)
26+
27+
print(f"Final result: {result}")
28+
29+
30+
if __name__ == "__main__":
31+
asyncio.run(main())
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import asyncio
2+
import os
3+
4+
from temporalio.client import Client
5+
from temporalio.contrib.openai_agents import OpenAIAgentsPlugin
6+
7+
from openai_agents.basic.workflows.local_image_workflow import LocalImageWorkflow
8+
9+
10+
async def main():
11+
client = await Client.connect(
12+
"localhost:7233",
13+
plugins=[
14+
OpenAIAgentsPlugin(),
15+
],
16+
)
17+
18+
# Use the media file from the original example
19+
image_path = os.path.join(os.path.dirname(__file__), "media/image_bison.jpg")
20+
21+
result = await client.execute_workflow(
22+
LocalImageWorkflow.run,
23+
args=[image_path, "What do you see in this image?"],
24+
id="local-image-workflow",
25+
task_queue="openai-agents-basic-task-queue",
26+
)
27+
28+
print(f"Agent response: {result}")
29+
30+
31+
if __name__ == "__main__":
32+
asyncio.run(main())
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import asyncio
2+
3+
from temporalio.client import Client
4+
from temporalio.contrib.openai_agents import OpenAIAgentsPlugin
5+
6+
from openai_agents.basic.workflows.non_strict_output_workflow import (
7+
NonStrictOutputWorkflow,
8+
)
9+
10+
11+
async def main():
12+
client = await Client.connect(
13+
"localhost:7233",
14+
plugins=[
15+
OpenAIAgentsPlugin(),
16+
],
17+
)
18+
19+
input_message = "Tell me 3 short jokes."
20+
21+
result = await client.execute_workflow(
22+
NonStrictOutputWorkflow.run,
23+
input_message,
24+
id="non-strict-output-workflow",
25+
task_queue="openai-agents-basic-task-queue",
26+
)
27+
28+
print("=== Non-Strict Output Type Results ===")
29+
for key, value in result.items():
30+
print(f"{key}: {value}")
31+
print()
32+
33+
34+
if __name__ == "__main__":
35+
asyncio.run(main())

0 commit comments

Comments
 (0)