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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(aiohttp): honor global ssl_verify on the aiohttp_openai handler path
mateo-berrif0412345b553
diff --git a/litellm/llms/custom_httpx/aiohttp_handler.py b/litellm/llms/custom_httpx/aiohttp_handler.pyindex 9f579fd6f55..e9140e63cb3 100644--- a/litellm/llms/custom_httpx/aiohttp_handler.py+++ b/litellm/llms/custom_httpx/aiohttp_handler.py@@ -1,3 +1,4 @@+import sslfrom collections.abc import Callablefrom typing import TYPE_CHECKING, Any, Final, cast@@ -18,6 +19,7 @@ from litellm.llms.custom_httpx.http_handler import (AsyncHTTPHandler,HTTPHandler,_get_httpx_client,+ get_ssl_configuration,)from litellm.types.llms.openai import FileTypesfrom litellm.types.utils import HttpHandlerRequestFields, ImageResponse, LlmProviders@@ -56,7 +58,11 @@ class BaseLLMAIOHTTPHandler:# Create a transport using AsyncHTTPHandler's logictry:- self.transport = AsyncHTTPHandler._create_aiohttp_transport()+ ssl_config: Final = get_ssl_configuration()+ self.transport = AsyncHTTPHandler._create_aiohttp_transport(+ ssl_verify=ssl_config if isinstance(ssl_config, bool) else None,+ ssl_context=ssl_config if isinstance(ssl_config, ssl.SSLContext) else None,+ )self._owns_transport = Truereturn self.transportexcept Exception:@@ -79,20 +85,19 @@ class BaseLLMAIOHTTPHandler:def _create_client_session_with_transport(self) -> ClientSession:"""Create a new client session using transport or connector configuration."""- connector: Final = self._get_connector()-- if self.transport and hasattr(self.transport, "_get_valid_client_session"):- # Use transport's session creation if available- session = self.transport._get_valid_client_session()- return session- elif connector:- # Use provided connector- session = aiohttp.ClientSession(connector=connector)- return session- else:- # Default session creation- session = aiohttp.ClientSession()- return session+ if self.transport is None:+ connector: Final = self._get_connector()+ if connector:+ return aiohttp.ClientSession(connector=connector)++ transport: Final = self.transport or self._get_or_create_transport()+ if transport is not None and hasattr(transport, "_get_valid_client_session"):+ try:+ return transport._get_valid_client_session()+ except RuntimeError:+ pass++ return aiohttp.ClientSession()def _get_async_client_session(self, dynamic_client_session: ClientSession | None = None) -> ClientSession:if dynamic_client_session:diff --git a/tests/test_litellm/llms/custom_httpx/test_aiohttp_handler.py b/tests/test_litellm/llms/custom_httpx/test_aiohttp_handler.pyindex 763647aa463..c58e6d6cf5c 100644--- a/tests/test_litellm/llms/custom_httpx/test_aiohttp_handler.py+++ b/tests/test_litellm/llms/custom_httpx/test_aiohttp_handler.py@@ -3,7 +3,7 @@ from unittest.mock import AsyncMock, Mock, patchimport aiohttpimport pytest-+import litellmfrom litellm.llms.custom_httpx.aiohttp_handler import BaseLLMAIOHTTPHandlerfrom litellm.llms.custom_httpx.aiohttp_transport import LiteLLMAiohttpTransport@@ -318,19 +318,47 @@ class TestBaseLLMAIOHTTPHandler:mock_client_session.assert_called_once_with(connector=mock_connector)assert result is mock_session_instance- @patch("aiohttp.ClientSession")- def test_create_client_session_default(self, mock_client_session):- """Test default session creation when no transport/connector provided"""- mock_session_instance = Mock()- mock_client_session.return_value = mock_session_instance+ @pytest.mark.asyncio+ async def test_create_client_session_default_honors_global_ssl_verify_false(+ self, monkeypatch: pytest.MonkeyPatch+ ):+ """Regression test for LIT-3369: `litellm.ssl_verify = False` (set via+ `litellm_settings.ssl_verify: false`) must reach the default session's+ connector instead of being ignored by a bare `aiohttp.ClientSession()`."""+ monkeypatch.setattr(litellm, "ssl_verify", False)handler = BaseLLMAIOHTTPHandler()+ session = handler._create_client_session_with_transport()+ try:+ assert isinstance(session.connector, aiohttp.TCPConnector)+ assert session.connector._ssl is False+ finally:+ await session.close()+ await handler.close()- result = handler._create_client_session_with_transport()+ @pytest.mark.asyncio+ async def test_create_client_session_default_keeps_ssl_verification(self):+ """Default `ssl_verify=True` must not collapse to `ssl=False`."""+ handler = BaseLLMAIOHTTPHandler()+ session = handler._create_client_session_with_transport()+ try:+ assert isinstance(session.connector, aiohttp.TCPConnector)+ assert session.connector._ssl is not False+ finally:+ await session.close()+ await handler.close()- # Should create default session- mock_client_session.assert_called_once_with()- assert result is mock_session_instance+ def test_get_or_create_transport_resolves_global_ssl_verify(+ self, monkeypatch: pytest.MonkeyPatch+ ):+ """The lazily created transport must carry the resolved global ssl config."""+ monkeypatch.setattr(litellm, "ssl_verify", False)++ handler = BaseLLMAIOHTTPHandler()+ transport = handler._get_or_create_transport()++ assert transport is not None+ assert transport._ssl_verify is Falsedef test_get_or_create_transport(self):"""Test that _get_or_create_transport creates or returns a transport.