From 16b6df8b59f2bd3b9d301f8a03ef8f2b0f6a627c Mon Sep 17 00:00:00 2001 From: Sunhwan Jo Date: Tue, 7 Apr 2026 14:00:53 -0700 Subject: [PATCH 1/2] fix: guard aclose() against missing attributes during GC --- google/genai/_api_client.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/google/genai/_api_client.py b/google/genai/_api_client.py index 8efae0bf3..e93ee5fbd 100644 --- a/google/genai/_api_client.py +++ b/google/genai/_api_client.py @@ -2096,9 +2096,10 @@ 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: + http_options = getattr(self, '_http_options', None) + if http_options and not 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: + if getattr(self, '_aiohttp_session', None) and http_options and not http_options.aiohttp_client: await self._aiohttp_session.close() def __del__(self) -> None: From 22ee573f6bbb5b25c239fdd787eb8fd5810c0ba0 Mon Sep 17 00:00:00 2001 From: Sunhwan Jo Date: Wed, 8 Apr 2026 08:32:43 -0700 Subject: [PATCH 2/2] fix: add type ignore comment for mypy union-attr error --- google/genai/_api_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google/genai/_api_client.py b/google/genai/_api_client.py index e93ee5fbd..4f64ba465 100644 --- a/google/genai/_api_client.py +++ b/google/genai/_api_client.py @@ -2100,7 +2100,7 @@ async def aclose(self) -> None: if http_options and not http_options.httpx_async_client and getattr(self, '_async_httpx_client', None): await self._async_httpx_client.aclose() # type: ignore[union-attr] if getattr(self, '_aiohttp_session', None) and http_options and not http_options.aiohttp_client: - await self._aiohttp_session.close() + await self._aiohttp_session.close() # type: ignore[union-attr] def __del__(self) -> None: """Closes the API client when the object is garbage collected.