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
-`ToolsToFinalOutputFunction`: A custom function that processes tool results and decides whether to stop or continue with the LLM.
207
+
208
+
```python
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_weather(city: str) -> str:
215
+
"""Returns weather info for the specified city."""
216
+
returnf"The weather in {city} is sunny"
217
+
218
+
defcustom_tool_handler(
219
+
context: RunContextWrapper[Any],
220
+
tool_results: List[FunctionToolResult]
221
+
) -> ToolsToFinalOutputResult:
222
+
"""Processes tool results to decide final output."""
223
+
for result in tool_results:
224
+
if result.output and"sunny"in result.output:
225
+
return ToolsToFinalOutputResult(
226
+
is_final_output=True,
227
+
final_output=f"Final weather: {result.output}"
228
+
)
229
+
return ToolsToFinalOutputResult(
230
+
is_final_output=False,
231
+
final_output=None
232
+
)
233
+
234
+
agent = Agent(
235
+
name="Weather Agent",
236
+
instructions="Retrieve weather details.",
237
+
tools=[get_weather],
238
+
tool_use_behavior=custom_tool_handler
239
+
)
240
+
```
241
+
146
242
!!! note
147
243
148
244
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.
149
245
150
-
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