Skip to content

Commit cbb2d84

Browse files
fix(client): ignore the patch version when preferring a 1.0 interface (a2aproject#1304)
# Description `ClientFactory._find_best_interface` prefers a 1.0 interface with an exact string comparison (`protocol_version == '1.0'`). When an agent card lists a legacy interface and a 1.0 interface written with a patch version, e.g. ```python supported_interfaces=[ AgentInterface(url='http://agent/v03', protocol_binding='JSONRPC', protocol_version='0.3.0'), AgentInterface(url='http://agent/v1', protocol_binding='JSONRPC', protocol_version='1.0.0'), ] ``` the `1.0.0` entry is neither equal to `'1.0'` nor greater than `1.0`, so the loop falls through to the first `>= 0.3` candidate and the client is built with `CompatJsonRpcTransport` against `/v03`, i.e. it speaks v0.3 to an agent that supports 1.0. (`'1.0'` and `'1.1'` are handled correctly.) The specification's versioning section says patch versions "MUST not be considered when clients and servers negotiate protocol versions". Patch versions do show up in practice; several tests in this repo build cards with `protocol_version='1.0.0'`. - Compare the `Major.Minor` release (`packaging.version.Version(...).release[:2]`) instead of the raw string, via a small `_major_minor` helper (kept out of the loop for ruff's PERF203). - Regression test: with `0.3.0` listed first, both `1.0` and `1.0.0` must select the 1.0 interface and `JsonRpcTransport`. The `1.0.0` case fails on main with `CompatJsonRpcTransport`. Checks: `scripts/lint.sh` clean, `uv run pytest` 2183 passed (plus `tests/integration/cross_version` 5 passed), coverage 93%. - [x] Follow the [`CONTRIBUTING` Guide](https://github.com/a2aproject/a2a-python/blob/main/CONTRIBUTING.md). - [x] Make your Pull Request title in the <https://www.conventionalcommits.org/> specification. - [x] Ensure the tests and linter pass - [x] Appropriate docs were updated (if necessary) --------- Co-authored-by: mykytanetipa <mykytanetipa@google.com>
1 parent e11cb3e commit cbb2d84

2 files changed

Lines changed: 45 additions & 7 deletions

File tree

‎src/a2a/client/client_factory.py‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@
5555
]
5656

5757

58+
def major_minor(version: str) -> tuple[int, int]:
59+
"""Returns the `Major.Minor` release of a protocol version."""
60+
v = Version(version)
61+
return (v.major, v.minor)
62+
63+
5864
class ClientFactory:
5965
"""Factory for creating clients that communicate with A2A agents.
6066
@@ -229,10 +235,10 @@ def _find_best_interface(
229235
if not candidates:
230236
return None
231237

232-
# Prefer interface with version 1.0
233-
for i in candidates:
234-
if i.protocol_version == PROTOCOL_VERSION_1_0:
235-
return i
238+
# Prefer interface with version 1.0. Patch versions must not affect
239+
# negotiation, so '1.0.0' counts as '1.0'.
240+
v1_0 = major_minor(PROTOCOL_VERSION_1_0)
241+
v0_3 = major_minor(PROTOCOL_VERSION_0_3)
236242

237243
best_gt_1_0 = None
238244
best_ge_0_3 = None
@@ -245,10 +251,12 @@ def _find_best_interface(
245251
continue
246252

247253
try:
248-
v = Version(i.protocol_version)
249-
if best_gt_1_0 is None and v > Version(PROTOCOL_VERSION_1_0):
254+
v = major_minor(i.protocol_version)
255+
if v == v1_0:
256+
return i
257+
if best_gt_1_0 is None and v > v1_0:
250258
best_gt_1_0 = i
251-
if best_ge_0_3 is None and v >= Version(PROTOCOL_VERSION_0_3):
259+
if best_ge_0_3 is None and v >= v0_3:
252260
best_ge_0_3 = i
253261
except InvalidVersion:
254262
pass

‎tests/client/test_client_factory.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,36 @@ def test_client_factory_server_preference(base_agent_card: AgentCard):
115115
assert client._transport.url == 'http://primary-url.com' # type: ignore[attr-defined]
116116

117117

118+
@pytest.mark.parametrize('v1_version', ['1.0', '1.0.0'])
119+
def test_client_factory_prefers_1_0_ignoring_patch_version(
120+
base_agent_card: AgentCard, v1_version: str
121+
):
122+
"""A patch version must not affect protocol negotiation (spec: Versioning)."""
123+
del base_agent_card.supported_interfaces[:]
124+
base_agent_card.supported_interfaces.extend(
125+
[
126+
AgentInterface(
127+
protocol_binding=TransportProtocol.JSONRPC,
128+
url='http://legacy-url.com',
129+
protocol_version='0.3.0',
130+
),
131+
AgentInterface(
132+
protocol_binding=TransportProtocol.JSONRPC,
133+
url='http://v1-url.com',
134+
protocol_version=v1_version,
135+
),
136+
]
137+
)
138+
config = ClientConfig(
139+
httpx_client=httpx.AsyncClient(),
140+
supported_protocol_bindings=[TransportProtocol.JSONRPC],
141+
)
142+
client = ClientFactory(config).create(base_agent_card)
143+
144+
assert isinstance(client._transport, JsonRpcTransport) # type: ignore[attr-defined]
145+
assert client._transport.url == 'http://v1-url.com' # type: ignore[attr-defined]
146+
147+
118148
def test_client_factory_no_compatible_transport(base_agent_card: AgentCard):
119149
"""Verify that the factory raises an error if no compatible transport is found."""
120150
config = ClientConfig(

0 commit comments

Comments
 (0)