You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Drastically Improve Agent Tool Usage Clarity for All Developers
This update significantly enhances the "Tool Behavior Definitions" documentation, directly addressing the common challenges and wasted time developers previously experienced. Without clear examples and explicit guidance on import paths and usage patterns, implementing advanced agent tool behaviors was often a source of confusion and trial-and-error.
**Key improvements in this update include:**
- **Explicitly defining crucial import paths** for `StopAtTools` and `ToolsToFinalOutputFunction`, removing guesswork.
- **Providing comprehensive and corrected code examples** for all `tool_choice` and `tool_use_behavior` configurations, including `"stop_on_first_tool"`, `StopAtTools`, and `ToolsToFinalOutputFunction`. These examples are now streamlined and use consistent, easy-to-understand tools like `get_weather`.
- **Ensuring proper Markdown formatting** for code blocks and notes to enhance readability and accuracy.
My personal experience, including significant time spent troubleshooting these very behaviors due to lack of clear examples, fueled this contribution. This update aims to drastically reduce ambiguity and improve the developer experience by offering ready-to-use and well-explained code snippets, saving countless hours for others.
-`ToolsToFinalOutputFunction`: A custom function that processes tool results and decides whether to stop or continue with the LLM.
@@ -212,37 +213,36 @@ from agents.agent import ToolsToFinalOutputResult
212
213
from typing import List, Any
213
214
214
215
@function_tool
215
-
defget_product_price(product_id: str) -> str:
216
-
"""Fetches the price for a given product ID."""
217
-
prices = {"P101": "$25.00", "P102": "$49.99"}
218
-
return prices.get(product_id, "Product not found")
216
+
defget_weather(city: str) -> str:
217
+
"""Returns weather info for the specified city."""
218
+
returnf"The weather in {city} is sunny"
219
219
220
220
defcustom_tool_handler(
221
221
context: RunContextWrapper[Any],
222
222
tool_results: List[FunctionToolResult]
223
223
) -> ToolsToFinalOutputResult:
224
224
"""Processes tool results to decide final output."""
225
225
for result in tool_results:
226
-
if result.output and"$25.00"in result.output:
226
+
if result.output and"sunny"in result.output:
227
227
return ToolsToFinalOutputResult(
228
228
is_final_output=True,
229
-
final_output=f"Final result: {result.output}"
229
+
final_output=f"Final weather: {result.output}"
230
230
)
231
231
return ToolsToFinalOutputResult(
232
232
is_final_output=False,
233
233
final_output=None
234
234
)
235
235
236
-
# Create the agent
237
236
agent = Agent(
238
-
name="Product Price Agent",
239
-
instructions="Retrieve product prices and format them.",
240
-
tools=[get_product_price],
237
+
name="Weather Agent",
238
+
instructions="Retrieve weather details.",
239
+
tools=[get_weather],
241
240
tool_use_behavior=custom_tool_handler
242
241
)
243
242
244
243
```
245
-
!!! note
244
+
245
+
!!! note
246
246
247
247
To prevent infinite loops, the framework automatically resets `tool_choice` to "auto" after a tool call. This behavior is configurable via [`agent.reset_tool_choice`][agents.agent.Agent.reset_tool_choice]. The infinite loop is because tool results are sent to the LLM, which then generates another tool call because of `tool_choice`, ad infinitum.
0 commit comments