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.
This commit is contained in:
HardMax71 2026-10-25 14:26:33 +01:00
parent 69279b1f13
commit dce51ce3cb
2 changed files with 28 additions and 4 deletions

View file

@ -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(),

View file

@ -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,