Skip to content
Open
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
6 changes: 5 additions & 1 deletion src/google/adk/agents/remote_a2a_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
26 changes: 26 additions & 0 deletions tests/unittests/agents/test_remote_a2a_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down