Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions src/google/adk/a2a/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -923,18 +923,19 @@ def rebind_client_factory_httpx(factory: Any, httpx_client: Any) -> Any:

new_factory = copy.copy(factory)
new_factory._config = config
# a2a-sdk 1.3 dropped ``ClientFactory._httpx_client``; it now reads the
# client from ``_config`` when it builds a transport, so the version header
# has to be stamped on the rebound client whether that attribute exists.
if hasattr(new_factory, "_httpx_client"):
new_factory._httpx_client = httpx_client
if hasattr(httpx_client, "headers"):
try:
from a2a.utils.constants import PROTOCOL_VERSION_CURRENT
from a2a.utils.constants import VERSION_HEADER
if IS_A2A_V1 and hasattr(httpx_client, "headers"):
try:
from a2a.utils.constants import PROTOCOL_VERSION_CURRENT
from a2a.utils.constants import VERSION_HEADER

httpx_client.headers.setdefault(
VERSION_HEADER, PROTOCOL_VERSION_CURRENT
)
except ImportError:
pass
httpx_client.headers.setdefault(VERSION_HEADER, PROTOCOL_VERSION_CURRENT)
except ImportError:
pass
if hasattr(new_factory, "_registry") and new_factory._registry is not None:
new_factory._registry = copy.copy(factory._registry)
if hasattr(new_factory, "_register_defaults"):
Expand Down
2 changes: 1 addition & 1 deletion tests/unittests/a2a/agent/test_remote_a2a_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ def create(self, *args, **kwargs):
)
if _compat.IS_A2A_V1:
assert (
agent._a2a_client_factory._httpx_client.headers[VERSION_HEADER]
agent._httpx_client.headers[VERSION_HEADER]
== PROTOCOL_VERSION_CURRENT
)
assert factory._config.httpx_client is None
Expand Down
18 changes: 18 additions & 0 deletions tests/unittests/a2a/test_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,24 @@ async def test_rebind_client_factory_httpx_defaults_protocol_bindings():
await new_client.aclose()


@pytest.mark.asyncio
async def test_rebind_client_factory_httpx_sets_version_header_on_new_client():
if not _compat.IS_A2A_V1:
pytest.skip('the A2A-Version header is 1.x only')
from a2a.utils.constants import PROTOCOL_VERSION_CURRENT
from a2a.utils.constants import VERSION_HEADER

new_client = httpx.AsyncClient()
factory = ClientFactory(ClientConfig(httpx_client=None))
rebound = _compat.rebind_client_factory_httpx(factory, new_client)

try:
assert rebound._config.httpx_client is new_client
assert new_client.headers[VERSION_HEADER] == PROTOCOL_VERSION_CURRENT
finally:
await new_client.aclose()


@pytest.mark.asyncio
async def test_rebind_client_factory_httpx_preserves_protocol_bindings():
old_client, new_client = httpx.AsyncClient(), httpx.AsyncClient()
Expand Down
Loading