fix(router): apply decoded provider for managed container IDs without model_id

A managed cntr_ ID can encode a non-OpenAI provider (e.g. azure) with an
empty model_id when streaming events have no router model_info.id. The
provider override was nested inside 'if model_id:', so such IDs unwrapped
the container_id but kept custom_llm_provider='openai', routing the
request to the wrong upstream. Hoist the override out of the model_id
guard.
This commit is contained in:
mateo-berri 2026-04-30 19:51:02 +00:00 committed by Cursor Agent
parent 3e73381449
commit 46ba48e69a
No known key found for this signature in database
2 changed files with 36 additions and 3 deletions

View File

@ -5278,12 +5278,12 @@ class Router:
original_id = decoded.get("response_id", container_id)
if original_id != container_id:
kwargs["container_id"] = original_id
decoded_provider = decoded.get("custom_llm_provider")
if decoded_provider and kwargs.get("custom_llm_provider") == "openai":
kwargs["custom_llm_provider"] = decoded_provider
model_id = decoded.get("model_id")
if model_id:
kwargs["model"] = model_id
decoded_provider = decoded.get("custom_llm_provider")
if decoded_provider and kwargs.get("custom_llm_provider") == "openai":
kwargs["custom_llm_provider"] = decoded_provider
return await self._ageneric_api_call_with_fallbacks(
original_function=original_function,
**kwargs,

View File

@ -1203,3 +1203,36 @@ async def test_init_containers_api_endpoints_managed_id_without_model_id_unwraps
assert call_kw["container_id"] == "cfile_upstream_abc"
assert call_kw["file_id"] == "cfile_xyz"
assert call_kw["custom_llm_provider"] == "openai"
@pytest.mark.asyncio
async def test_init_containers_api_endpoints_managed_id_without_model_id_applies_decoded_provider():
"""
A managed ``cntr_`` ID can encode a non-OpenAI provider (e.g. ``azure``) with
an empty ``model_id`` (streaming events without router ``model_info.id``).
The router must still apply the decoded provider so the request routes to
the correct upstream not stay on the default ``openai``.
"""
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="azure",
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"] == "azure"