Skip to content

Commit d2bb078

Browse files
committed
refactor(tools): tighten HttpxClientFactory type and document client lifecycle
- Tighten HttpxClientFactory to Callable[[], httpx.AsyncClient] to match the zero-arg call site. - Document per-request client lifecycle (closed after each request); drop the inaccurate "shared connection pools" claim.
1 parent be08e06 commit d2bb078

2 files changed

Lines changed: 22 additions & 18 deletions

File tree

src/google/adk/tools/openapi_tool/openapi_spec_parser/openapi_toolset.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,14 @@ def __init__(
133133
context. Useful for adding custom headers like correlation IDs,
134134
authentication tokens, or other request metadata.
135135
httpx_client_factory: Optional zero-argument callable returning an
136-
``httpx.AsyncClient`` to use for every generated tool's API calls.
137-
When provided, it takes precedence over the per-tool default client
136+
``httpx.AsyncClient`` to use for every generated tool's API calls. When
137+
provided, it takes precedence over the per-tool default client
138138
construction and unlocks ``httpx.AsyncClient`` options that
139139
``ssl_verify`` can't reach (proxies, HTTP/2, custom transports such as
140-
request signing, shared connection pools). Defaults to ``None``, which
141-
preserves today's behaviour. Mirrors the pattern exposed for MCP by
140+
request signing). The returned client is used as an async context
141+
manager and closed after each request, so the factory must return a
142+
fresh client on every call. Defaults to ``None``, which preserves
143+
today's behaviour. Mirrors the pattern exposed for MCP by
142144
``StreamableHTTPConnectionParams.httpx_client_factory``.
143145
preserve_property_names: If True, preserve the original property names
144146
from the OpenAPI spec instead of converting them to snake_case. This

src/google/adk/tools/openapi_tool/openapi_spec_parser/rest_api_tool.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,16 @@ def snake_to_lower_camel(snake_case_string: str):
7575

7676
AuthPreparationState = Literal["pending", "done"]
7777

78-
HttpxClientFactory = Callable[..., httpx.AsyncClient]
79-
"""Type alias for a factory returning an ``httpx.AsyncClient``.
78+
HttpxClientFactory = Callable[[], httpx.AsyncClient]
79+
"""Type alias for a zero-argument factory returning an ``httpx.AsyncClient``.
8080
8181
When supplied to ``RestApiTool`` or ``OpenAPIToolset``, the factory is invoked
82-
once per API call and its returned client is used (as an async context
83-
manager) to issue the request, in place of the default
84-
``httpx.AsyncClient(verify=..., timeout=None)``. This unlocks knobs that the
85-
narrower ``ssl_verify`` parameter can't reach: proxies, HTTP/2, custom
86-
transports (e.g. request-signing), shared connection pools, and so on.
82+
once per API call and its returned client is used as an async context manager
83+
to issue the request, in place of the default
84+
``httpx.AsyncClient(verify=..., timeout=None)``. Because the client is closed
85+
when the request completes, the factory must return a fresh client on every
86+
call. This unlocks knobs that the narrower ``ssl_verify`` parameter can't
87+
reach: proxies, HTTP/2, custom transports (e.g. request-signing), and so on.
8788
"""
8889

8990

@@ -155,13 +156,14 @@ def __init__(
155156
context. Useful for adding custom headers like correlation IDs,
156157
authentication tokens, or other request metadata.
157158
httpx_client_factory: Optional zero-argument callable returning an
158-
``httpx.AsyncClient``. When provided, the returned client is used to
159-
issue the request, allowing callers to configure proxies, HTTP/2,
160-
custom transports (e.g. request signing), shared connection pools,
161-
or any other ``httpx.AsyncClient`` option that ``ssl_verify`` can't
162-
reach. When ``None`` (default), behaviour is unchanged: a fresh
163-
``httpx.AsyncClient(verify=..., timeout=None)`` is created per
164-
request. Mirrors the pattern exposed for MCP by
159+
``httpx.AsyncClient``. When provided, the returned client is used as
160+
an async context manager to issue the request and is closed once the
161+
request completes, so the factory must return a fresh client on each
162+
call. This lets callers configure proxies, HTTP/2, custom transports
163+
(e.g. request signing), or any other ``httpx.AsyncClient`` option
164+
that ``ssl_verify`` can't reach. When ``None`` (default), behaviour
165+
is unchanged: a fresh ``httpx.AsyncClient(verify=..., timeout=None)``
166+
is created per request. Mirrors the pattern exposed for MCP by
165167
``StreamableHTTPConnectionParams.httpx_client_factory``.
166168
credential_key: Optional stable key used for interactive auth and
167169
credential caching.

0 commit comments

Comments
 (0)