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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(router): always unwrap managed container_id even when model_id is empty
Claude3e7338144927
diff --git a/litellm/router.py b/litellm/router.pyindex a11281bb151..7449492e89f 100644--- a/litellm/router.py+++ b/litellm/router.py@@ -5275,9 +5275,11 @@ class Router:container_id = kwargs.get("container_id")if isinstance(container_id, str):decoded = ResponsesAPIRequestUtils._decode_container_id(container_id)+ original_id = decoded.get("response_id", container_id)+ if original_id != container_id:+ kwargs["container_id"] = original_idmodel_id = decoded.get("model_id")if model_id:- kwargs["container_id"] = decoded.get("response_id", container_id)kwargs["model"] = model_iddecoded_provider = decoded.get("custom_llm_provider")if decoded_provider and kwargs.get("custom_llm_provider") == "openai":diff --git a/tests/router_unit_tests/test_router_endpoints.py b/tests/router_unit_tests/test_router_endpoints.pyindex 93bb84dfe35..19d7155e4aa 100644--- a/tests/router_unit_tests/test_router_endpoints.py+++ b/tests/router_unit_tests/test_router_endpoints.py@@ -1170,3 +1170,36 @@ async def test_init_containers_api_endpoints_managed_id_routes_via_generic_fallbassert call_kw["container_id"] == "cfile_upstream_abc"assert call_kw["file_id"] == "cfile_xyz"assert call_kw["custom_llm_provider"] == "azure"+++async def test_init_containers_api_endpoints_managed_id_without_model_id_unwraps():+ """+ Managed ``cntr_`` IDs may be encoded with an empty ``model_id`` (e.g. when a+ streaming response had no router metadata). The router must still unwrap the+ managed ID before calling the upstream provider — otherwise the raw+ ``cntr_...`` token leaks downstream and the provider rejects it.+ """+ from litellm.responses.utils import ResponsesAPIRequestUtils++ router = Router(model_list=[])+ mock_original_function = AsyncMock(return_value={"ok": True})++ managed_id = ResponsesAPIRequestUtils._build_container_id(+ custom_llm_provider="openai",+ model_id=None,+ container_id="cfile_upstream_abc",+ )++ await router._init_containers_api_endpoints(+ original_function=mock_original_function,+ custom_llm_provider="openai",+ container_id=managed_id,+ file_id="cfile_xyz",+ )++ mock_original_function.assert_called_once()+ call_kw = mock_original_function.call_args.kwargs+ assert call_kw["container_id"] == "cfile_upstream_abc"+ assert call_kw["file_id"] == "cfile_xyz"+ assert call_kw["custom_llm_provider"] == "openai"