From ada17a3430e01a62eb362e47846fff2a0b7520f1 Mon Sep 17 00:00:00 2001 From: moe-berri Date: Sat, 12 Sep 2026 03:08:44 -0700 Subject: [PATCH] fix(memory): preserve client network context for internal calls --- litellm/proxy/memory/gateway.py | 6 ++++-- .../proxy/memory/test_memory_v2_boundaries.py | 8 +++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/litellm/proxy/memory/gateway.py b/litellm/proxy/memory/gateway.py index 221a0bc5b5f..4e788156cdf 100644 --- a/litellm/proxy/memory/gateway.py +++ b/litellm/proxy/memory/gateway.py @@ -192,13 +192,15 @@ async def prepare_gateway_memory( try: # Dispatch in process through the existing authenticated endpoint. No # network client, TLS context, or connection pool is created here. - async with httpx.ASGITransport(app=app) as transport: + async with httpx.ASGITransport( + app=app, client=request.client or ("127.0.0.1", 0), root_path=request.scope.get("root_path", "") + ) as transport: async def dispatch_round(body: Mapping[str, object]) -> httpx.Response: result: Final = await transport.handle_async_request( httpx.Request( "POST", - "http://litellm-memory" + request.url.path, + str(request.url), json=body, headers=headers, params=request.query_params, diff --git a/tests/test_litellm/proxy/memory/test_memory_v2_boundaries.py b/tests/test_litellm/proxy/memory/test_memory_v2_boundaries.py index 81e82656a91..e3d1793d11e 100644 --- a/tests/test_litellm/proxy/memory/test_memory_v2_boundaries.py +++ b/tests/test_litellm/proxy/memory/test_memory_v2_boundaries.py @@ -321,6 +321,9 @@ async def test_gateway_rounds_keep_separate_limiter_contexts_and_original_client @provider.post("/v1/messages") async def model(request: Request): + assert request.client is not None and request.client.host == "203.0.113.7" + assert request.url.scheme == "https" and request.headers["host"] == "gateway.example" + assert request.query_params["api-version"] == "test-version" body = await request.json() call_id = body["litellm_call_id"] stash = claim_request_stash_for_data(body) @@ -345,7 +348,10 @@ async def test_gateway_rounds_keep_separate_limiter_contexts_and_original_client { "type": "http", "path": "/v1/messages", - "query_string": b"", + "scheme": "https", + "server": ("gateway.example", 443), + "client": ("203.0.113.7", 41231), + "query_string": b"api-version=test-version", "headers": [(b"authorization", b"Bearer test"), (b"idempotency-key", b"visible-only")], } )