Skip to content

The agent engineering platform.

fix(openai): guard httpx finalizers (#37570)

Andy Young625ed0ee8c68
diff --git a/libs/partners/openai/langchain_openai/chat_models/_client_utils.py b/libs/partners/openai/langchain_openai/chat_models/_client_utils.py
index 1d7acc6472..d853d19d45 100644
--- a/libs/partners/openai/langchain_openai/chat_models/_client_utils.py
+++ b/libs/partners/openai/langchain_openai/chat_models/_client_utils.py
@@ -363,10 +363,9 @@ class _SyncHttpxClientWrapper(openai.DefaultHttpxClient):
"""Borrowed from openai._base_client."""
def __del__(self) -> None:
- if self.is_closed:
- return
-
try:
+ if self.is_closed:
+ return
self.close()
except Exception: # noqa: S110
pass
@@ -376,10 +375,9 @@ class _AsyncHttpxClientWrapper(openai.DefaultAsyncHttpxClient):
"""Borrowed from openai._base_client."""
def __del__(self) -> None:
- if self.is_closed:
- return
-
try:
+ if self.is_closed:
+ return
# TODO(someday): support non asyncio runtimes here
asyncio.get_running_loop().create_task(self.aclose())
except Exception: # noqa: S110
diff --git a/libs/partners/openai/tests/unit_tests/chat_models/test_client_utils.py b/libs/partners/openai/tests/unit_tests/chat_models/test_client_utils.py
index 37679c3a8e..7605776d0f 100644
--- a/libs/partners/openai/tests/unit_tests/chat_models/test_client_utils.py
+++ b/libs/partners/openai/tests/unit_tests/chat_models/test_client_utils.py
@@ -810,3 +810,21 @@ def test_client_build_applies_socket_options_when_user_opts_in(
assert all(tuple(opts) == tuple(explicit) for opts in recorded), (
f"expected user-supplied opts, got {recorded!r}"
)
+
+
+def test_sync_client_wrapper_del_handles_uninitialized_client() -> None:
+ """Test sync wrapper finalizer handles clients without initialized state."""
+ client = _client_utils._SyncHttpxClientWrapper.__new__(
+ _client_utils._SyncHttpxClientWrapper
+ )
+
+ client.__del__()
+
+
+async def test_async_client_wrapper_del_handles_uninitialized_client() -> None:
+ """Test async wrapper finalizer handles clients without initialized state."""
+ client = _client_utils._AsyncHttpxClientWrapper.__new__(
+ _client_utils._AsyncHttpxClientWrapper
+ )
+
+ client.__del__()