forked from phoenix/litellm-mirror
(fix) httpx handler - bind to ipv4 for httpx handler (#6785)
* bind to ipv4 on httpx handler * add force_ipv4 * use helper for _create_async_transport * fix circular import * document force_ipv4 * test_async_http_handler_force_ipv4
This commit is contained in:
parent
b854f6c07b
commit
f43768d617
4 changed files with 87 additions and 1 deletions
|
@ -4,7 +4,7 @@ import traceback
|
|||
from typing import TYPE_CHECKING, Any, Callable, List, Mapping, Optional, Union
|
||||
|
||||
import httpx
|
||||
from httpx import USE_CLIENT_DEFAULT
|
||||
from httpx import USE_CLIENT_DEFAULT, AsyncHTTPTransport, HTTPTransport
|
||||
|
||||
import litellm
|
||||
|
||||
|
@ -60,8 +60,10 @@ class AsyncHTTPHandler:
|
|||
if timeout is None:
|
||||
timeout = _DEFAULT_TIMEOUT
|
||||
# Create a client with a connection pool
|
||||
transport = self._create_async_transport()
|
||||
|
||||
return httpx.AsyncClient(
|
||||
transport=transport,
|
||||
event_hooks=event_hooks,
|
||||
timeout=timeout,
|
||||
limits=httpx.Limits(
|
||||
|
@ -297,6 +299,18 @@ class AsyncHTTPHandler:
|
|||
except Exception:
|
||||
pass
|
||||
|
||||
def _create_async_transport(self) -> Optional[AsyncHTTPTransport]:
|
||||
"""
|
||||
Create an async transport with IPv4 only if litellm.force_ipv4 is True.
|
||||
Otherwise, return None.
|
||||
|
||||
Some users have seen httpx ConnectionError when using ipv6 - forcing ipv4 resolves the issue for them
|
||||
"""
|
||||
if litellm.force_ipv4:
|
||||
return AsyncHTTPTransport(local_address="0.0.0.0")
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
class HTTPHandler:
|
||||
def __init__(
|
||||
|
@ -316,8 +330,11 @@ class HTTPHandler:
|
|||
cert = os.getenv("SSL_CERTIFICATE", litellm.ssl_certificate)
|
||||
|
||||
if client is None:
|
||||
transport = self._create_sync_transport()
|
||||
|
||||
# Create a client with a connection pool
|
||||
self.client = httpx.Client(
|
||||
transport=transport,
|
||||
timeout=timeout,
|
||||
limits=httpx.Limits(
|
||||
max_connections=concurrent_limit,
|
||||
|
@ -427,6 +444,18 @@ class HTTPHandler:
|
|||
except Exception:
|
||||
pass
|
||||
|
||||
def _create_sync_transport(self) -> Optional[HTTPTransport]:
|
||||
"""
|
||||
Create an HTTP transport with IPv4 only if litellm.force_ipv4 is True.
|
||||
Otherwise, return None.
|
||||
|
||||
Some users have seen httpx ConnectionError when using ipv6 - forcing ipv4 resolves the issue for them
|
||||
"""
|
||||
if litellm.force_ipv4:
|
||||
return HTTPTransport(local_address="0.0.0.0")
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
def get_async_httpx_client(
|
||||
llm_provider: Union[LlmProviders, httpxSpecialProvider],
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue