Skip to content

Commit f696ac9

Browse files
giles17Copilot
andauthored
Python: A2AAgent defaults name/description from AgentCard (#4661)
* 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 <[email protected]> * Use 'is None' checks instead of truthiness for name/description fallback Ensures explicitly provided empty strings are not overridden by agent_card values. Adds test for the empty string edge case. Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent 5e33def commit f696ac9

2 files changed

Lines changed: 60 additions & 2 deletions

File tree

‎python/packages/a2a/agent_framework_a2a/_agent.py‎

Lines changed: 10 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,13 @@ 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+
if name is None:
134+
name = agent_card.name
135+
if description is None:
136+
description = agent_card.description
137+
130138
super().__init__(id=id, name=name, description=description, **kwargs)
131139
self._http_client: httpx.AsyncClient | None = http_client
132140
self._timeout_config = self._create_timeout_config(timeout)

‎python/packages/a2a/tests/test_a2a_agent.py‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,54 @@ 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+
178+
def test_a2a_agent_empty_string_name_description_not_overridden(mock_a2a_client: MockA2AClient) -> None:
179+
"""Test that explicitly provided empty strings are not overridden by agent_card values."""
180+
mock_card = MagicMock(spec=AgentCard)
181+
mock_card.name = "Card Agent Name"
182+
mock_card.description = "Card agent description"
183+
184+
agent = A2AAgent(
185+
name="",
186+
description="",
187+
agent_card=mock_card,
188+
client=mock_a2a_client,
189+
http_client=None,
190+
)
191+
192+
assert agent.name == ""
193+
assert agent.description == ""
194+
195+
148196
def test_a2a_agent_initialization_without_client_raises_error() -> None:
149197
"""Test A2AAgent initialization without client or URL raises ValueError."""
150198
with raises(ValueError, match="Either agent_card or url must be provided"):
@@ -561,6 +609,8 @@ def test_transport_negotiation_both_fail() -> None:
561609
# Create a mock agent card
562610
mock_agent_card = MagicMock(spec=AgentCard)
563611
mock_agent_card.url = "http://test-agent.example.com"
612+
mock_agent_card.name = "Test Agent"
613+
mock_agent_card.description = "A test agent"
564614

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

0 commit comments

Comments
 (0)