Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

Enable System Proxy Support for aiohttp Transport (#11616)

Del33c134c6acb1
diff --git a/litellm/__init__.py b/litellm/__init__.py
index 340e952af44..30abccbf84e 100644
--- a/litellm/__init__.py
+++ b/litellm/__init__.py
@@ -318,6 +318,9 @@ use_aiohttp_transport: bool = (
True # Older variable, aiohttp is now the default. use disable_aiohttp_transport instead.
)
disable_aiohttp_transport: bool = False # Set this to true to use httpx instead
+disable_aiohttp_trust_env: bool = (
+ False # when True, aiohttp will not trust environment variables for proxy settings
+)
force_ipv4: bool = (
False # when True, litellm will force ipv4 for all LLM requests. Some users have seen httpx ConnectionError when using ipv6.
)
diff --git a/litellm/llms/custom_httpx/http_handler.py b/litellm/llms/custom_httpx/http_handler.py
index 87eea27c4ad..0ced937396e 100644
--- a/litellm/llms/custom_httpx/http_handler.py
+++ b/litellm/llms/custom_httpx/http_handler.py
@@ -545,6 +545,8 @@ class AsyncHTTPHandler:
- [Default] If force_ipv4 is False, it will create an AiohttpTransport with default settings
"""
from litellm.llms.custom_httpx.aiohttp_transport import LiteLLMAiohttpTransport
+
+ from litellm.secret_managers.main import str_to_bool
#########################################################
# If ssl_verify is None, set it to True
@@ -553,10 +555,20 @@ class AsyncHTTPHandler:
#########################################################
if ssl_verify is None:
ssl_verify = True
+
+ #########################################################
+ # Check if user disabled aiohttp trust env
+ # When True, aiohttp will not trust environment variables for proxy settings
+ ########################################################
+ disable_trust_env = (
+ litellm.disable_aiohttp_trust_env is True
+ or str_to_bool(os.getenv("DISABLE_AIOHTTP_TRUST_ENV", "False")) is True
+ )
verbose_logger.debug("Creating AiohttpTransport...")
return LiteLLMAiohttpTransport(
client=lambda: ClientSession(
+ trust_env=not disable_trust_env,
connector=TCPConnector(
verify_ssl=ssl_verify,
ssl_context=ssl_context,
diff --git a/tests/test_litellm/llms/custom_httpx/test_http_handler.py b/tests/test_litellm/llms/custom_httpx/test_http_handler.py
index 75fb0b92984..ebd759b79ba 100644
--- a/tests/test_litellm/llms/custom_httpx/test_http_handler.py
+++ b/tests/test_litellm/llms/custom_httpx/test_http_handler.py
@@ -120,3 +120,70 @@ async def test_ssl_verification_with_aiohttp_transport():
# assert both litellm transport and aiohttp session have ssl_verify=False
assert transport_connector._ssl == aiohttp_session.connector._ssl
+
+
+async def test_disable_aiohttp_trust_env_with_env_variable(monkeypatch):
+ """Test aiohttp transport respects DISABLE_AIOHTTP_TRUST_ENV environment variable"""
+ # Set environment variable to disable trust env
+ monkeypatch.setenv("DISABLE_AIOHTTP_TRUST_ENV", "True")
+
+ # Ensure aiohttp transport is enabled
+ litellm.disable_aiohttp_transport = False
+
+ # Create async client
+ client = AsyncHTTPHandler()
+
+ # Get the transport (should be LiteLLMAiohttpTransport)
+ transport = client.client._transport
+ assert isinstance(transport, LiteLLMAiohttpTransport)
+
+ # Get the aiohttp ClientSession
+ client_session = transport._get_valid_client_session()
+
+ # Verify that trust_env is False when DISABLE_AIOHTTP_TRUST_ENV is True
+ assert client_session._trust_env is False
+
+
+async def test_disable_aiohttp_trust_env_with_litellm_setting():
+ """Test aiohttp transport respects litellm.disable_aiohttp_trust_env setting"""
+ # Set litellm setting to disable trust env
+ litellm.disable_aiohttp_trust_env = True
+
+ # Ensure aiohttp transport is enabled
+ litellm.disable_aiohttp_transport = False
+
+ # Create async client
+ client = AsyncHTTPHandler()
+
+ # Get the transport (should be LiteLLMAiohttpTransport)
+ transport = client.client._transport
+ assert isinstance(transport, LiteLLMAiohttpTransport)
+
+ # Get the aiohttp ClientSession
+ client_session = transport._get_valid_client_session()
+
+ # Verify that trust_env is False when litellm.disable_aiohttp_trust_env is True
+ assert client_session._trust_env is False
+
+
+async def test_enable_aiohttp_trust_env_default():
+ """Test aiohttp transport enables trust_env by default"""
+ # Ensure both settings are disabled/default
+ litellm.disable_aiohttp_trust_env = False
+ litellm.disable_aiohttp_transport = False
+
+ # Create async client
+ client = AsyncHTTPHandler()
+
+ # Get the transport (should be LiteLLMAiohttpTransport)
+ transport = client.client._transport
+ assert isinstance(transport, LiteLLMAiohttpTransport)
+
+ # Get the aiohttp ClientSession
+ client_session = transport._get_valid_client_session()
+
+ # Verify that trust_env is True by default
+ assert client_session._trust_env is True