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
Update var name for consistency
Dharamendra Kumar04acd4b73908
diff --git a/litellm/llms/custom_httpx/http_handler.py b/litellm/llms/custom_httpx/http_handler.pyindex 18b0cbd594f..706b09bb098 100644--- a/litellm/llms/custom_httpx/http_handler.py+++ b/litellm/llms/custom_httpx/http_handler.py@@ -167,7 +167,7 @@ class AsyncHTTPHandler:concurrent_limit=1000,client_alias: Optional[str] = None, # name for client in logsssl_verify: Optional[VerifyTypes] = None,- existing_session: Optional["ClientSession"] = None,+ shared_session: Optional["ClientSession"] = None,):self.timeout = timeoutself.event_hooks = event_hooks@@ -176,7 +176,7 @@ class AsyncHTTPHandler:concurrent_limit=concurrent_limit,event_hooks=event_hooks,ssl_verify=ssl_verify,- existing_session=existing_session,+ shared_session=shared_session,)self.client_alias = client_alias@@ -186,7 +186,7 @@ class AsyncHTTPHandler:concurrent_limit: int,event_hooks: Optional[Mapping[str, List[Callable[..., Any]]]],ssl_verify: Optional[VerifyTypes] = None,- existing_session: Optional["ClientSession"] = None,+ shared_session: Optional["ClientSession"] = None,) -> httpx.AsyncClient:# Get unified SSL configurationssl_config = get_ssl_configuration(ssl_verify)@@ -202,7 +202,7 @@ class AsyncHTTPHandler:transport = AsyncHTTPHandler._create_async_transport(ssl_context=ssl_config if isinstance(ssl_config, ssl.SSLContext) else None,ssl_verify=ssl_config if isinstance(ssl_config, bool) else None,- existing_session=existing_session,+ shared_session=shared_session,)return httpx.AsyncClient(@@ -528,7 +528,7 @@ class AsyncHTTPHandler:def _create_async_transport(ssl_context: Optional[ssl.SSLContext] = None,ssl_verify: Optional[bool] = None,- existing_session: Optional["ClientSession"] = None,+ shared_session: Optional["ClientSession"] = None,) -> Optional[Union[LiteLLMAiohttpTransport, AsyncHTTPTransport]]:"""- Creates a transport for httpx.AsyncClient@@ -551,7 +551,7 @@ class AsyncHTTPHandler:return AsyncHTTPHandler._create_aiohttp_transport(ssl_context=ssl_context,ssl_verify=ssl_verify,- existing_session=existing_session,+ shared_session=shared_session,)#########################################################@@ -619,7 +619,7 @@ class AsyncHTTPHandler:def _create_aiohttp_transport(ssl_verify: Optional[bool] = None,ssl_context: Optional[ssl.SSLContext] = None,- existing_session: Optional["ClientSession"] = None,+ shared_session: Optional["ClientSession"] = None,) -> LiteLLMAiohttpTransport:"""Creates an AiohttpTransport with RequestNotRead error handling@@ -644,12 +644,12 @@ class AsyncHTTPHandler:verbose_logger.debug("Creating AiohttpTransport...")- # Use existing session if provided and valid- if existing_session is not None and not existing_session.closed:+ # Use shared session if provided and valid+ if shared_session is not None and not shared_session.closed:verbose_logger.debug(- f"SHARED SESSION: Reusing existing ClientSession (ID: {id(existing_session)})"+ f"SHARED SESSION: Reusing existing ClientSession (ID: {id(shared_session)})")- return LiteLLMAiohttpTransport(client=existing_session)+ return LiteLLMAiohttpTransport(client=shared_session)# Create new session only if none provided or existing one is invalidverbose_logger.debug(@@ -941,7 +941,7 @@ class HTTPHandler:def get_async_httpx_client(llm_provider: Union[LlmProviders, httpxSpecialProvider],params: Optional[dict] = None,- existing_session: Optional["ClientSession"] = None,+ shared_session: Optional["ClientSession"] = None,) -> AsyncHTTPHandler:"""Retrieves the async HTTP client from the cache@@ -963,12 +963,12 @@ def get_async_httpx_client(return _cached_clientif params is not None:- params["existing_session"] = existing_session+ params["shared_session"] = shared_session_new_client = AsyncHTTPHandler(**params)else:_new_client = AsyncHTTPHandler(timeout=httpx.Timeout(timeout=600.0, connect=5.0),- existing_session=existing_session,+ shared_session=shared_session,)litellm.in_memory_llm_clients_cache.set_cache(diff --git a/litellm/llms/custom_httpx/llm_http_handler.py b/litellm/llms/custom_httpx/llm_http_handler.pyindex 74d438db110..f8a92c2ac96 100644--- a/litellm/llms/custom_httpx/llm_http_handler.py+++ b/litellm/llms/custom_httpx/llm_http_handler.py@@ -246,7 +246,7 @@ class BaseLLMHTTPHandler:async_httpx_client = get_async_httpx_client(llm_provider=litellm.LlmProviders(custom_llm_provider),params={"ssl_verify": litellm_params.get("ssl_verify", None)},- existing_session=shared_session,+ shared_session=shared_session,)else:async_httpx_client = clientdiff --git a/litellm/llms/openai/common_utils.py b/litellm/llms/openai/common_utils.pyindex 0b2daa1446c..00197a67e95 100644--- a/litellm/llms/openai/common_utils.py+++ b/litellm/llms/openai/common_utils.py@@ -214,7 +214,7 @@ class BaseOpenAILLM:if isinstance(ssl_config, ssl.SSLContext)else None,ssl_verify=ssl_config if isinstance(ssl_config, bool) else None,- existing_session=shared_session,+ shared_session=shared_session,),follow_redirects=True,)diff --git a/tests/test_litellm/llms/custom_httpx/test_http_handler.py b/tests/test_litellm/llms/custom_httpx/test_http_handler.pyindex d33a3d0918b..0e31699fd83 100644--- a/tests/test_litellm/llms/custom_httpx/test_http_handler.py+++ b/tests/test_litellm/llms/custom_httpx/test_http_handler.py@@ -192,30 +192,30 @@ class MockClientSession:self.closed = False@pytest.mark.asyncio-async def test_create_aiohttp_transport_with_existing_session():- """Test that _create_aiohttp_transport reuses existing session when provided"""+async def test_create_aiohttp_transport_with_shared_session():+ """Test that _create_aiohttp_transport reuses shared session when provided"""from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler- # Create a mock existing session that's not callable+ # Create a mock shared session that's not callablemock_session = MockClientSession()- # Test with existing session+ # Test with shared sessiontransport = AsyncHTTPHandler._create_aiohttp_transport(- existing_session=mock_session # type: ignore+ shared_session=mock_session # type: ignore)- # Verify the transport uses the existing session directly+ # Verify the transport uses the shared session directlyassert transport.client is mock_sessionassert not callable(transport.client) # Should not be callable@pytest.mark.asyncio-async def test_create_aiohttp_transport_without_existing_session():+async def test_create_aiohttp_transport_without_shared_session():"""Test that _create_aiohttp_transport creates new session when none provided"""from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler- # Test without existing session- transport = AsyncHTTPHandler._create_aiohttp_transport(existing_session=None)+ # Test without shared session+ transport = AsyncHTTPHandler._create_aiohttp_transport(shared_session=None)# Verify the transport uses a lambda function (for backward compatibility)assert callable(transport.client) # Should be a lambda function@@ -223,7 +223,7 @@ async def test_create_aiohttp_transport_without_existing_session():@pytest.mark.asyncioasync def test_create_aiohttp_transport_with_closed_session():- """Test that _create_aiohttp_transport creates new session when existing session is closed"""+ """Test that _create_aiohttp_transport creates new session when shared session is closed"""from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler# Create a mock closed session@@ -232,7 +232,7 @@ async def test_create_aiohttp_transport_with_closed_session():# Test with closed sessiontransport = AsyncHTTPHandler._create_aiohttp_transport(- existing_session=mock_session # type: ignore+ shared_session=mock_session # type: ignore)# Verify the transport creates a new session (lambda function)@@ -240,15 +240,15 @@ async def test_create_aiohttp_transport_with_closed_session():@pytest.mark.asyncio-async def test_async_handler_with_existing_session():- """Test AsyncHTTPHandler initialization with existing session"""+async def test_async_handler_with_shared_session():+ """Test AsyncHTTPHandler initialization with shared session"""from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler- # Create a mock existing session+ # Create a mock shared sessionmock_session = MockClientSession()- # Create handler with existing session- handler = AsyncHTTPHandler(existing_session=mock_session) # type: ignore+ # Create handler with shared session+ handler = AsyncHTTPHandler(shared_session=mock_session) # type: ignore# Verify the handler was created successfullyassert handler is not None@@ -256,18 +256,18 @@ async def test_async_handler_with_existing_session():@pytest.mark.asyncio-async def test_get_async_httpx_client_with_existing_session():- """Test get_async_httpx_client with existing session"""+async def test_get_async_httpx_client_with_shared_session():+ """Test get_async_httpx_client with shared session"""from litellm.llms.custom_httpx.http_handler import get_async_httpx_clientfrom litellm.types.utils import LlmProviders- # Create a mock existing session+ # Create a mock shared sessionmock_session = MockClientSession()- # Test with existing session+ # Test with shared sessionclient = get_async_httpx_client(llm_provider=LlmProviders.ANTHROPIC,- existing_session=mock_session # type: ignore+ shared_session=mock_session # type: ignore)# Verify the client was created successfully@@ -276,15 +276,15 @@ async def test_get_async_httpx_client_with_existing_session():@pytest.mark.asyncio-async def test_get_async_httpx_client_without_existing_session():- """Test get_async_httpx_client without existing session (backward compatibility)"""+async def test_get_async_httpx_client_without_shared_session():+ """Test get_async_httpx_client without shared session (backward compatibility)"""from litellm.llms.custom_httpx.http_handler import get_async_httpx_clientfrom litellm.types.utils import LlmProviders- # Test without existing session+ # Test without shared sessionclient = get_async_httpx_client(llm_provider=LlmProviders.ANTHROPIC,- existing_session=None+ shared_session=None)# Verify the client was created successfully@@ -297,19 +297,19 @@ async def test_session_reuse_chain():"""Test that session is properly passed through the entire call chain"""from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler- # Create a mock existing session+ # Create a mock shared sessionmock_session = MockClientSession()# Test the entire chaintransport = AsyncHTTPHandler._create_async_transport(- existing_session=mock_session # type: ignore+ shared_session=mock_session # type: ignore)# Verify the transport was createdassert transport is not None# Test AsyncHTTPHandler creation- handler = AsyncHTTPHandler(existing_session=mock_session) # type: ignore+ handler = AsyncHTTPHandler(shared_session=mock_session) # type: ignoreassert handler is not None@@ -359,12 +359,12 @@ async def test_session_reuse_integration():# Create two clients with the same sessionclient1 = get_async_httpx_client(llm_provider=LlmProviders.ANTHROPIC,- existing_session=mock_session # type: ignore+ shared_session=mock_session # type: ignore)client2 = get_async_httpx_client(llm_provider=LlmProviders.OPENAI,- existing_session=mock_session # type: ignore+ shared_session=mock_session # type: ignore)# Both clients should be created successfully@@ -386,16 +386,16 @@ async def test_session_validation():from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler# Test with None session- transport1 = AsyncHTTPHandler._create_aiohttp_transport(existing_session=None)+ transport1 = AsyncHTTPHandler._create_aiohttp_transport(shared_session=None)assert callable(transport1.client) # Should create lambda# Test with closed sessionmock_closed_session = MockClientSession()mock_closed_session.closed = True- transport2 = AsyncHTTPHandler._create_aiohttp_transport(existing_session=mock_closed_session) # type: ignore+ transport2 = AsyncHTTPHandler._create_aiohttp_transport(shared_session=mock_closed_session) # type: ignoreassert callable(transport2.client) # Should create lambda# Test with valid sessionmock_valid_session = MockClientSession()- transport3 = AsyncHTTPHandler._create_aiohttp_transport(existing_session=mock_valid_session) # type: ignore+ transport3 = AsyncHTTPHandler._create_aiohttp_transport(shared_session=mock_valid_session) # type: ignoreassert transport3.client is mock_valid_session # Should reuse session