From dc6cb766d7c796738a5edbc01d041c9db3a132ab Mon Sep 17 00:00:00 2001 From: gioboa Date: Thu, 27 Aug 2026 22:51:53 +0200 Subject: [PATCH] fix(a2a): preserve custom client factories --- src/google/adk/agents/remote_a2a_agent.py | 6 ++++- .../unittests/agents/test_remote_a2a_agent.py | 26 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/google/adk/agents/remote_a2a_agent.py b/src/google/adk/agents/remote_a2a_agent.py index c835f12f36..6aad824102 100644 --- a/src/google/adk/agents/remote_a2a_agent.py +++ b/src/google/adk/agents/remote_a2a_agent.py @@ -810,7 +810,11 @@ async def _ensure_httpx_client(self) -> httpx.AsyncClient: timeout=httpx.Timeout(timeout=self._timeout) ) self._httpx_client_needs_cleanup = True - if self._a2a_client_factory: + # Rebuilding a subclass would discard its custom create behavior. + if ( + self._a2a_client_factory + and self._a2a_client_factory.__class__ is A2AClientFactory + ): self._a2a_client_factory = _compat.rebind_client_factory_httpx( self._a2a_client_factory, self._httpx_client ) diff --git a/tests/unittests/agents/test_remote_a2a_agent.py b/tests/unittests/agents/test_remote_a2a_agent.py index 3ce01ff869..025ea6a60b 100644 --- a/tests/unittests/agents/test_remote_a2a_agent.py +++ b/tests/unittests/agents/test_remote_a2a_agent.py @@ -442,6 +442,32 @@ async def test_ensure_httpx_client_updates_factory_with_new_client(self): assert agent._httpx_client_needs_cleanup is True assert agent._a2a_client_factory._config.httpx_client == client + async def test_ensure_resolved_uses_custom_factory(self): + """A caller-provided factory subclass creates the remote client.""" + + class CustomFactory(ClientFactory): + + def create(self, *args, **kwargs): + del args, kwargs + return custom_client + + custom_client = Mock(spec=A2AClient) + factory = CustomFactory(ClientConfig(httpx_client=None)) + agent = RemoteA2aAgent( + name="test_agent", + agent_card=create_test_agent_card(), + a2a_client_factory=factory, + ) + + client = await agent._ensure_resolved() + + try: + assert client is custom_client + assert agent._a2a_client_factory is factory + finally: + await agent._httpx_client.aclose() + await factory._httpx_client.aclose() + @pytest.mark.asyncio async def test_ensure_httpx_client_reregisters_transports_with_new_client( self,