mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-17 09:34:48 +00:00
fix(proxy): apply team model aliases before routing usage Ask AI
The access check accepts a team alias name, so routing must rewrite it the same way /chat/completions does. Extract the team alias resolution into a pure resolve_team_model_alias helper and apply it before key aliases.
This commit is contained in:
parent
b466afdf61
commit
83058b4067
3 changed files with 89 additions and 15 deletions
|
|
@ -3177,8 +3177,14 @@ def _update_model_if_team_alias_exists(
|
|||
data: dict,
|
||||
user_api_key_dict: UserAPIKeyAuth,
|
||||
) -> None:
|
||||
_model: Final = data.get("model")
|
||||
if isinstance(_model, str) and _model:
|
||||
data["model"] = resolve_team_model_alias(_model, user_api_key_dict)
|
||||
|
||||
|
||||
def resolve_team_model_alias(model: str, user_api_key_dict: UserAPIKeyAuth) -> str:
|
||||
"""
|
||||
Update the model if the team alias exists
|
||||
Return the model the team alias points to, or the requested model when no alias applies
|
||||
|
||||
If a alias map has been set on a team, then we want to make the request with the model the team alias is pointing to
|
||||
|
||||
|
|
@ -3199,15 +3205,14 @@ def _update_model_if_team_alias_exists(
|
|||
Keeping the requested model name lets it resolve against the deployments that still
|
||||
exist (e.g. a gateway-level model group shared with the team).
|
||||
"""
|
||||
_model: Final = data.get("model")
|
||||
if not _model or not user_api_key_dict.team_model_aliases or _model not in user_api_key_dict.team_model_aliases:
|
||||
return
|
||||
if not model or not user_api_key_dict.team_model_aliases or model not in user_api_key_dict.team_model_aliases:
|
||||
return model
|
||||
|
||||
from litellm.proxy.proxy_server import llm_router
|
||||
|
||||
# Skip alias rewrite if this model resolves to team-specific deployments
|
||||
# (team models use team_public_model_name, not model_aliases)
|
||||
aliased_target: Final = user_api_key_dict.team_model_aliases[_model]
|
||||
aliased_target: Final = user_api_key_dict.team_model_aliases[model]
|
||||
|
||||
# Optional bypass for stale aliases from pre-PR deployments:
|
||||
# only enabled via feature flag to preserve backwards compatibility.
|
||||
|
|
@ -3222,33 +3227,33 @@ def _update_model_if_team_alias_exists(
|
|||
if is_stale_team_alias and llm_router:
|
||||
if aliased_target not in llm_router.model_name_to_deployment_indices:
|
||||
_warn_stale_team_alias_once(
|
||||
f"deleted:{user_api_key_dict.team_id}:{_model}:{aliased_target}",
|
||||
f"deleted:{user_api_key_dict.team_id}:{model}:{aliased_target}",
|
||||
"Team model alias for model='%s', team_id='%s' targets '%s', which has no live "
|
||||
"deployment. Routing with the requested model name instead; remove the stale "
|
||||
"entry from the team's model_aliases to silence this warning.",
|
||||
_sanitize_for_log(_model),
|
||||
_sanitize_for_log(model),
|
||||
_sanitize_for_log(user_api_key_dict.team_id),
|
||||
_sanitize_for_log(aliased_target),
|
||||
)
|
||||
return
|
||||
return model
|
||||
# This is a stale alias from pre-PR deployments.
|
||||
# Check if current team deployments exist for the public name.
|
||||
key: Final = (user_api_key_dict.team_id, _model)
|
||||
key: Final = (user_api_key_dict.team_id, model)
|
||||
if key in llm_router.team_model_to_deployment_indices:
|
||||
if enable_stale_alias_bypass:
|
||||
# Team deployments exist; skip stale alias
|
||||
return
|
||||
return model
|
||||
_warn_stale_team_alias_once(
|
||||
f"{user_api_key_dict.team_id}:{_model}:{aliased_target}",
|
||||
f"{user_api_key_dict.team_id}:{model}:{aliased_target}",
|
||||
"Stale team model alias detected for model='%s', team_id='%s'. "
|
||||
"New sibling deployments may be unreachable. "
|
||||
"Set LITELLM_ENABLE_TEAM_STALE_ALIAS_BYPASS=true to enable "
|
||||
"team-scoped sibling routing.",
|
||||
_sanitize_for_log(_model),
|
||||
_sanitize_for_log(model),
|
||||
_sanitize_for_log(user_api_key_dict.team_id),
|
||||
)
|
||||
|
||||
data["model"] = aliased_target
|
||||
return aliased_target
|
||||
|
||||
|
||||
def _update_model_if_key_alias_exists(
|
||||
|
|
|
|||
|
|
@ -482,7 +482,10 @@ async def _acompletion(
|
|||
user_api_key_dict: "UserAPIKeyAuth | None" = None,
|
||||
) -> ModelResponse | CustomStreamWrapper:
|
||||
from litellm.proxy.auth.auth_checks import key_model_aliases_for_auth_check
|
||||
from litellm.proxy.litellm_pre_call_utils import sanitized_user_api_key_auth
|
||||
from litellm.proxy.litellm_pre_call_utils import (
|
||||
resolve_team_model_alias,
|
||||
sanitized_user_api_key_auth,
|
||||
)
|
||||
from litellm.proxy.proxy_server import llm_router
|
||||
from litellm.types.llms.openai import AllMessageValues
|
||||
|
||||
|
|
@ -490,10 +493,15 @@ async def _acompletion(
|
|||
list[AllMessageValues], messages
|
||||
)
|
||||
team_id: Final = user_api_key_dict.team_id if user_api_key_dict is not None else None
|
||||
team_aliased_model: Final = (
|
||||
resolve_team_model_alias(model, user_api_key_dict) if user_api_key_dict is not None else model
|
||||
)
|
||||
model_aliases: Final = (
|
||||
key_model_aliases_for_auth_check(user_api_key_dict) if user_api_key_dict is not None else None
|
||||
)
|
||||
resolved_model: Final = model_aliases.get(model, model) if model_aliases is not None else model
|
||||
resolved_model: Final = (
|
||||
model_aliases.get(team_aliased_model, team_aliased_model) if model_aliases is not None else team_aliased_model
|
||||
)
|
||||
router_metadata: Final = (
|
||||
{
|
||||
"user_api_key_team_id": team_id,
|
||||
|
|
|
|||
|
|
@ -1330,6 +1330,67 @@ class TestUsageAiChatTeamModelRouting:
|
|||
assert router.acompletion.await_args.kwargs["metadata"]["user_api_key_team_id"] is None
|
||||
mock_litellm_acompletion.assert_not_called()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_team_alias_routes_default_model_to_the_aliased_team_deployment(self):
|
||||
from litellm.proxy._types import UserAPIKeyAuth
|
||||
from litellm.proxy.management_endpoints.usage_endpoints.ai_usage_chat import (
|
||||
DEFAULT_COMPETITOR_DISCOVERY_MODEL,
|
||||
check_usage_ai_chat_model_access,
|
||||
)
|
||||
from litellm.router import Router
|
||||
|
||||
router: Final = Router(
|
||||
model_list=[
|
||||
{
|
||||
"model_name": DEFAULT_COMPETITOR_DISCOVERY_MODEL,
|
||||
"litellm_params": {
|
||||
"model": "openai/gpt-4o-mini",
|
||||
"api_key": "sk-admin",
|
||||
"mock_response": "from the admin deployment",
|
||||
},
|
||||
},
|
||||
{
|
||||
"model_name": "team-gpt",
|
||||
"litellm_params": {
|
||||
"model": "openai/gpt-4o-mini",
|
||||
"api_key": "sk-team",
|
||||
"mock_response": "from the team deployment",
|
||||
},
|
||||
},
|
||||
]
|
||||
)
|
||||
team_key: Final = UserAPIKeyAuth(
|
||||
api_key="sk-test",
|
||||
team_id="t1",
|
||||
team_models=["team-gpt"],
|
||||
team_model_aliases={DEFAULT_COMPETITOR_DISCOVERY_MODEL: "team-gpt"},
|
||||
)
|
||||
|
||||
with (
|
||||
patch( # test-quality-ok: Ask AI reads the proxy_server.llm_router module global; no injection seam
|
||||
"litellm.proxy.proxy_server.llm_router", router
|
||||
),
|
||||
patch( # test-quality-ok: the check reads the proxy_server.llm_model_list module global; no injection seam
|
||||
"litellm.proxy.proxy_server.llm_model_list", router.model_list
|
||||
),
|
||||
patch( # test-quality-ok: the check reads the proxy_server.prisma_client module global; no injection seam
|
||||
"litellm.proxy.proxy_server.prisma_client", None
|
||||
),
|
||||
):
|
||||
await check_usage_ai_chat_model_access(user_api_key_dict=team_key, model=None)
|
||||
events: Final = [
|
||||
json.loads(raw.removeprefix("data: "))
|
||||
async for raw in stream_usage_ai_chat(
|
||||
messages=[{"role": "user", "content": "hi"}],
|
||||
model=None,
|
||||
is_admin=True,
|
||||
user_api_key_dict=team_key,
|
||||
)
|
||||
]
|
||||
|
||||
assert {"type": "chunk", "content": "from the team deployment"} in events
|
||||
assert {"type": "chunk", "content": "from the admin deployment"} not in events
|
||||
|
||||
|
||||
class TestUsageAiChatEndpointAccessCheck:
|
||||
@pytest.mark.asyncio
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue