Files
langchain/libs/partners
Bagatur e0ace8227c fix(anthropic, openai): never close shared/cached httpx pools on aclose()
The first cut of close()/aclose() unconditionally closed the SDK
client's underlying httpx pool. But both integrations back their
clients with a PROCESS-WIDE SHARED pool via @lru_cache
(`_get_default_*httpx_client`): every model with the same
base_url/timeout/proxy reuses one pool by design. Closing it from a
single model's teardown broke every other live model in the process —
observed in a long-lived worker as:

    RuntimeError: Cannot send a request, as the client has been closed.
    -> anthropic.APIConnectionError: Connection error.

Fix: close()/aclose() now release the underlying httpx client ONLY when
the model privately owns it; the shared cached pool and user-supplied
clients are left intact.

- anthropic: `ChatAnthropic` always wraps the shared cached pool (it has
  no http_client field), so close()/aclose() are effectively no-ops for
  the pool. An identity check against the lru-cache getter
  (`_wraps_shared_httpx`) guards a hypothetical future private-client
  path. `_http_client_params()` is factored out so the cached_property
  builders and the identity check stay in sync.
- openai: ownership is computed in `validate_environment` and stored on
  `_owns_sync_http_client` / `_owns_async_http_client`. A client is owned
  iff the model built it privately — the unhashable-`httpx.Timeout`
  fresh-client path or an `openai_proxy` transport — and the user did not
  supply their own `http_client` / `http_async_client`. Default (shared
  cache) and user-supplied clients are never closed.

Tests rewritten to pin the invariant: a regression test builds two
default models, closes one, and asserts the other's shared pool is still
open; plus owned-path and user-injected-not-closed cases.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-28 16:47:23 -04:00
..

FAQ

Looking for an integration not listed here? Check out the integrations documentation and the note in the libs/ README about third-party maintained packages.

Integration docs

For full documentation, see the primary and API reference docs for integrations.