mirror of
https://github.com/hwchase17/langchain.git
synced 2025-08-31 10:23:18 +00:00
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:
@@ -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)
|
||||
|
Reference in New Issue
Block a user