From 148dbcbdca2f6000de6f9e4136b3470933148835 Mon Sep 17 00:00:00 2001 From: Engineer Abdullah Iqbal Date: Thu, 10 Jul 2025 10:16:31 +0500 Subject: [PATCH 1/2] Agent name validation --- src/agents/agent.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/agents/agent.py b/src/agents/agent.py index 6c87297f13..e57799fd39 100644 --- a/src/agents/agent.py +++ b/src/agents/agent.py @@ -79,7 +79,7 @@ class Agent(Generic[TContext]): """ name: str - """The name of the agent.""" + """The name of the agent. Must be a non-empty string.""" instructions: ( str @@ -264,6 +264,13 @@ async def get_mcp_tools( return await MCPUtil.get_all_function_tools( self.mcp_servers, convert_schemas_to_strict, run_context, self ) + + def __post_init__(self): + if not isinstance(self.name, str): + raise TypeError("name must be a string") + if not self.name.strip(): + raise ValueError("name cannot be empty or contain only whitespace") + async def get_all_tools(self, run_context: RunContextWrapper[Any]) -> list[Tool]: """All agent tools, including MCP tools and function tools.""" From a2b290bf8d4c813f8f070b5a8a16e3d03839963f Mon Sep 17 00:00:00 2001 From: Engineer Abdullah Iqbal Date: Thu, 10 Jul 2025 10:39:26 +0500 Subject: [PATCH 2/2] Error Message Updated --- src/agents/agent.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/agents/agent.py b/src/agents/agent.py index e57799fd39..db51257c3c 100644 --- a/src/agents/agent.py +++ b/src/agents/agent.py @@ -79,7 +79,11 @@ class Agent(Generic[TContext]): """ name: str - """The name of the agent. Must be a non-empty string.""" + """The name of the agent. Must be a non-empty string. + Raises: + TypeError: if not a string + ValueError: if empty or only whitespace + """ instructions: ( str @@ -267,10 +271,10 @@ async def get_mcp_tools( def __post_init__(self): if not isinstance(self.name, str): - raise TypeError("name must be a string") + raise TypeError(f"`name` must be a string, got {type(self.name).__name__!r}") if not self.name.strip(): - raise ValueError("name cannot be empty or contain only whitespace") - + raise ValueError("`name` cannot be empty or contain only whitespace") + async def get_all_tools(self, run_context: RunContextWrapper[Any]) -> list[Tool]: """All agent tools, including MCP tools and function tools."""