Skip to content

Add Runtime Validation for name Attribute in Agent Class #1050

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/agents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ class Agent(Generic[TContext]):
"""

name: str
"""The name of the agent."""
"""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
Expand Down Expand Up @@ -264,6 +268,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(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")


async def get_all_tools(self, run_context: RunContextWrapper[Any]) -> list[Tool]:
"""All agent tools, including MCP tools and function tools."""
Expand Down