@@ -48,11 +48,13 @@ The most common properties of an agent are:
4848from agents import Agent
4949from agents.decorators import tool
5050
51+
5152@tool
5253def get_weather (city : str ) -> str :
5354 """ returns weather info for the specified city."""
5455 return f " The weather in { city} is sunny "
5556
57+
5658agent = Agent(
5759 name = " Haiku agent" ,
5860 instructions = " Always respond in haiku form" ,
@@ -99,6 +101,7 @@ from dataclasses import dataclass
99101
100102from agents import Agent, GenerateDynamicPromptData, Runner
101103
104+
102105@dataclass
103106class PromptContext :
104107 prompt_id: str
@@ -131,10 +134,12 @@ Read the [context guide](context.md) for the full `RunContextWrapper` surface, s
131134``` python
132135from dataclasses import dataclass
133136
137+
134138@dataclass
135139class Purchase :
136140 id : str
137141
142+
138143@dataclass
139144class UserContext :
140145 name: str
@@ -145,6 +150,7 @@ class UserContext:
145150 # implement your logic here
146151 return []
147152
153+
148154agent = Agent[UserContext](
149155 ... ,
150156)
@@ -164,6 +170,7 @@ class CalendarEvent(BaseModel):
164170 date: str
165171 participants: list[str ]
166172
173+
167174agent = Agent(
168175 name = " Calendar extractor" ,
169176 instructions = " Extract calendar events from text" ,
@@ -208,7 +215,7 @@ customer_facing_agent = Agent(
208215 refund_agent.as_tool(
209216 tool_name = " refund_expert" ,
210217 tool_description = " Handles refund questions and requests." ,
211- )
218+ ),
212219 ],
213220)
214221```
@@ -241,9 +248,8 @@ In most cases, you can provide instructions when you create the agent. However,
241248``` python
242249from agents import Agent, RunContextWrapper
243250
244- def dynamic_instructions (
245- context : RunContextWrapper[UserContext], agent : Agent[UserContext]
246- ) -> str :
251+
252+ def dynamic_instructions (context : RunContextWrapper[UserContext], agent : Agent[UserContext]) -> str :
247253 return f " The user's name is { context.context.name} . Help them with their questions. "
248254
249255
@@ -336,16 +342,18 @@ When you are using OpenAI Responses tool search, named tool choices are more lim
336342from agents import Agent, ModelSettings
337343from agents.decorators import tool
338344
345+
339346@tool
340347def get_weather (city : str ) -> str :
341348 """ Returns weather info for the specified city."""
342349 return f " The weather in { city} is sunny "
343350
351+
344352agent = Agent(
345353 name = " Weather Agent" ,
346354 instructions = " Retrieve weather details." ,
347355 tools = [get_weather],
348- model_settings = ModelSettings(tool_choice = " get_weather" )
356+ model_settings = ModelSettings(tool_choice = " get_weather" ),
349357)
350358```
351359
@@ -360,16 +368,18 @@ The `tool_use_behavior` parameter in the `Agent` configuration controls how tool
360368from agents import Agent
361369from agents.decorators import tool
362370
371+
363372@tool
364373def get_weather (city : str ) -> str :
365374 """ Returns weather info for the specified city."""
366375 return f " The weather in { city} is sunny "
367376
377+
368378agent = Agent(
369379 name = " Weather Agent" ,
370380 instructions = " Retrieve weather details." ,
371381 tools = [get_weather],
372- tool_use_behavior = " stop_on_first_tool"
382+ tool_use_behavior = " stop_on_first_tool" ,
373383)
374384```
375385
@@ -380,21 +390,24 @@ from agents import Agent
380390from agents.agent import StopAtTools
381391from agents.decorators import tool
382392
393+
383394@tool
384395def get_weather (city : str ) -> str :
385396 """ Returns weather info for the specified city."""
386397 return f " The weather in { city} is sunny "
387398
399+
388400@tool
389401def sum_numbers (a : int , b : int ) -> int :
390402 """ Adds two numbers."""
391403 return a + b
392404
405+
393406agent = Agent(
394407 name = " Stop At Stock Agent" ,
395408 instructions = " Get weather or sum numbers." ,
396409 tools = [get_weather, sum_numbers],
397- tool_use_behavior = StopAtTools(stop_at_tool_names = [" get_weather" ])
410+ tool_use_behavior = StopAtTools(stop_at_tool_names = [" get_weather" ]),
398411)
399412```
400413
@@ -406,32 +419,30 @@ from agents.agent import ToolsToFinalOutputResult
406419from agents.decorators import tool
407420from typing import List, Any
408421
422+
409423@tool
410424def get_weather (city : str ) -> str :
411425 """ Returns weather info for the specified city."""
412426 return f " The weather in { city} is sunny "
413427
428+
414429def custom_tool_handler (
415- context : RunContextWrapper[Any],
416- tool_results : List[FunctionToolResult]
430+ context : RunContextWrapper[Any], tool_results : List[FunctionToolResult]
417431) -> ToolsToFinalOutputResult:
418432 """ Processes tool results to decide final output."""
419433 for result in tool_results:
420434 if result.output and " sunny" in result.output:
421435 return ToolsToFinalOutputResult(
422- is_final_output = True ,
423- final_output = f " Final weather: { result.output} "
436+ is_final_output = True , final_output = f " Final weather: { result.output} "
424437 )
425- return ToolsToFinalOutputResult(
426- is_final_output = False ,
427- final_output = None
428- )
438+ return ToolsToFinalOutputResult(is_final_output = False , final_output = None )
439+
429440
430441agent = Agent(
431442 name = " Weather Agent" ,
432443 instructions = " Retrieve weather details." ,
433444 tools = [get_weather],
434- tool_use_behavior = custom_tool_handler
445+ tool_use_behavior = custom_tool_handler,
435446)
436447```
437448
0 commit comments