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
Docs: Improve and clarify tool behavior definitions Enhances the "Tool Behavior Definitions" documentation to provide clearer explanations and more robust examples for agent tool usage. Key improvements include: - Explicitly defining import paths for StopAtTools and ToolsToFinalOutputFunction. - Providing comprehensive and corrected code examples for all tool_choice and tool_use_behavior configurations, including "stop_on_first_tool", StopAtTools, and the usage of ToolsToFinalOutputFunction. - Ensuring proper Markdown formatting for code blocks and notes to enhance readability and accuracy. This update aims to significantly reduce ambiguity and improve the developer experience by offering ready-to-use and well-explained code snippets.
-`ToolsToFinalOutputFunction`: A custom function that processes tool results and decides whether to stop or continue with the LLM.
206
+
207
+
```python
208
+
209
+
from agents import Agent, Runner, function_tool, FunctionToolResult, RunContextWrapper
210
+
from agents.agent import ToolsToFinalOutputResult
211
+
from typing import List, Any
212
+
213
+
@function_tool
214
+
defget_product_price(product_id: str) -> str:
215
+
"""Fetches the price for a given product ID."""
216
+
prices = {"P101": "$25.00", "P102": "$49.99"}
217
+
return prices.get(product_id, "Product not found")
218
+
219
+
defcustom_tool_handler(
220
+
context: RunContextWrapper[Any],
221
+
tool_results: List[FunctionToolResult]
222
+
) -> ToolsToFinalOutputResult:
223
+
"""Processes tool results to decide final output."""
224
+
for result in tool_results:
225
+
if result.output and"$25.00"in result.output:
226
+
return ToolsToFinalOutputResult(
227
+
is_final_output=True,
228
+
final_output=f"Final result: {result.output}"
229
+
)
230
+
return ToolsToFinalOutputResult(
231
+
is_final_output=False,
232
+
final_output=None
233
+
)
234
+
235
+
# Create the agent
236
+
agent = Agent(
237
+
name="Product Price Agent",
238
+
instructions="Retrieve product prices and format them.",
239
+
tools=[get_product_price],
240
+
tool_use_behavior=custom_tool_handler
241
+
)
242
+
243
+
```
244
+
145
245
!!! note
146
246
147
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.
148
248
149
-
If you want the Agent to completely stop after a tool call (rather than continuing with auto mode), you can set [`Agent.tool_use_behavior="stop_on_first_tool"`] which will directly use the tool output as the final response without further LLM processing.
0 commit comments