Skip to content

Commit ce7b5b1

Browse files
giles17Copilot
andauthored
Python: Fix as_agent() not defaulting name/description from client properties (#4484)
* Fix as_agent() not defaulting name/description from client properties AzureAIClient.as_agent() and AzureAIAgentClient.as_agent() now fall back to self.agent_name and self.agent_description when name/description are not explicitly passed. This ensures Agent.name is populated for telemetry spans without requiring callers to repeat the name. Fixes #4471 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Address review: use is None checks instead of truthiness Switch from name or self.agent_name to explicit is None checks so that callers can intentionally pass empty strings without them being replaced by client defaults. Added edge-case tests for empty strings. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Update docstrings to document name/description defaulting behavior Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 8bf4235 commit ce7b5b1

File tree

4 files changed

+94
-8
lines changed

4 files changed

+94
-8
lines changed

python/packages/azure-ai/agent_framework_azure_ai/_chat_client.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,8 +1461,9 @@ def as_agent(
14611461
14621462
Keyword Args:
14631463
id: The unique identifier for the agent. Will be created automatically if not provided.
1464-
name: The name of the agent.
1465-
description: A brief description of the agent's purpose.
1464+
name: The name of the agent. Defaults to the client's ``agent_name`` when None.
1465+
description: A brief description of the agent's purpose. Defaults to the client's
1466+
``agent_description`` when None.
14661467
instructions: Optional instructions for the agent.
14671468
tools: The tools to use for the request.
14681469
default_options: A TypedDict containing chat options.
@@ -1475,8 +1476,8 @@ def as_agent(
14751476
"""
14761477
return super().as_agent(
14771478
id=id,
1478-
name=name,
1479-
description=description,
1479+
name=self.agent_name if name is None else name,
1480+
description=self.agent_description if description is None else description,
14801481
instructions=instructions,
14811482
tools=tools,
14821483
default_options=default_options,

python/packages/azure-ai/agent_framework_azure_ai/_client.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,8 +1189,9 @@ def as_agent(
11891189
11901190
Keyword Args:
11911191
id: The unique identifier for the agent. Will be created automatically if not provided.
1192-
name: The name of the agent.
1193-
description: A brief description of the agent's purpose.
1192+
name: The name of the agent. Defaults to the client's ``agent_name`` when None.
1193+
description: A brief description of the agent's purpose. Defaults to the client's
1194+
``agent_description`` when None.
11941195
instructions: Optional instructions for the agent.
11951196
tools: The tools to use for the request.
11961197
default_options: A TypedDict containing chat options.
@@ -1203,8 +1204,8 @@ def as_agent(
12031204
"""
12041205
return super().as_agent(
12051206
id=id,
1206-
name=name,
1207-
description=description,
1207+
name=self.agent_name if name is None else name,
1208+
description=self.agent_description if description is None else description,
12081209
instructions=instructions,
12091210
tools=tools,
12101211
default_options=default_options,

python/packages/azure-ai/tests/test_azure_ai_agent_client.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,48 @@ async def test_azure_ai_chat_client_prepare_options_merges_instructions_from_mes
509509
assert "concise" in instructions_text.lower()
510510

511511

512+
def test_as_agent_uses_client_agent_name_as_default(mock_agents_client: MagicMock) -> None:
513+
"""Test that as_agent() defaults Agent.name to client.agent_name when name is not provided."""
514+
client = create_test_azure_ai_chat_client(mock_agents_client, agent_name="my_agent")
515+
client.agent_description = "my description"
516+
517+
agent = client.as_agent(instructions="You are helpful.")
518+
519+
assert agent.name == "my_agent"
520+
assert agent.description == "my description"
521+
522+
523+
def test_as_agent_explicit_name_overrides_client_agent_name(mock_agents_client: MagicMock) -> None:
524+
"""Test that an explicit name passed to as_agent() takes precedence over client.agent_name."""
525+
client = create_test_azure_ai_chat_client(mock_agents_client, agent_name="client_name")
526+
client.agent_description = "client description"
527+
528+
agent = client.as_agent(name="explicit_name", description="explicit description", instructions="You are helpful.")
529+
530+
assert agent.name == "explicit_name"
531+
assert agent.description == "explicit description"
532+
533+
534+
def test_as_agent_no_name_anywhere(mock_agents_client: MagicMock) -> None:
535+
"""Test that Agent.name is None when neither as_agent name nor client.agent_name is provided."""
536+
client = create_test_azure_ai_chat_client(mock_agents_client)
537+
538+
agent = client.as_agent(instructions="You are helpful.")
539+
540+
assert agent.name is None
541+
542+
543+
def test_as_agent_empty_string_preserves_explicit_value(mock_agents_client: MagicMock) -> None:
544+
"""Test that empty-string name/description are preserved and do not fall back to client defaults."""
545+
client = create_test_azure_ai_chat_client(mock_agents_client, agent_name="client_name")
546+
client.agent_description = "client description"
547+
548+
agent = client.as_agent(name="", description="", instructions="You are helpful.")
549+
550+
assert agent.name == ""
551+
assert agent.description == ""
552+
553+
512554
async def test_azure_ai_chat_client_inner_get_response(mock_agents_client: MagicMock) -> None:
513555
"""Test _inner_get_response method."""
514556
client = create_test_azure_ai_chat_client(mock_agents_client, agent_id="test-agent")

python/packages/azure-ai/tests/test_azure_ai_client.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,48 @@ def test_update_agent_name_and_description(mock_project_client: MagicMock) -> No
546546
mock_update.assert_called_once_with(None)
547547

548548

549+
def test_as_agent_uses_client_agent_name_as_default(mock_project_client: MagicMock) -> None:
550+
"""Test that as_agent() defaults Agent.name to client.agent_name when name is not provided."""
551+
client = create_test_azure_ai_client(mock_project_client, agent_name="my_agent")
552+
client.agent_description = "my description"
553+
554+
agent = client.as_agent(instructions="You are helpful.")
555+
556+
assert agent.name == "my_agent"
557+
assert agent.description == "my description"
558+
559+
560+
def test_as_agent_explicit_name_overrides_client_agent_name(mock_project_client: MagicMock) -> None:
561+
"""Test that an explicit name passed to as_agent() takes precedence over client.agent_name."""
562+
client = create_test_azure_ai_client(mock_project_client, agent_name="client_name")
563+
client.agent_description = "client description"
564+
565+
agent = client.as_agent(name="explicit_name", description="explicit description", instructions="You are helpful.")
566+
567+
assert agent.name == "explicit_name"
568+
assert agent.description == "explicit description"
569+
570+
571+
def test_as_agent_no_name_anywhere(mock_project_client: MagicMock) -> None:
572+
"""Test that Agent.name is None when neither as_agent name nor client.agent_name is provided."""
573+
client = create_test_azure_ai_client(mock_project_client)
574+
575+
agent = client.as_agent(instructions="You are helpful.")
576+
577+
assert agent.name is None
578+
579+
580+
def test_as_agent_empty_string_preserves_explicit_value(mock_project_client: MagicMock) -> None:
581+
"""Test that empty-string name/description are preserved and do not fall back to client defaults."""
582+
client = create_test_azure_ai_client(mock_project_client, agent_name="client_name")
583+
client.agent_description = "client description"
584+
585+
agent = client.as_agent(name="", description="", instructions="You are helpful.")
586+
587+
assert agent.name == ""
588+
assert agent.description == ""
589+
590+
549591
async def test_async_context_manager(mock_project_client: MagicMock) -> None:
550592
"""Test async context manager functionality."""
551593
client = create_test_azure_ai_client(mock_project_client, should_close_client=True)

0 commit comments

Comments
 (0)