From 22a0c400dd5aaef4ec99239572b5f750e1307792 Mon Sep 17 00:00:00 2001 From: Krrish Dholakia Date: Sat, 1 Jun 2024 13:56:45 -0700 Subject: [PATCH] fix(http_handler.py): support verify_ssl=False when using httpx client --- litellm/llms/custom_httpx/http_handler.py | 49 ++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/litellm/llms/custom_httpx/http_handler.py b/litellm/llms/custom_httpx/http_handler.py index 1efbb45018c..4f312a351b1 100644 --- a/litellm/llms/custom_httpx/http_handler.py +++ b/litellm/llms/custom_httpx/http_handler.py @@ -1,4 +1,5 @@ -import httpx, asyncio, traceback +import litellm +import httpx, asyncio, traceback, os from typing import Optional, Union, Mapping, Any # https://www.python-httpx.org/advanced/timeouts @@ -11,6 +12,29 @@ class AsyncHTTPHandler: timeout: Optional[Union[float, httpx.Timeout]] = None, concurrent_limit=1000, ): + sync_proxy_mounts = None + async_proxy_mounts = None + # Check if the HTTP_PROXY and HTTPS_PROXY environment variables are set and use them accordingly. + http_proxy = os.getenv("HTTP_PROXY", None) + https_proxy = os.getenv("HTTPS_PROXY", None) + no_proxy = os.getenv("NO_PROXY", None) + ssl_verify = bool(os.getenv("SSL_VERIFY", litellm.ssl_verify)) + + sync_proxy_mounts = None + if http_proxy is not None and https_proxy is not None: + async_proxy_mounts = { + "http://": httpx.AsyncHTTPTransport(proxy=httpx.Proxy(url=http_proxy)), + "https://": httpx.AsyncHTTPTransport( + proxy=httpx.Proxy(url=https_proxy) + ), + } + # assume no_proxy is a list of comma separated urls + if no_proxy is not None and isinstance(no_proxy, str): + no_proxy_urls = no_proxy.split(",") + + for url in no_proxy_urls: # set no-proxy support for specific urls + async_proxy_mounts[url] = None # type: ignore + if timeout is None: timeout = _DEFAULT_TIMEOUT # Create a client with a connection pool @@ -20,6 +44,8 @@ class AsyncHTTPHandler: max_connections=concurrent_limit, max_keepalive_connections=concurrent_limit, ), + verify=ssl_verify, + mounts=async_proxy_mounts, ) async def close(self): @@ -77,6 +103,25 @@ class HTTPHandler: if timeout is None: timeout = _DEFAULT_TIMEOUT + # Check if the HTTP_PROXY and HTTPS_PROXY environment variables are set and use them accordingly. + http_proxy = os.getenv("HTTP_PROXY", None) + https_proxy = os.getenv("HTTPS_PROXY", None) + no_proxy = os.getenv("NO_PROXY", None) + ssl_verify = os.getenv("SSL_VERIFY", litellm.ssl_verify) + + sync_proxy_mounts = None + if http_proxy is not None and https_proxy is not None: + sync_proxy_mounts = { + "http://": httpx.HTTPTransport(proxy=httpx.Proxy(url=http_proxy)), + "https://": httpx.HTTPTransport(proxy=httpx.Proxy(url=https_proxy)), + } + # assume no_proxy is a list of comma separated urls + if no_proxy is not None and isinstance(no_proxy, str): + no_proxy_urls = no_proxy.split(",") + + for url in no_proxy_urls: # set no-proxy support for specific urls + sync_proxy_mounts[url] = None # type: ignore + if client is None: # Create a client with a connection pool self.client = httpx.Client( @@ -85,6 +130,8 @@ class HTTPHandler: max_connections=concurrent_limit, max_keepalive_connections=concurrent_limit, ), + verify=ssl_verify, + mounts=sync_proxy_mounts, ) else: self.client = client