From 5cc466f1d9d022b3125016e59875e53b61799dda Mon Sep 17 00:00:00 2001 From: Asna Siddiqui Date: Wed, 30 Jul 2025 21:07:33 +0500 Subject: [PATCH 1/2] Fixed name coercion for int and boolean in Agent class with passing tests --- src/agents/agent.py | 10 ++++++++++ tests/test_agent_name.py | 14 ++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 tests/test_agent_name.py diff --git a/src/agents/agent.py b/src/agents/agent.py index c6f25b08f..1975c268c 100644 --- a/src/agents/agent.py +++ b/src/agents/agent.py @@ -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 @@ -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 as string, can be provided as int or bool which will be converted to string"}) + + def __post_init__(self): + if not isinstance(self.name, str): + self.name = str(self.name) + instructions: ( str | Callable[ diff --git a/tests/test_agent_name.py b/tests/test_agent_name.py new file mode 100644 index 000000000..85607e5d9 --- /dev/null +++ b/tests/test_agent_name.py @@ -0,0 +1,14 @@ +import pytest +from src.agents import Agent +def test_agent_name_types(): + # String name + agent1 = Agent(name="Test", instructions="Test", model="gpt-4o") + assert agent1.name == "Test" + + # Integer name + agent2 = Agent(name=123, instructions="Test", model="gpt-4o") + assert agent2.name == "123" + + # Boolean name + agent3 = Agent(name=True, instructions="Test", model="gpt-4o") + assert agent3.name == "True" \ No newline at end of file From 1c5fcc6606b43d509d89ff3721e3c5902370dbe0 Mon Sep 17 00:00:00 2001 From: Asna Siddiqui Date: Wed, 30 Jul 2025 21:33:00 +0500 Subject: [PATCH 2/2] Restrict Agent name to string only, raise TypeError for int/boolean --- src/agents/agent.py | 7 +++---- tests/test_agent_name.py | 19 ++++++++++--------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/agents/agent.py b/src/agents/agent.py index 1975c268c..895671484 100644 --- a/src/agents/agent.py +++ b/src/agents/agent.py @@ -140,11 +140,11 @@ class Agent(AgentBase, Generic[TContext]): See `AgentBase` for base parameters that are shared with `RealtimeAgent`s. """ - name: str = field(default=..., metadata={"description": "Agent name as string, can be provided as int or bool which will be converted to string"}) - + 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): - self.name = str(self.name) + raise TypeError("Agent name must be a string, got {} instead".format(type(self.name).__name__)) instructions: ( str @@ -230,7 +230,6 @@ def __post_init__(self): 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: ``` diff --git a/tests/test_agent_name.py b/tests/test_agent_name.py index 85607e5d9..cc0594811 100644 --- a/tests/test_agent_name.py +++ b/tests/test_agent_name.py @@ -1,14 +1,15 @@ import pytest from src.agents import Agent + def test_agent_name_types(): - # String name + # String name (should pass) agent1 = Agent(name="Test", instructions="Test", model="gpt-4o") assert agent1.name == "Test" - - # Integer name - agent2 = Agent(name=123, instructions="Test", model="gpt-4o") - assert agent2.name == "123" - - # Boolean name - agent3 = Agent(name=True, instructions="Test", model="gpt-4o") - assert agent3.name == "True" \ No newline at end of file + + # 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") \ No newline at end of file