From edb8d7459ee8d08972bcefe2c47a3cf61c3b0f2a Mon Sep 17 00:00:00 2001 From: SyedShahmeerAli12 Date: Wed, 25 Mar 2026 13:32:12 +0500 Subject: [PATCH 1/3] fix(google_ai): add close/close_async lifecycle methods to GoogleGenAIChatGenerator Using GoogleGenAIChatGenerator in an AsyncPipeline triggered 'ResourceWarning: Unclosed client session' because the underlying HTTP client was never explicitly closed. Add close() and close_async() methods so callers and pipelines can release resources deterministically. Also expose sync and async context manager support (__enter__/__exit__ and __aenter__/__aexit__) for use in with-statement patterns. Fixes #2628 Co-Authored-By: Claude Sonnet 4.6 --- .../google_genai/chat/chat_generator.py | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/integrations/google_genai/src/haystack_integrations/components/generators/google_genai/chat/chat_generator.py b/integrations/google_genai/src/haystack_integrations/components/generators/google_genai/chat/chat_generator.py index 6e689ccf3a..74cbfe9b55 100644 --- a/integrations/google_genai/src/haystack_integrations/components/generators/google_genai/chat/chat_generator.py +++ b/integrations/google_genai/src/haystack_integrations/components/generators/google_genai/chat/chat_generator.py @@ -266,6 +266,36 @@ def __init__( self._timeout = timeout self._max_retries = max_retries + def close(self) -> None: + """ + Closes the underlying Google GenAI HTTP client and releases its resources. + + Call this method when the component is no longer needed, or use the component + as a context manager (``with`` statement) to have it closed automatically. + """ + self._client.close() + + async def close_async(self) -> None: + """ + Async version of :meth:`close`. Closes the underlying async HTTP client session. + + Call this when running inside an async context (e.g. ``AsyncPipeline``) to avoid + ``ResourceWarning: Unclosed client session`` warnings. + """ + await self._client.aio.close() + + def __enter__(self) -> "GoogleGenAIChatGenerator": + return self + + def __exit__(self, *args: object) -> None: + self.close() + + async def __aenter__(self) -> "GoogleGenAIChatGenerator": + return self + + async def __aexit__(self, *args: object) -> None: + await self.close_async() + def to_dict(self) -> dict[str, Any]: """ Serializes the component to a dictionary. From c600da85d220a0e8cb37138cbb03a8c73743cda6 Mon Sep 17 00:00:00 2001 From: SyedShahmeerAli12 Date: Wed, 25 Mar 2026 13:48:26 +0500 Subject: [PATCH 2/3] fix: use aclose() instead of close() for AsyncClient --- .../components/generators/google_genai/chat/chat_generator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integrations/google_genai/src/haystack_integrations/components/generators/google_genai/chat/chat_generator.py b/integrations/google_genai/src/haystack_integrations/components/generators/google_genai/chat/chat_generator.py index 74cbfe9b55..350581411e 100644 --- a/integrations/google_genai/src/haystack_integrations/components/generators/google_genai/chat/chat_generator.py +++ b/integrations/google_genai/src/haystack_integrations/components/generators/google_genai/chat/chat_generator.py @@ -282,7 +282,7 @@ async def close_async(self) -> None: Call this when running inside an async context (e.g. ``AsyncPipeline``) to avoid ``ResourceWarning: Unclosed client session`` warnings. """ - await self._client.aio.close() + await self._client.aio.aclose() def __enter__(self) -> "GoogleGenAIChatGenerator": return self From 89b628e1ed1ce8a9c80245e307e6865239c16621 Mon Sep 17 00:00:00 2001 From: SyedShahmeerAli12 Date: Wed, 25 Mar 2026 14:51:40 +0500 Subject: [PATCH 3/3] fix: suppress mypy false positive on AsyncClient.aclose() stub Co-Authored-By: Claude Sonnet 4.6 --- .../components/generators/google_genai/chat/chat_generator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integrations/google_genai/src/haystack_integrations/components/generators/google_genai/chat/chat_generator.py b/integrations/google_genai/src/haystack_integrations/components/generators/google_genai/chat/chat_generator.py index 350581411e..aa48fb4515 100644 --- a/integrations/google_genai/src/haystack_integrations/components/generators/google_genai/chat/chat_generator.py +++ b/integrations/google_genai/src/haystack_integrations/components/generators/google_genai/chat/chat_generator.py @@ -282,7 +282,7 @@ async def close_async(self) -> None: Call this when running inside an async context (e.g. ``AsyncPipeline``) to avoid ``ResourceWarning: Unclosed client session`` warnings. """ - await self._client.aio.aclose() + await self._client.aio.aclose() # type: ignore[attr-defined] def __enter__(self) -> "GoogleGenAIChatGenerator": return self