Skip to content

fix: _is_openai_client_closed handles is_closed as method (openai SDK >=2.21.0)#4383

Open
ekkoitac wants to merge 1 commit intoNousResearch:mainfrom
ekkoitac:fix/is-closed-method-not-property
Open

fix: _is_openai_client_closed handles is_closed as method (openai SDK >=2.21.0)#4383
ekkoitac wants to merge 1 commit intoNousResearch:mainfrom
ekkoitac:fix/is-closed-method-not-property

Conversation

@ekkoitac
Copy link
Copy Markdown
Contributor

@ekkoitac ekkoitac commented Apr 1, 2026

Bug: _is_openai_client_closed false positive — openai SDK is_closed is a method, not a property

Fixes #4377

Problem

In openai SDK v2.21.0+, OpenAI.is_closed is a bound method, not a bool property. The old code:

if bool(getattr(client, "is_closed", False)):

bool(<bound method>) is always True, even when the client is open. This causes every API call to:

  1. Falsely detect the shared client as closed
  2. Create a new shared client (TCP + TLS handshake)
  3. Close the old healthy client
  4. Create a per-request client (another TCP + TLS handshake)
  5. Close the per-request client after the stream

This adds ~100-200ms of connection overhead per API call.

Fix

Check if is_closed is callable and call it if so:

is_closed = getattr(client, "is_closed", None)
if callable(is_closed):
    if is_closed():
        return True
elif bool(is_closed):
    return True
http_client = getattr(client, "_client", None)
if http_client is not None:
    return bool(getattr(http_client, "is_closed", False))
return False

Also guard against http_client being None.

… >=2.21.0)

In openai SDK v2.21.0+, OpenAI.is_closed is a bound method, not a
bool property. bool(<bound method>) is always True, causing every
API call to unnecessarily create+close+recreate the OpenAI client,
adding ~100-200ms connection overhead per call.

Fix: check if is_closed is callable and call it if so, falling back
to the httpx _client.is_closed check. Also guard against http_client
being None.

Closes NousResearch#4377
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants