Skip to content

Commit a0882bd

Browse files
sasha-gitgcopybara-github
authored andcommitted
fix: unbreak client closed errors when using vertexai session service
PiperOrigin-RevId: 811911528
1 parent 4343332 commit a0882bd

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

google/genai/_api_client.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,3 +1775,20 @@ async def aclose(self) -> None:
17751775
await self._async_httpx_client.aclose()
17761776
if self._aiohttp_session:
17771777
await self._aiohttp_session.close()
1778+
1779+
def __del__(self) -> None:
1780+
"""Closes the API client when the object is garbage collected.
1781+
1782+
ADK uses this client so cannot rely on the genai.[Async]Client.__del__
1783+
for cleanup.
1784+
"""
1785+
1786+
try:
1787+
self.close()
1788+
except Exception: # pylint: disable=broad-except
1789+
pass
1790+
1791+
try:
1792+
asyncio.get_running_loop().create_task(self.aclose())
1793+
except Exception: # pylint: disable=broad-except
1794+
pass

google/genai/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414
#
1515

16+
import asyncio
1617
import os
1718
from types import TracebackType
1819
from typing import Optional, Union
@@ -125,7 +126,6 @@ async def __aexit__(
125126
await self.aclose()
126127

127128
def __del__(self) -> None:
128-
import asyncio
129129
try:
130130
asyncio.get_running_loop().create_task(self.aclose())
131131
except Exception:

0 commit comments

Comments
 (0)