Skip to content

Commit c79d953

Browse files
authored
Use durable function tools (#34)
* Upgrade Restate SDK and fix readme * Fix function tools
1 parent 71e7588 commit c79d953

File tree

10 files changed

+31
-32
lines changed

10 files changed

+31
-32
lines changed

openai-agents/examples/app/rollback_agent.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import restate
22

33
from typing import Callable
4-
from agents import Agent, RunContextWrapper, function_tool
4+
from agents import Agent, RunContextWrapper
55
from pydantic import Field, BaseModel, ConfigDict
6-
from restate.ext.openai import restate_context, DurableRunner, raise_terminal_errors
6+
from restate.ext.openai import restate_context, DurableRunner, durable_function_tool
77

88
from app.utils.models import HotelBooking, FlightBooking, BookingPrompt, BookingResult
99
from app.utils.utils import (
@@ -22,7 +22,7 @@ class BookingContext(BaseModel):
2222

2323

2424
# Functions raise terminal errors instead of feeding them back to the agent
25-
@function_tool(failure_error_function=raise_terminal_errors)
25+
@durable_function_tool
2626
async def book_hotel(
2727
wrapper: RunContextWrapper[BookingContext], booking: HotelBooking
2828
) -> BookingResult:
@@ -40,7 +40,7 @@ async def book_hotel(
4040
)
4141

4242

43-
@function_tool(failure_error_function=raise_terminal_errors)
43+
@durable_function_tool
4444
async def book_flight(
4545
wrapper: RunContextWrapper[BookingContext], booking: FlightBooking
4646
) -> BookingResult:

openai-agents/template/agent.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import restate
22

3-
from agents import Agent, function_tool
4-
from restate.ext.openai import restate_context, DurableRunner
3+
from agents import Agent
4+
from restate.ext.openai import restate_context, DurableRunner, durable_function_tool
55

66
from utils.utils import (
77
fetch_weather,
@@ -11,7 +11,7 @@
1111
)
1212

1313

14-
@function_tool
14+
@durable_function_tool
1515
async def get_weather(req: WeatherRequest) -> WeatherResponse:
1616
"""Get the current weather for a given city."""
1717
# Do durable steps using the Restate context

openai-agents/tour-of-agents/app/durable_agent.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@
66
RunConfig,
77
RunContextWrapper,
88
ModelSettings,
9-
function_tool,
109
)
1110
from restate import Service, VirtualObject
12-
from restate.ext.openai import restate_context, DurableRunner
11+
from restate.ext.openai import restate_context, DurableRunner, durable_function_tool
1312

1413
from app.utils.models import WeatherPrompt, WeatherRequest, WeatherResponse
1514
from app.utils.utils import fetch_weather
1615

1716

18-
@function_tool
17+
@durable_function_tool
1918
async def get_weather(city: WeatherRequest) -> WeatherResponse:
2019
"""Get the current weather for a given city."""
2120
return await restate_context().run_typed("get weather", fetch_weather, req=city)

openai-agents/tour-of-agents/app/error_handling.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22

33
import restate
44

5-
from agents import Agent, function_tool
5+
from agents import Agent
66
from restate.ext.openai import (
77
restate_context,
88
DurableRunner,
9-
raise_terminal_errors,
109
LlmRetryOpts,
10+
durable_function_tool,
1111
)
1212

1313
from app.utils.models import WeatherPrompt, WeatherRequest, WeatherResponse
1414
from app.utils.utils import fetch_weather
1515

1616

1717
# <start_here>
18-
@function_tool(failure_error_function=raise_terminal_errors)
18+
@durable_function_tool
1919
async def get_weather(city: WeatherRequest) -> WeatherResponse:
2020
"""Get the current weather for a given city."""
2121
return await restate_context().run_typed("get weather", fetch_weather, req=city)

openai-agents/tour-of-agents/app/human_approval_agent.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import restate
22

3-
from agents import Agent, function_tool
4-
from restate.ext.openai import restate_context, DurableRunner
3+
from agents import Agent
4+
from restate.ext.openai import restate_context, DurableRunner, durable_function_tool
55

66
from app.utils.models import ClaimPrompt
77
from app.utils.utils import (
@@ -11,7 +11,7 @@
1111

1212

1313
# <start_here>
14-
@function_tool
14+
@durable_function_tool
1515
async def human_approval(claim: InsuranceClaim) -> str:
1616
"""Ask for human approval for high-value claims."""
1717

openai-agents/tour-of-agents/app/human_approval_agent_with_timeout.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from datetime import timedelta
22

33
import restate
4-
from agents import Agent, function_tool
5-
from restate.ext.openai import restate_context, DurableRunner
4+
from agents import Agent
5+
from restate.ext.openai import restate_context, DurableRunner, durable_function_tool
66

77
from app.utils.models import ClaimPrompt
88
from app.utils.utils import (
@@ -11,7 +11,7 @@
1111
)
1212

1313

14-
@function_tool
14+
@durable_function_tool
1515
async def human_approval(claim: InsuranceClaim) -> str:
1616
"""Ask for human approval for high-value claims."""
1717
# Create an awakeable for human approval

openai-agents/tour-of-agents/app/multi_agent_remote.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
import restate
22

3-
from agents import Agent, function_tool
4-
from restate.ext.openai import restate_context, DurableRunner
3+
from agents import Agent
4+
from restate.ext.openai import restate_context, DurableRunner, durable_function_tool
55

66
from app.utils.utils import InsuranceClaim, run_eligibility_agent, run_fraud_agent
77

88

99
# Durable service call to the eligibility agent; persisted and retried by Restate
10-
@function_tool
10+
@durable_function_tool
1111
async def check_eligibility(claim: InsuranceClaim) -> str:
1212
"""Analyze claim eligibility."""
1313
return await restate_context().service_call(run_eligibility_agent, claim)
1414

1515

1616
# <start_here>
1717
# Durable service call to the fraud agent; persisted and retried by Restate
18-
@function_tool
18+
@durable_function_tool
1919
async def check_fraud(claim: InsuranceClaim) -> str:
2020
"""Analyze the probability of fraud."""
2121
return await restate_context().service_call(run_fraud_agent, claim)

openai-agents/tour-of-agents/app/parallel_tools_agent.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import restate
22

3-
from agents import Agent, function_tool
4-
from restate.ext.openai import restate_context, DurableRunner
3+
from agents import Agent
4+
from restate.ext.openai import restate_context, DurableRunner, durable_function_tool
55

66
from app.utils.utils import (
77
InsuranceClaim,
@@ -12,7 +12,7 @@
1212

1313

1414
# <start_here>
15-
@function_tool
15+
@durable_function_tool
1616
async def calculate_metrics(claim: InsuranceClaim) -> list[str]:
1717
"""Calculate claim metrics."""
1818
ctx = restate_context()

openai-agents/tour-of-agents/app/sub_workflow_agent.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import restate
2-
from agents import Agent, function_tool
3-
from restate.ext.openai import restate_context, DurableRunner
2+
from agents import Agent
3+
from restate.ext.openai import restate_context, DurableRunner, durable_function_tool
44

55
from app.utils.models import ClaimPrompt
66
from app.utils.utils import (
@@ -33,7 +33,7 @@ async def review(ctx: restate.Context, claim: InsuranceClaim) -> str:
3333

3434

3535
# <start_here>
36-
@function_tool
36+
@durable_function_tool
3737
async def human_approval(claim: InsuranceClaim) -> str:
3838
"""Ask for human approval for high-value claims."""
3939
return await restate_context().service_call(review, claim)

openai-agents/tour-of-agents/app/utils/models.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from typing import Optional, Dict, Any
2-
31
from pydantic.alias_generators import to_camel as camelize
42

53
from pydantic import BaseModel, ConfigDict
@@ -12,7 +10,9 @@ class WeatherPrompt(BaseModel):
1210

1311

1412
class ClaimPrompt(BaseModel):
15-
message: str = "Process my hospital bill of 2024-10-01 for 3000USD for a broken leg at General Hospital."
13+
message: str = (
14+
"Process my hospital bill of 2024-10-01 for 3000USD for a broken leg at General Hospital."
15+
)
1616

1717

1818
class ChatMessage(BaseModel):

0 commit comments

Comments
 (0)