openai[patch]: fix client caching when request_timeout is specified via httpx.Timeout (#31698)

Resolves https://github.com/langchain-ai/langchain/issues/31697
This commit is contained in:
ccurme
2025-06-23 10:37:49 -04:00
committed by GitHub
parent 4ee6112161
commit b268ab6a28
2 changed files with 71 additions and 4 deletions

View File

@@ -41,8 +41,7 @@ class _AsyncHttpxClientWrapper(openai.DefaultAsyncHttpxClient):
pass
@lru_cache
def _get_default_httpx_client(
def _build_sync_httpx_client(
base_url: Optional[str], timeout: Any
) -> _SyncHttpxClientWrapper:
return _SyncHttpxClientWrapper(
@@ -53,8 +52,7 @@ def _get_default_httpx_client(
)
@lru_cache
def _get_default_async_httpx_client(
def _build_async_httpx_client(
base_url: Optional[str], timeout: Any
) -> _AsyncHttpxClientWrapper:
return _AsyncHttpxClientWrapper(
@@ -63,3 +61,47 @@ def _get_default_async_httpx_client(
or "https://api.openai.com/v1",
timeout=timeout,
)
@lru_cache
def _cached_sync_httpx_client(
base_url: Optional[str], timeout: Any
) -> _SyncHttpxClientWrapper:
return _build_sync_httpx_client(base_url, timeout)
@lru_cache
def _cached_async_httpx_client(
base_url: Optional[str], timeout: Any
) -> _AsyncHttpxClientWrapper:
return _build_async_httpx_client(base_url, timeout)
def _get_default_httpx_client(
base_url: Optional[str], timeout: Any
) -> _SyncHttpxClientWrapper:
"""Get default httpx client.
Uses cached client unless timeout is ``httpx.Timeout``, which is not hashable.
"""
try:
hash(timeout)
except TypeError:
return _build_sync_httpx_client(base_url, timeout)
else:
return _cached_sync_httpx_client(base_url, timeout)
def _get_default_async_httpx_client(
base_url: Optional[str], timeout: Any
) -> _AsyncHttpxClientWrapper:
"""Get default httpx client.
Uses cached client unless timeout is ``httpx.Timeout``, which is not hashable.
"""
try:
hash(timeout)
except TypeError:
return _build_async_httpx_client(base_url, timeout)
else:
return _cached_async_httpx_client(base_url, timeout)