diff --git a/litellm/router.py b/litellm/router.py index bf5781c563..a970b5d449 100644 --- a/litellm/router.py +++ b/litellm/router.py @@ -1407,6 +1407,32 @@ class Router: max_retries = litellm.get_secret(max_retries_env_name) litellm_params["max_retries"] = max_retries + + # proxy support + import os + import httpx + + # 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) + + # Create the proxies dictionary only if the environment variables are set. + sync_proxy_mounts = None + async_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)), + } + async_proxy_mounts = { + "http://": httpx.AsyncHTTPTransport( + proxy=httpx.Proxy(url=http_proxy) + ), + "https://": httpx.AsyncHTTPTransport( + proxy=httpx.Proxy(url=https_proxy) + ), + } + organization = litellm_params.get("organization", None) if isinstance(organization, str) and organization.startswith("os.environ/"): organization_env_name = organization.replace("os.environ/", "") @@ -1437,6 +1463,7 @@ class Router: limits=httpx.Limits( max_connections=1000, max_keepalive_connections=100 ), + mounts=async_proxy_mounts, ), # type: ignore ) self.cache.set_cache( @@ -1458,6 +1485,7 @@ class Router: limits=httpx.Limits( max_connections=1000, max_keepalive_connections=100 ), + mounts=sync_proxy_mounts, ), # type: ignore ) self.cache.set_cache( @@ -1479,6 +1507,7 @@ class Router: limits=httpx.Limits( max_connections=1000, max_keepalive_connections=100 ), + mounts=async_proxy_mounts, ), # type: ignore ) self.cache.set_cache( @@ -1500,6 +1529,7 @@ class Router: limits=httpx.Limits( max_connections=1000, max_keepalive_connections=100 ), + mounts=sync_proxy_mounts, ), # type: ignore ) self.cache.set_cache( @@ -1535,6 +1565,7 @@ class Router: limits=httpx.Limits( max_connections=1000, max_keepalive_connections=100 ), + mounts=async_proxy_mounts, ), # type: ignore ) self.cache.set_cache( @@ -1554,6 +1585,7 @@ class Router: limits=httpx.Limits( max_connections=1000, max_keepalive_connections=100 ), + mounts=sync_proxy_mounts, ), # type: ignore ) self.cache.set_cache( @@ -1574,6 +1606,7 @@ class Router: limits=httpx.Limits( max_connections=1000, max_keepalive_connections=100 ), + mounts=async_proxy_mounts, ), ) self.cache.set_cache( @@ -1593,6 +1626,7 @@ class Router: limits=httpx.Limits( max_connections=1000, max_keepalive_connections=100 ), + mounts=sync_proxy_mounts, ), ) self.cache.set_cache( @@ -1618,6 +1652,7 @@ class Router: limits=httpx.Limits( max_connections=1000, max_keepalive_connections=100 ), + mounts=async_proxy_mounts, ), # type: ignore ) self.cache.set_cache( @@ -1639,6 +1674,7 @@ class Router: limits=httpx.Limits( max_connections=1000, max_keepalive_connections=100 ), + mounts=sync_proxy_mounts, ), # type: ignore ) self.cache.set_cache( @@ -1661,6 +1697,7 @@ class Router: limits=httpx.Limits( max_connections=1000, max_keepalive_connections=100 ), + mounts=async_proxy_mounts, ), # type: ignore ) self.cache.set_cache( @@ -1683,6 +1720,7 @@ class Router: limits=httpx.Limits( max_connections=1000, max_keepalive_connections=100 ), + mounts=sync_proxy_mounts, ), # type: ignore ) self.cache.set_cache(