From dce51ce3cb27226809551419f8cbce0a348f0401 Mon Sep 17 00:00:00 2001 From: HardMax71 Date: Sun, 25 Oct 2026 14:26:33 +0100 Subject: [PATCH] fix(http_handler): keep an explicit transport when litellm.aclient_session is set get_async_asgi_client and the ROI calculator pass their own transport (in-process ASGI dispatch, pinned egress). Handing those handlers the global session replaced the transport, so their requests went out over the session. The session now applies only when no transport is given, and client= is passed outside the untyped params dict so the type gate count stays at the merge base. --- litellm/llms/custom_httpx/http_handler.py | 7 +++--- .../llms/custom_httpx/test_http_handler.py | 25 +++++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/litellm/llms/custom_httpx/http_handler.py b/litellm/llms/custom_httpx/http_handler.py index 29b774a7853..eea07aa1534 100644 --- a/litellm/llms/custom_httpx/http_handler.py +++ b/litellm/llms/custom_httpx/http_handler.py @@ -1767,8 +1767,8 @@ def get_async_httpx_client( except Exception: pass - # A caller-supplied session is part of the key, so swapping it never serves a stale handler. - session: Final = litellm.aclient_session + # An explicit transport (in-process ASGI, pinned egress) wins over the session; the key holds the session so a swap never serves a stale handler. + session: Final = litellm.aclient_session if params is None or "transport" not in params else None _session_key_name: Final = f"_session{id(session)}" if session is not None else "" _cache_key_name: Final = "async_httpx_client" + _params_key_name + llm_provider + _session_key_name @@ -1789,8 +1789,7 @@ def get_async_httpx_client( # Filter out params that are only used for cache key, not for AsyncHTTPHandler.__init__ handler_params: Final = {k: v for k, v in params.items() if k != "disable_aiohttp_transport"} handler_params["shared_session"] = shared_session - handler_params["client"] = session - _new_client = AsyncHTTPHandler(**handler_params) + _new_client = AsyncHTTPHandler(**handler_params, client=session) else: _new_client = AsyncHTTPHandler( timeout=_default_cached_client_timeout(), diff --git a/tests/unit/llms/custom_httpx/test_http_handler.py b/tests/unit/llms/custom_httpx/test_http_handler.py index 5c28cc0630e..dc7bf563514 100644 --- a/tests/unit/llms/custom_httpx/test_http_handler.py +++ b/tests/unit/llms/custom_httpx/test_http_handler.py @@ -2243,6 +2243,31 @@ async def test_swapping_litellm_aclient_session_gets_a_fresh_handler(monkeypatch await session.aclose() +@pytest.mark.asyncio +async def test_an_explicit_transport_wins_over_litellm_aclient_session(monkeypatch): + """A caller that passes its own transport, like the in-process ASGI client, keeps it + when a session is configured, so its requests never leave through the session.""" + from litellm.llms.custom_httpx.http_handler import get_async_httpx_client + from litellm.types.utils import LlmProviders + + session_seen: list[str] = [] + transport_seen: list[str] = [] + session = httpx.AsyncClient(transport=_recording_transport(session_seen)) + monkeypatch.setattr(litellm, "aclient_session", session) + monkeypatch.setattr(litellm, "in_memory_llm_clients_cache", LLMClientCache()) + + handler = get_async_httpx_client( + llm_provider=LlmProviders.ANTHROPIC, params={"transport": _recording_transport(transport_seen)} + ) + await handler.post("https://example.invalid/v1/messages", json={}) + + assert handler.client is not session + assert transport_seen == ["https://example.invalid/v1/messages"] + assert session_seen == [] + await handler.close() + await session.aclose() + + @pytest.mark.asyncio async def test_acompletion_anthropic_goes_through_litellm_aclient_session(monkeypatch): """Providers on the HTTP handler path (here Anthropic) send through the configured session,