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(router): forward target_model_names on file uploads to litellm_proxy deployments

Shivam Rawata14b2ab960d0
diff --git a/litellm/files/main.py b/litellm/files/main.py
index 34421d13761..cf8826895aa 100644
--- a/litellm/files/main.py
+++ b/litellm/files/main.py
@@ -24,12 +24,15 @@ FileCreateProvider = Literal[
"vertex_ai",
"bedrock",
"hosted_vllm",
+ "litellm_proxy",
"manus",
"anthropic",
]
-FileRetrieveProvider = Literal["openai", "azure", "gemini", "vertex_ai", "hosted_vllm", "manus", "anthropic"]
-FileDeleteProvider = Literal["openai", "azure", "gemini", "manus", "anthropic"]
-FileListProvider = Literal["openai", "azure", "manus", "anthropic"]
+FileRetrieveProvider = Literal[
+ "openai", "azure", "gemini", "vertex_ai", "hosted_vllm", "litellm_proxy", "manus", "anthropic"
+]
+FileDeleteProvider = Literal["openai", "azure", "gemini", "litellm_proxy", "manus", "anthropic"]
+FileListProvider = Literal["openai", "azure", "litellm_proxy", "manus", "anthropic"]
import litellm
from litellm import get_secret_str
from litellm.files.streaming import FileContentStreamingResponse
diff --git a/litellm/router.py b/litellm/router.py
index 39d5080a33d..b35506554d1 100644
--- a/litellm/router.py
+++ b/litellm/router.py
@@ -21,6 +21,7 @@ import traceback
from collections import defaultdict
from collections.abc import AsyncGenerator, Callable, Generator, Mapping, Sequence
from functools import lru_cache
+from types import MappingProxyType
from typing import TYPE_CHECKING, Any, Final, Literal, Optional, TypeVar, Union, cast
import anyio
@@ -192,6 +193,7 @@ from litellm.types.utils import (
CustomPricingLiteLLMParams,
GenericBudgetConfigType,
LiteLLMBatch,
+ LlmProviders,
ModelInfo,
ModelResponseStream,
StandardLoggingPayload,
@@ -4919,6 +4921,13 @@ class Router:
)
kwargs_copy["file"] = file
+ if custom_llm_provider == LlmProviders.LITELLM_PROXY.value:
+ kwargs_copy["extra_body"] = MappingProxyType(
+ {
+ **(kwargs_copy.get("extra_body") or MappingProxyType({})),
+ "target_model_names": stripped_model,
+ }
+ )
if (
"gcs_bucket_name" in data
): # TODO: Remove this once we have a better way to handle GCS bucket name: Problem is that we need to pass the gcs_bucket_name to the router for the create_file call but it doesn't show up there
diff --git a/litellm/types/utils.py b/litellm/types/utils.py
index 4f824262867..20225d7dc49 100644
--- a/litellm/types/utils.py
+++ b/litellm/types/utils.py
@@ -3706,6 +3706,7 @@ LlmProvidersSet: Final = {provider.value for provider in LlmProviders}
OPENAI_COMPATIBLE_BATCH_AND_FILES_PROVIDERS: set[str] = {
LlmProviders.OPENAI.value,
LlmProviders.HOSTED_VLLM.value,
+ LlmProviders.LITELLM_PROXY.value,
}
ListBatchesSupportedProvider = Literal["openai", "azure", "hosted_vllm", "vertex_ai"]
diff --git a/tests/test_litellm/test_router.py b/tests/test_litellm/test_router.py
index 8f35597768f..92085a009af 100644
--- a/tests/test_litellm/test_router.py
+++ b/tests/test_litellm/test_router.py
@@ -522,6 +522,141 @@ async def test_async_router_acreate_file_uses_deployment_custom_llm_provider():
assert mock_acreate_file.call_args.kwargs["custom_llm_provider"] == "azure"
+async def test_async_router_acreate_file_forwards_target_model_names_to_litellm_proxy():
+ """
+ A deployment pointing at a second LiteLLM proxy (litellm_proxy provider) must forward
+ target_model_names downstream so the second proxy can route the upload to the right
+ deployment. Regression test for https://github.com/BerriAI/litellm/issues/36176
+ """
+ import json
+ from io import BytesIO
+ from unittest.mock import MagicMock, patch
+
+ jsonl_file = BytesIO(
+ json.dumps({"body": {"model": "chained-batch", "messages": [{"role": "user", "content": "hi"}]}}).encode(
+ "utf-8"
+ )
+ )
+ jsonl_file.name = "test.jsonl"
+
+ router = litellm.Router(
+ model_list=[
+ {
+ "model_name": "chained-batch",
+ "litellm_params": {
+ "model": "litellm_proxy/gpt-4.1-batch",
+ "api_base": "http://localhost:4001/v1",
+ "api_key": "sk-proxy-b",
+ },
+ },
+ ],
+ )
+
+ with patch("litellm.acreate_file", return_value=MagicMock()) as mock_acreate_file:
+ await router.acreate_file(
+ model="chained-batch",
+ purpose="batch",
+ file=jsonl_file,
+ )
+
+ assert mock_acreate_file.call_count == 1
+ call_kwargs = mock_acreate_file.call_args.kwargs
+ assert call_kwargs["custom_llm_provider"] == "litellm_proxy"
+ assert call_kwargs["extra_body"] == {"target_model_names": "gpt-4.1-batch"}
+ uploaded_line = json.loads(call_kwargs["file"].read().decode("utf-8").split("\n")[0])
+ assert uploaded_line["body"]["model"] == "gpt-4.1-batch"
+
+
+async def test_async_router_acreate_file_does_not_inject_target_model_names_for_other_providers():
+ """
+ target_model_names is a LiteLLM proxy routing hint; it must not leak into uploads
+ sent to non-litellm_proxy providers.
+ """
+ from unittest.mock import MagicMock, patch
+
+ router = litellm.Router(
+ model_list=[
+ {
+ "model_name": "gpt-4.1-batch",
+ "litellm_params": {"model": "gpt-4.1"},
+ },
+ ],
+ )
+
+ with patch("litellm.acreate_file", return_value=MagicMock()) as mock_acreate_file:
+ await router.acreate_file(
+ model="gpt-4.1-batch",
+ purpose="batch",
+ file=MagicMock(),
+ )
+
+ assert mock_acreate_file.call_count == 1
+ assert mock_acreate_file.call_args.kwargs.get("extra_body") is None
+
+
+async def test_async_router_acreate_file_litellm_proxy_sends_target_model_names_in_multipart_form():
+ """
+ End-to-end through litellm.acreate_file and the OpenAI SDK: the multipart form that
+ reaches the second proxy must carry target_model_names as a form field, since the
+ downstream /v1/files endpoint reads it via Form(). Would raise BadRequestError
+ (unsupported provider) before litellm_proxy was supported for files.
+ """
+ import json
+ from io import BytesIO
+
+ import httpx
+ import respx
+
+ jsonl_file = BytesIO(
+ json.dumps({"body": {"model": "chained-batch", "messages": [{"role": "user", "content": "hi"}]}}).encode(
+ "utf-8"
+ )
+ )
+ jsonl_file.name = "test.jsonl"
+
+ router = litellm.Router(
+ model_list=[
+ {
+ "model_name": "chained-batch",
+ "litellm_params": {
+ "model": "litellm_proxy/gpt-4.1-batch",
+ "api_base": "http://localhost:4001/v1",
+ "api_key": "sk-proxy-b",
+ },
+ },
+ ],
+ )
+
+ file_object_json = {
+ "id": "file-abc123",
+ "object": "file",
+ "bytes": 100,
+ "created_at": 1700000000,
+ "filename": "test.jsonl",
+ "purpose": "batch",
+ "status": "processed",
+ }
+
+ with respx.mock(assert_all_called=True) as respx_mock:
+ create_route = respx_mock.post("http://localhost:4001/v1/files").mock(
+ return_value=httpx.Response(200, json=file_object_json)
+ )
+ response = await router.acreate_file(
+ model="chained-batch",
+ purpose="batch",
+ file=jsonl_file,
+ )
+
+ assert response.id == "file-abc123"
+ request_body = create_route.calls.last.request.content
+ assert b'name="target_model_names"' in request_body
+ assert b"gpt-4.1-batch" in request_body
+ assert b'name="purpose"' in request_body
+
+
@pytest.mark.asyncio
async def test_async_router_afile_content_uses_deployment_custom_llm_provider():
"""