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]

fix(http_handler.py): support reading ssl security level from env var

Krrish Dholakia8e27b2026ab3
diff --git a/litellm/llms/custom_httpx/http_handler.py b/litellm/llms/custom_httpx/http_handler.py
index 736b85dc538..cafa530130f 100644
--- a/litellm/llms/custom_httpx/http_handler.py
+++ b/litellm/llms/custom_httpx/http_handler.py
@@ -1,10 +1,12 @@
import asyncio
import os
+import ssl
import time
from typing import TYPE_CHECKING, Any, Callable, List, Mapping, Optional, Union
import httpx
from httpx import USE_CLIENT_DEFAULT, AsyncHTTPTransport, HTTPTransport
+from httpx._types import VerifyTypes
import litellm
from litellm.litellm_core_utils.logging_utils import track_llm_api_timing
@@ -94,7 +96,7 @@ class AsyncHTTPHandler:
event_hooks: Optional[Mapping[str, List[Callable[..., Any]]]] = None,
concurrent_limit=1000,
client_alias: Optional[str] = None, # name for client in logs
- ssl_verify: Optional[Union[bool, str]] = None,
+ ssl_verify: Optional[VerifyTypes] = None,
):
self.timeout = timeout
self.event_hooks = event_hooks
@@ -111,13 +113,33 @@ class AsyncHTTPHandler:
timeout: Optional[Union[float, httpx.Timeout]],
concurrent_limit: int,
event_hooks: Optional[Mapping[str, List[Callable[..., Any]]]],
- ssl_verify: Optional[Union[bool, str]] = None,
+ ssl_verify: Optional[VerifyTypes] = None,
) -> httpx.AsyncClient:
# SSL certificates (a.k.a CA bundle) used to verify the identity of requested hosts.
# /path/to/certificate.pem
if ssl_verify is None:
ssl_verify = os.getenv("SSL_VERIFY", litellm.ssl_verify)
+
+ ssl_security_level = os.getenv("SSL_SECURITY_LEVEL")
+
+ # If ssl_verify is not False and we need a lower security level
+ if (
+ not ssl_verify
+ and ssl_security_level
+ and isinstance(ssl_security_level, str)
+ ):
+ # Create a custom SSL context with reduced security level
+ custom_ssl_context = ssl.create_default_context()
+ custom_ssl_context.set_ciphers(ssl_security_level)
+
+ # If ssl_verify is a path to a CA bundle, load it into our custom context
+ if isinstance(ssl_verify, str) and os.path.exists(ssl_verify):
+ custom_ssl_context.load_verify_locations(cafile=ssl_verify)
+
+ # Use our custom SSL context instead of the original ssl_verify value
+ ssl_verify = custom_ssl_context
+
# An SSL certificate used by the requested host to authenticate the client.
# /path/to/client.pem
cert = os.getenv("SSL_CERTIFICATE", litellm.ssl_certificate)
diff --git a/tests/litellm/llms/custom_httpx/test_http_handler.py b/tests/litellm/llms/custom_httpx/test_http_handler.py
new file mode 100644
index 00000000000..9ab8e24f856
--- /dev/null
+++ b/tests/litellm/llms/custom_httpx/test_http_handler.py
@@ -0,0 +1,29 @@
+import io
+import os
+import pathlib
+import ssl
+import sys
+from unittest.mock import MagicMock
+
+import pytest
+
+sys.path.insert(
+ 0, os.path.abspath("../../../..")
+) # Adds the parent directory to the system path
+import litellm
+from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler, HTTPHandler
+
+
+async def test_ssl_security_level(monkeypatch):
+ # Set environment variable for SSL security level
+ monkeypatch.setenv("SSL_SECURITY_LEVEL", "DEFAULT@SECLEVEL=1")
+
+ # Create async client with SSL verification disabled to isolate SSL context testing
+ client = AsyncHTTPHandler(ssl_verify=False)
+
+ # Get the SSL context from the client
+ ssl_context = client.client._transport._pool._ssl_context
+
+ # Verify that the SSL context exists and has the correct cipher string
+ assert isinstance(ssl_context, ssl.SSLContext)