Skip to content

Commit 43bf53d

Browse files
giles17Copilot
andcommitted
Python: A2AAgent defaults name/description from AgentCard
When an AgentCard is provided but name/description are not explicitly set, A2AAgent now falls back to agent_card.name and agent_card.description. This avoids redundant duplication when constructing A2AAgent instances, especially in GroupChat orchestrations where name and description are essential for routing decisions. Explicit values still take precedence over card values. Fixes #4630 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent b6a1315 commit 43bf53d

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

python/packages/a2a/agent_framework_a2a/_agent.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,10 @@ def __init__(
114114
"""Initialize the A2AAgent.
115115
116116
Keyword Args:
117-
name: The name of the agent.
117+
name: The name of the agent. Defaults to agent_card.name if agent_card is provided.
118118
id: The unique identifier for the agent, will be created automatically if not provided.
119-
description: A brief description of the agent's purpose.
119+
description: A brief description of the agent's purpose. Defaults to agent_card.description
120+
if agent_card is provided.
120121
agent_card: The agent card for the agent.
121122
url: The URL for the A2A server.
122123
client: The A2A client for the agent.
@@ -127,6 +128,11 @@ def __init__(
127128
10.0s write, 5.0s pool - optimized for A2A operations).
128129
kwargs: any additional properties, passed to BaseAgent.
129130
"""
131+
# Default name/description from agent_card when not explicitly provided
132+
if agent_card is not None:
133+
name = name or agent_card.name
134+
description = description or agent_card.description
135+
130136
super().__init__(id=id, name=name, description=description, **kwargs)
131137
self._http_client: httpx.AsyncClient | None = http_client
132138
self._timeout_config = self._create_timeout_config(timeout)

python/packages/a2a/tests/test_a2a_agent.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,36 @@ def test_a2a_agent_initialization_with_client(mock_a2a_client: MockA2AClient) ->
145145
assert agent.client == mock_a2a_client
146146

147147

148+
def test_a2a_agent_defaults_name_description_from_agent_card(mock_a2a_client: MockA2AClient) -> None:
149+
"""Test A2AAgent defaults name and description from agent_card when not explicitly provided."""
150+
mock_card = MagicMock(spec=AgentCard)
151+
mock_card.name = "Card Agent Name"
152+
mock_card.description = "Card agent description"
153+
154+
agent = A2AAgent(agent_card=mock_card, client=mock_a2a_client, http_client=None)
155+
156+
assert agent.name == "Card Agent Name"
157+
assert agent.description == "Card agent description"
158+
159+
160+
def test_a2a_agent_explicit_name_description_overrides_agent_card(mock_a2a_client: MockA2AClient) -> None:
161+
"""Test that explicit name/description take precedence over agent_card values."""
162+
mock_card = MagicMock(spec=AgentCard)
163+
mock_card.name = "Card Agent Name"
164+
mock_card.description = "Card agent description"
165+
166+
agent = A2AAgent(
167+
name="Explicit Name",
168+
description="Explicit description",
169+
agent_card=mock_card,
170+
client=mock_a2a_client,
171+
http_client=None,
172+
)
173+
174+
assert agent.name == "Explicit Name"
175+
assert agent.description == "Explicit description"
176+
177+
148178
def test_a2a_agent_initialization_without_client_raises_error() -> None:
149179
"""Test A2AAgent initialization without client or URL raises ValueError."""
150180
with raises(ValueError, match="Either agent_card or url must be provided"):
@@ -561,6 +591,8 @@ def test_transport_negotiation_both_fail() -> None:
561591
# Create a mock agent card
562592
mock_agent_card = MagicMock(spec=AgentCard)
563593
mock_agent_card.url = "http://test-agent.example.com"
594+
mock_agent_card.name = "Test Agent"
595+
mock_agent_card.description = "A test agent"
564596

565597
# Mock the factory to simulate both primary and fallback failures
566598
mock_factory = MagicMock()

0 commit comments

Comments
 (0)