@@ -76,22 +76,29 @@ In some cases, you might want to access extra metadata about the tool being exec
7676For this, you can use the [ ` ToolContext ` ] [ agents.tool_context.ToolContext ] class, which extends ` RunContextWrapper ` .
7777
7878``` python
79- from dataclasses import dataclass
80- from agents import function_tool
79+ from typing import Annotated
80+ from pydantic import BaseModel, Field
81+ from agents import Agent, Runner, function_tool
8182from agents.tool_context import ToolContext
8283
83- @dataclass
84- class UserInfo :
85- name: str
86- uid: int
84+ class WeatherContext (BaseModel ):
85+ user_id: str
86+
87+ class Weather (BaseModel ):
88+ city: str = Field(description = " The city name" )
89+ temperature_range: str = Field(description = " The temperature range in Celsius" )
90+ conditions: str = Field(description = " The weather conditions" )
8791
8892@function_tool
89- async def fetch_user_age (ctx : ToolContext[UserInfo]) -> str :
90- """ Fetch the age of the user, with access to tool metadata."""
91- print (ctx.tool_name) # e.g. "fetch_user_age"
92- print (ctx.tool_call_id) # unique ID for this invocation
93- print (ctx.tool_arguments) # raw arguments as string
94- return f " The user { ctx.context.name} is 47 years old. "
93+ def get_weather (ctx : ToolContext[WeatherContext], city : Annotated[str , " The city to get the weather for" ]) -> Weather:
94+ print (f " [debug] Tool context: (name: { ctx.tool_name} , call_id: { ctx.tool_call_id} , args: { ctx.tool_arguments} ) " )
95+ return Weather(city = city, temperature_range = " 14-20C" , conditions = " Sunny with wind." )
96+
97+ agent = Agent(
98+ name = " Weather Agent" ,
99+ instructions = " You are a helpful agent that can tell the weather of a given city." ,
100+ tools = [get_weather],
101+ )
95102```
96103
97104` ToolContext ` provides the same ` .context ` property as ` RunContextWrapper ` ,
0 commit comments