Skip to content

Commit 1275b3c

Browse files
committed
Return Weather object from get_weather
1 parent e17f349 commit 1275b3c

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

samples-v2/openai_agents/function_app.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from dataclasses import dataclass
2+
import json
23
import os
34
import azure.functions as func
45
from openai import AsyncAzureOpenAI
@@ -8,6 +9,7 @@
89
from model_activity import create_invoke_model_activity
910
from azure.identity import DefaultAzureCredential
1011
from tools import activity_as_tool
12+
from pydantic import BaseModel
1113

1214

1315
#region Regular Azure OpenAI setup
@@ -132,19 +134,25 @@ def weather_expert(context):
132134
result = Runner.run_sync(agent, "What is the weather in Tokio?")
133135
return result.final_output
134136

135-
@dataclass
136-
class Weather:
137+
class Weather(BaseModel):
137138
city: str
138139
temperature_range: str
139140
conditions: str
140141

142+
def to_json(self) -> str:
143+
return self.model_dump_json()
144+
145+
@staticmethod
146+
def from_json(data: str) -> "Weather":
147+
return Weather(**json.loads(data))
148+
141149
@app.activity_trigger(input_name="city")
142-
async def get_weather(city: str) -> str:
150+
async def get_weather(city: str) -> Weather:
143151
"""
144152
Get the weather for a given city.
145153
"""
146154
weather = Weather(city=city, temperature_range="14-20C", conditions="Sunny with wind.")
147-
return f"Weather in {weather.city}: {weather.temperature_range}, {weather.conditions}"
155+
return weather
148156

149157

150158
@function_tool

samples-v2/openai_agents/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ azure-functions-durable
77
azure-identity
88
openai
99
openai-agents
10+
pydantic

0 commit comments

Comments
 (0)