diff --git a/python/packages/azure-ai/agent_framework_azure_ai/_chat_client.py b/python/packages/azure-ai/agent_framework_azure_ai/_chat_client.py index 2c0498b1e4..7236c36a32 100644 --- a/python/packages/azure-ai/agent_framework_azure_ai/_chat_client.py +++ b/python/packages/azure-ai/agent_framework_azure_ai/_chat_client.py @@ -1474,8 +1474,9 @@ def as_agent( Keyword Args: id: The unique identifier for the agent. Will be created automatically if not provided. - name: The name of the agent. - description: A brief description of the agent's purpose. + name: The name of the agent. Defaults to the client's ``agent_name`` when None. + description: A brief description of the agent's purpose. Defaults to the client's + ``agent_description`` when None. instructions: Optional instructions for the agent. tools: The tools to use for the request. default_options: A TypedDict containing chat options. @@ -1488,8 +1489,8 @@ def as_agent( """ return super().as_agent( id=id, - name=name, - description=description, + name=self.agent_name if name is None else name, + description=self.agent_description if description is None else description, instructions=instructions, tools=tools, default_options=default_options, diff --git a/python/packages/azure-ai/agent_framework_azure_ai/_client.py b/python/packages/azure-ai/agent_framework_azure_ai/_client.py index 61c4a09e94..93b287ae54 100644 --- a/python/packages/azure-ai/agent_framework_azure_ai/_client.py +++ b/python/packages/azure-ai/agent_framework_azure_ai/_client.py @@ -1181,8 +1181,9 @@ def as_agent( Keyword Args: id: The unique identifier for the agent. Will be created automatically if not provided. - name: The name of the agent. - description: A brief description of the agent's purpose. + name: The name of the agent. Defaults to the client's ``agent_name`` when None. + description: A brief description of the agent's purpose. Defaults to the client's + ``agent_description`` when None. instructions: Optional instructions for the agent. tools: The tools to use for the request. default_options: A TypedDict containing chat options. @@ -1195,8 +1196,8 @@ def as_agent( """ return super().as_agent( id=id, - name=name, - description=description, + name=self.agent_name if name is None else name, + description=self.agent_description if description is None else description, instructions=instructions, tools=tools, default_options=default_options, diff --git a/python/packages/azure-ai/tests/test_azure_ai_agent_client.py b/python/packages/azure-ai/tests/test_azure_ai_agent_client.py index 6c18352195..4d20add20a 100644 --- a/python/packages/azure-ai/tests/test_azure_ai_agent_client.py +++ b/python/packages/azure-ai/tests/test_azure_ai_agent_client.py @@ -509,6 +509,48 @@ async def test_azure_ai_chat_client_prepare_options_merges_instructions_from_mes assert "concise" in instructions_text.lower() +def test_as_agent_uses_client_agent_name_as_default(mock_agents_client: MagicMock) -> None: + """Test that as_agent() defaults Agent.name to client.agent_name when name is not provided.""" + client = create_test_azure_ai_chat_client(mock_agents_client, agent_name="my_agent") + client.agent_description = "my description" + + agent = client.as_agent(instructions="You are helpful.") + + assert agent.name == "my_agent" + assert agent.description == "my description" + + +def test_as_agent_explicit_name_overrides_client_agent_name(mock_agents_client: MagicMock) -> None: + """Test that an explicit name passed to as_agent() takes precedence over client.agent_name.""" + client = create_test_azure_ai_chat_client(mock_agents_client, agent_name="client_name") + client.agent_description = "client description" + + agent = client.as_agent(name="explicit_name", description="explicit description", instructions="You are helpful.") + + assert agent.name == "explicit_name" + assert agent.description == "explicit description" + + +def test_as_agent_no_name_anywhere(mock_agents_client: MagicMock) -> None: + """Test that Agent.name is None when neither as_agent name nor client.agent_name is provided.""" + client = create_test_azure_ai_chat_client(mock_agents_client) + + agent = client.as_agent(instructions="You are helpful.") + + assert agent.name is None + + +def test_as_agent_empty_string_preserves_explicit_value(mock_agents_client: MagicMock) -> None: + """Test that empty-string name/description are preserved and do not fall back to client defaults.""" + client = create_test_azure_ai_chat_client(mock_agents_client, agent_name="client_name") + client.agent_description = "client description" + + agent = client.as_agent(name="", description="", instructions="You are helpful.") + + assert agent.name == "" + assert agent.description == "" + + async def test_azure_ai_chat_client_inner_get_response(mock_agents_client: MagicMock) -> None: """Test _inner_get_response method.""" client = create_test_azure_ai_chat_client(mock_agents_client, agent_id="test-agent") diff --git a/python/packages/azure-ai/tests/test_azure_ai_client.py b/python/packages/azure-ai/tests/test_azure_ai_client.py index e2145618c0..8760197284 100644 --- a/python/packages/azure-ai/tests/test_azure_ai_client.py +++ b/python/packages/azure-ai/tests/test_azure_ai_client.py @@ -546,6 +546,48 @@ def test_update_agent_name_and_description(mock_project_client: MagicMock) -> No mock_update.assert_called_once_with(None) +def test_as_agent_uses_client_agent_name_as_default(mock_project_client: MagicMock) -> None: + """Test that as_agent() defaults Agent.name to client.agent_name when name is not provided.""" + client = create_test_azure_ai_client(mock_project_client, agent_name="my_agent") + client.agent_description = "my description" + + agent = client.as_agent(instructions="You are helpful.") + + assert agent.name == "my_agent" + assert agent.description == "my description" + + +def test_as_agent_explicit_name_overrides_client_agent_name(mock_project_client: MagicMock) -> None: + """Test that an explicit name passed to as_agent() takes precedence over client.agent_name.""" + client = create_test_azure_ai_client(mock_project_client, agent_name="client_name") + client.agent_description = "client description" + + agent = client.as_agent(name="explicit_name", description="explicit description", instructions="You are helpful.") + + assert agent.name == "explicit_name" + assert agent.description == "explicit description" + + +def test_as_agent_no_name_anywhere(mock_project_client: MagicMock) -> None: + """Test that Agent.name is None when neither as_agent name nor client.agent_name is provided.""" + client = create_test_azure_ai_client(mock_project_client) + + agent = client.as_agent(instructions="You are helpful.") + + assert agent.name is None + + +def test_as_agent_empty_string_preserves_explicit_value(mock_project_client: MagicMock) -> None: + """Test that empty-string name/description are preserved and do not fall back to client defaults.""" + client = create_test_azure_ai_client(mock_project_client, agent_name="client_name") + client.agent_description = "client description" + + agent = client.as_agent(name="", description="", instructions="You are helpful.") + + assert agent.name == "" + assert agent.description == "" + + async def test_async_context_manager(mock_project_client: MagicMock) -> None: """Test async context manager functionality.""" client = create_test_azure_ai_client(mock_project_client, should_close_client=True)