Skip to content

Restrict Agent name to string only, raise TypeError for int/boolean #1314

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
Closed
Show file tree
Hide file tree
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
11 changes: 10 additions & 1 deletion src/agents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
from .util import _transforms
from .util._types import MaybeAwaitable

# Pydantic ke liye import add kiya
from pydantic import BaseModel, Field
from typing import Union

if TYPE_CHECKING:
from .lifecycle import AgentHooks
from .mcp import MCPServer
Expand Down Expand Up @@ -136,6 +140,12 @@ class Agent(AgentBase, Generic[TContext]):
See `AgentBase` for base parameters that are shared with `RealtimeAgent`s.
"""

name: str = field(default=..., metadata={"description": "Agent name must be a string, int or boolean will raise an error"})

def __post_init__(self):
if not isinstance(self.name, str):
raise TypeError("Agent name must be a string, got {} instead".format(type(self.name).__name__))

instructions: (
str
| Callable[
Expand Down Expand Up @@ -220,7 +230,6 @@ class Agent(AgentBase, Generic[TContext]):
reset_tool_choice: bool = True
"""Whether to reset the tool choice to the default value after a tool has been called. Defaults
to True. This ensures that the agent doesn't enter an infinite loop of tool usage."""

def clone(self, **kwargs: Any) -> Agent[TContext]:
"""Make a copy of the agent, with the given arguments changed. For example, you could do:
```
Expand Down
15 changes: 15 additions & 0 deletions tests/test_agent_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pytest
from src.agents import Agent

def test_agent_name_types():
# String name (should pass)
agent1 = Agent(name="Test", instructions="Test", model="gpt-4o")
assert agent1.name == "Test"

# Integer name (should raise TypeError)
with pytest.raises(TypeError):
Agent(name=123, instructions="Test", model="gpt-4o")

# Boolean name (should raise TypeError)
with pytest.raises(TypeError):
Agent(name=True, instructions="Test", model="gpt-4o")