From 2823d27eb5a0edefa7bab9d62686537b39277686 Mon Sep 17 00:00:00 2001 From: MUHAMMAD SALMAN HUSSAIN <160324527+mshsheikh@users.noreply.github.com> Date: Sun, 3 Aug 2025 05:57:40 +0500 Subject: [PATCH 1/3] Fix type annotation, avoid input shadowing, and clarify tool behavior docs This commit improves type safety by replacing Any with the correct generic type TContext in the get\_all\_tools method. It also avoids confusion by renaming the input parameter in the run\_agent function to user\_input because input is a built-in function in Python. Lastly, it updates the documentation of tool\_use\_behavior to clearly describe the use of the StopAtTools object instead of a plain list of tool names. These changes make the code more accurate, easier to understand, and better aligned with best practices. --- src/agents/agent.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/agents/agent.py b/src/agents/agent.py index bd1b13c2a..bb3c67814 100644 --- a/src/agents/agent.py +++ b/src/agents/agent.py @@ -101,7 +101,7 @@ async def get_mcp_tools(self, run_context: RunContextWrapper[TContext]) -> list[ self.mcp_servers, convert_schemas_to_strict, run_context, self ) - async def get_all_tools(self, run_context: RunContextWrapper[Any]) -> list[Tool]: + async def get_all_tools(self, run_context: RunContextWrapper[TContext]) -> list[Tool]: """All agent tools, including MCP tools and function tools.""" mcp_tools = await self.get_mcp_tools(run_context) @@ -201,14 +201,16 @@ class Agent(AgentBase, Generic[TContext]): tool_use_behavior: ( Literal["run_llm_again", "stop_on_first_tool"] | StopAtTools | ToolsToFinalOutputFunction ) = "run_llm_again" - """This lets you configure how tool use is handled. + """ + This lets you configure how tool use is handled. - "run_llm_again": The default behavior. Tools are run, and then the LLM receives the results and gets to respond. - "stop_on_first_tool": The output of the first tool call is used as the final output. This means that the LLM does not process the result of the tool call. - - A list of tool names: The agent will stop running if any of the tools in the list are called. - The final output will be the output of the first matching tool call. The LLM does not - process the result of the tool call. + - A StopAtTools object: The agent will stop running if any of the tools listed in + `stop_at_tool_names` is called. + The final output will be the output of the first matching tool call. + The LLM does not process the result of the tool call. - A function: If you pass a function, it will be called with the run context and the list of tool results. It must return a `ToolsToFinalOutputResult`, which determines whether the tool calls result in a final output. @@ -262,7 +264,7 @@ def as_tool( name_override=tool_name or _transforms.transform_string_function_style(self.name), description_override=tool_description or "", ) - async def run_agent(context: RunContextWrapper, input: str) -> str: + async def run_agent(context: RunContextWrapper, user_input: str) -> str: from .run import Runner output = await Runner.run( From dbf7fdc32518804379da30eae6669bdf91b8679d Mon Sep 17 00:00:00 2001 From: MUHAMMAD SALMAN HUSSAIN <160324527+mshsheikh@users.noreply.github.com> Date: Sun, 3 Aug 2025 06:03:02 +0500 Subject: [PATCH 2/3] Fix incorrect input argument passed to Runner.run Use user\_input instead of input to avoid passing the built-in function by mistake. --- src/agents/agent.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/agents/agent.py b/src/agents/agent.py index bb3c67814..4ecbfc171 100644 --- a/src/agents/agent.py +++ b/src/agents/agent.py @@ -269,7 +269,7 @@ async def run_agent(context: RunContextWrapper, user_input: str) -> str: output = await Runner.run( starting_agent=self, - input=input, + input=user_input, context=context.context, ) if custom_output_extractor: From ca0ad0ce37201b81d663a26939b288192ba7ec7d Mon Sep 17 00:00:00 2001 From: MUHAMMAD SALMAN HUSSAIN <160324527+mshsheikh@users.noreply.github.com> Date: Sun, 3 Aug 2025 06:34:56 +0500 Subject: [PATCH 3/3] Reverted input variable rename, safe for local usage --- src/agents/agent.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/agents/agent.py b/src/agents/agent.py index 4ecbfc171..2a9985d37 100644 --- a/src/agents/agent.py +++ b/src/agents/agent.py @@ -264,12 +264,12 @@ def as_tool( name_override=tool_name or _transforms.transform_string_function_style(self.name), description_override=tool_description or "", ) - async def run_agent(context: RunContextWrapper, user_input: str) -> str: + async def run_agent(context: RunContextWrapper, input: str) -> str: from .run import Runner output = await Runner.run( starting_agent=self, - input=user_input, + input=input, context=context.context, ) if custom_output_extractor: