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(proxy): route azure container file requests by decoded deployment

Sameer Kankutef0ff848e3662
diff --git a/litellm/proxy/container_endpoints/handler_factory.py b/litellm/proxy/container_endpoints/handler_factory.py
index fae7f939aed..60c98ca0a46 100644
--- a/litellm/proxy/container_endpoints/handler_factory.py
+++ b/litellm/proxy/container_endpoints/handler_factory.py
@@ -22,6 +22,54 @@ from litellm.proxy.common_utils.openai_endpoint_utils import (
from litellm.responses.utils import ResponsesAPIRequestUtils
+def _decode_container_routing_metadata(
+ container_id: str,
+ custom_llm_provider: str,
+) -> tuple[str, str, Any]:
+ """
+ Decode managed container IDs into original ID + routing metadata.
+ """
+ decoded = ResponsesAPIRequestUtils._decode_container_id(container_id)
+ original_container_id = decoded.get("response_id", container_id)
+ decoded_provider = decoded.get("custom_llm_provider")
+ decoded_model_id = decoded.get("model_id")
+
+ if decoded_provider and custom_llm_provider == "openai":
+ custom_llm_provider = decoded_provider
+
+ return original_container_id, custom_llm_provider, decoded_model_id
+
+
+def _get_deployment_credentials_for_model_id(model_id: Any) -> Dict[str, Any]:
+ """
+ Fetch router deployment credentials for a decoded model_id when available.
+ """
+ if not model_id:
+ return {}
+
+ from litellm.proxy.proxy_server import llm_router
+
+ if llm_router is None:
+ return {}
+
+ deployment_creds = llm_router.get_deployment_credentials(model_id=model_id)
+ if deployment_creds:
+ return deployment_creds
+
+ # Fallback: some ids may map via model group name instead of explicit model id.
+ deployment = llm_router.get_deployment_by_model_group_name(
+ model_group_name=str(model_id)
+ )
+ if deployment and deployment.litellm_params:
+ return deployment.litellm_params.model_dump(exclude_none=True)
+
+ # Final fallback: lookup helper that supports provider-aware resolution.
+ deployment_creds = llm_router.get_deployment_credentials_with_provider(
+ model_id=str(model_id)
+ )
+ return deployment_creds or {}
+
+
def _load_endpoints_config() -> Dict:
"""Load the endpoints configuration from JSON file."""
config_path = Path(__file__).parent.parent.parent / "containers" / "endpoints.json"
@@ -176,14 +224,28 @@ async def _process_binary_request(
# Build litellm_params - credentials are resolved by provider config from env
litellm_params = GenericLiteLLMParams()
- # Decode container ID and extract provider info
- decoded = ResponsesAPIRequestUtils._decode_container_id(container_id)
- original_container_id = decoded.get("response_id", container_id)
+ (
+ original_container_id,
+ custom_llm_provider,
+ decoded_model_id,
+ ) = _decode_container_routing_metadata(
+ container_id=container_id,
+ custom_llm_provider=custom_llm_provider,
+ )
- # If container ID has encoded provider info and user didn't explicitly set provider, use it
- decoded_provider = decoded.get("custom_llm_provider")
- if decoded_provider and custom_llm_provider == "openai":
- custom_llm_provider = decoded_provider
+ if decoded_model_id and not litellm_params.get("model_id"):
+ litellm_params["model_id"] = decoded_model_id
+
+ # Resolve provider credentials for routed deployment, e.g. Azure multi-region api_base.
+ deployment_creds = _get_deployment_credentials_for_model_id(
+ model_id=decoded_model_id
+ )
+ if deployment_creds.get("api_base"):
+ litellm_params["api_base"] = deployment_creds["api_base"]
+ if deployment_creds.get("api_key"):
+ litellm_params["api_key"] = deployment_creds["api_key"]
+ if deployment_creds.get("api_version"):
+ litellm_params["api_version"] = deployment_creds["api_version"]
# Get the provider config
container_provider_config = _get_container_provider_config(custom_llm_provider)
@@ -284,17 +346,19 @@ async def _process_multipart_upload_request(
or "openai"
)
- # Decode container ID and extract provider info
- decoded = ResponsesAPIRequestUtils._decode_container_id(container_id)
- original_container_id = decoded.get("response_id", container_id)
-
- # If container ID has encoded provider info and user didn't explicitly set provider, use it
- decoded_provider = decoded.get("custom_llm_provider")
- if decoded_provider and custom_llm_provider == "openai":
- custom_llm_provider = decoded_provider
+ (
+ original_container_id,
+ custom_llm_provider,
+ decoded_model_id,
+ ) = _decode_container_routing_metadata(
+ container_id=container_id,
+ custom_llm_provider=custom_llm_provider,
+ )
data["container_id"] = original_container_id # Use decoded original ID
data["custom_llm_provider"] = custom_llm_provider
+ if decoded_model_id:
+ data["model_id"] = decoded_model_id
processor = ProxyBaseLLMRequestProcessing(data=data)
try:
@@ -361,18 +425,19 @@ async def _process_request(
# Decode container_id if present in path_params
if "container_id" in path_params:
- decoded = ResponsesAPIRequestUtils._decode_container_id(
- path_params["container_id"]
+ (
+ original_container_id,
+ custom_llm_provider,
+ decoded_model_id,
+ ) = _decode_container_routing_metadata(
+ container_id=path_params["container_id"],
+ custom_llm_provider=custom_llm_provider,
)
- original_container_id = decoded.get("response_id", path_params["container_id"])
-
- # If container ID has encoded provider info and user didn't explicitly set provider, use it
- decoded_provider = decoded.get("custom_llm_provider")
- if decoded_provider and custom_llm_provider == "openai":
- custom_llm_provider = decoded_provider
# Update path_params with decoded original ID
data["container_id"] = original_container_id
+ if decoded_model_id:
+ data["model_id"] = decoded_model_id
data["custom_llm_provider"] = custom_llm_provider
diff --git a/tests/test_litellm/containers/test_azure_container_transformation.py b/tests/test_litellm/containers/test_azure_container_transformation.py
index a46046b318b..4f4006c1584 100644
--- a/tests/test_litellm/containers/test_azure_container_transformation.py
+++ b/tests/test_litellm/containers/test_azure_container_transformation.py
@@ -11,6 +11,7 @@ sys.path.insert(0, os.path.abspath("../../../"))
import litellm
from litellm.llms.azure.containers.transformation import AzureContainerConfig
from litellm.llms.base_llm.containers.transformation import BaseContainerConfig
+from litellm.responses.utils import ResponsesAPIRequestUtils
from litellm.types.containers.main import (
ContainerFileListResponse,
ContainerListResponse,
@@ -518,3 +519,119 @@ class TestAzureContainerKnownFailureRegressions:
c2 = _get_container_provider_config("azure_text")
assert type(c1) is type(c2)
assert isinstance(c1, AzureContainerConfig)
+
+ def test_regression_decode_container_routing_metadata_includes_model_id(self):
+ from litellm.proxy.container_endpoints.handler_factory import (
+ _decode_container_routing_metadata,
+ )
+
+ encoded_id = ResponsesAPIRequestUtils._build_container_id(
+ custom_llm_provider="azure",
+ model_id="model_abc123",
+ container_id="cntr_123",
+ )
+ original_id, provider, model_id = _decode_container_routing_metadata(
+ container_id=encoded_id,
+ custom_llm_provider="openai",
+ )
+
+ assert original_id == "cntr_123"
+ assert provider == "azure"
+ assert model_id == "model_abc123"
+
+ @pytest.mark.asyncio
+ async def test_regression_binary_file_request_uses_deployment_api_base(
+ self, monkeypatch
+ ):
+ from starlette.requests import Request
+
+ from litellm.proxy.container_endpoints import handler_factory
+ from litellm.types.router import GenericLiteLLMParams
+
+ encoded_id = ResponsesAPIRequestUtils._build_container_id(
+ custom_llm_provider="azure",
+ model_id="model_abc123",
+ container_id="cntr_123",
+ )
+
+ class _FakeContainerProviderConfig:
+ def validate_environment(self, headers, api_key=None):
+ return headers
+
+ def get_complete_url(self, api_base, litellm_params):
+ return f"{api_base}/openai/v1/containers?api-version=v1"
+
+ def transform_container_file_content_request(
+ self, container_id, file_id, api_base, litellm_params, headers
+ ):
+ return (
+ f"{api_base}/{container_id}/files/{file_id}/content",
+ {},
+ )
+
+ def transform_container_file_content_response(
+ self, raw_response, logging_obj
+ ):
+ return raw_response
+
+ monkeypatch.setattr(
+ handler_factory,
+ "_get_container_provider_config",
+ lambda *_: _FakeContainerProviderConfig(),
+ )
+ monkeypatch.setattr(
+ handler_factory,
+ "_get_deployment_credentials_for_model_id",
+ lambda model_id: {
+ "api_base": "https://swedencentral.openai.azure.com",
+ "api_key": "test-key",
+ "api_version": "v1",
+ },
+ )
+
+ async def _mock_async_content_handler(
+ self,
+ container_id,
+ file_id,
+ container_provider_config,
+ litellm_params: GenericLiteLLMParams,
+ logging_obj,
+ ):
+ assert container_id == "cntr_123"
+ assert file_id == "cfile_abc"
+ assert litellm_params.get("model_id") == "model_abc123"
+ assert (
+ litellm_params.get("api_base")
+ == "https://swedencentral.openai.azure.com"
+ )
+ assert litellm_params.get("api_key") == "test-key"
+ assert litellm_params.get("api_version") == "v1"
+ return b"csv-bytes"
+
+ from litellm.llms.custom_httpx.llm_http_handler import BaseLLMHTTPHandler
+
+ monkeypatch.setattr(
+ BaseLLMHTTPHandler,
+ "async_container_file_content_handler",
+ _mock_async_content_handler,
+ )
+
+ request = Request(
+ {
+ "type": "http",
+ "method": "GET",
+ "path": "/v1/containers/id/files/id/content",
+ "headers": [],
+ "query_string": b"",
+ }
+ )
+
+ response = await handler_factory._process_binary_request(
+ request=request,
+ container_id=encoded_id,
+ file_id="cfile_abc",
+ user_api_key_dict=MagicMock(),
+ )
+
+ assert response.status_code == 200
+ assert response.body == b"csv-bytes"