From c5f611a75dbec87e6bc1fb768d8fb1286586c693 Mon Sep 17 00:00:00 2001 From: bryan Date: Fri, 17 Apr 2026 21:44:05 +0530 Subject: [PATCH] fix: guard against missing _async_httpx_client in aclose() When __init__ raises before _async_httpx_client is assigned (e.g. an auth error thrown before any HTTP call), __del__ still schedules aclose() as an asyncio task. aclose() then crashes with: AttributeError: 'BaseApiClient' object has no attribute '_async_httpx_client' The error surfaces as an unhandled asyncio task exception, which is swallowed silently unless asyncio debug mode is on. close() already guards the equivalent sync path with `and self._httpx_client`; apply the same pattern to aclose() using getattr(..., None) to safely handle the uninitialized case. Fixes the unhandled task exception reported when using the async client with a missing or invalid API key. --- google/genai/_api_client.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/google/genai/_api_client.py b/google/genai/_api_client.py index bb9865cc4..a8bddbecd 100644 --- a/google/genai/_api_client.py +++ b/google/genai/_api_client.py @@ -2101,7 +2101,9 @@ async def aclose(self) -> None: """Closes the API async client.""" # Let users close the custom client explicitly by themselves. Otherwise, # close the client when the object is garbage collected. - if not self._http_options.httpx_async_client: + if not self._http_options.httpx_async_client and getattr( + self, '_async_httpx_client', None + ): await self._async_httpx_client.aclose() # type: ignore[union-attr] if self._aiohttp_session and not self._http_options.aiohttp_client: await self._aiohttp_session.close()