Skip to content

Commit 48fa547

Browse files
committed
fix: router model alias
1 parent 3c7210b commit 48fa547

File tree

1 file changed

+56
-9
lines changed

1 file changed

+56
-9
lines changed

litellm/router.py

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5419,9 +5419,11 @@ def _get_all_deployments(
54195419
for model in self.model_list:
54205420
if model_name is not None and model["model_name"] == model_name:
54215421
if model_alias is not None:
5422-
alias_model = copy.deepcopy(model)
5423-
alias_model["model_name"] = model_alias
5424-
returned_models.append(alias_model)
5422+
returned_models.append(
5423+
self._return_deep_copied_deployment_with_model_alias(
5424+
model_alias=model_alias, model=model
5425+
)
5426+
)
54255427
else:
54265428
returned_models.append(model)
54275429

@@ -5512,8 +5514,40 @@ def get_model_list_from_model_alias(
55125514
)
55135515
)
55145516

5517+
######################################
5518+
# get wildcard models for this specific model_alias value
5519+
# eg. if a user points special-alias -> xai/grok-3, where the router has a wildcard route for xai/*
5520+
# then ensure xai/* gets added as a deployment for this alias
5521+
model_value_str: Optional[str] = None
5522+
if isinstance(model_value, str):
5523+
model_value_str = model_value
5524+
elif hasattr(model_value, "model"):
5525+
model_value_str = model_value.get("model")
5526+
wildcard_models = self.get_wildcard_deployments_for_model_name(
5527+
model_name=model_value_str
5528+
)
5529+
5530+
# for we returned wildcard
5531+
if model_alias is not None and len(wildcard_models) > 0:
5532+
for model in wildcard_models:
5533+
returned_models.append(
5534+
self._return_deep_copied_deployment_with_model_alias(
5535+
model_alias=model_alias, model=model
5536+
)
5537+
)
5538+
######################################
55155539
return returned_models
55165540

5541+
def _return_deep_copied_deployment_with_model_alias(
5542+
self, model_alias: str, model: DeploymentTypedDict
5543+
):
5544+
"""
5545+
Creates a deep copy of a specific DeploymentTypedDict with the model_name set to the model_alias
5546+
"""
5547+
alias_model = copy.deepcopy(model)
5548+
alias_model["model_name"] = model_alias
5549+
return alias_model
5550+
55175551
def get_model_list(
55185552
self, model_name: Optional[str] = None
55195553
) -> Optional[List[DeploymentTypedDict]]:
@@ -5532,12 +5566,9 @@ def get_model_list(
55325566
)
55335567

55345568
if len(returned_models) == 0: # check if wildcard route
5535-
potential_wildcard_models = self.pattern_router.route(model_name)
5536-
if model_name is not None and potential_wildcard_models is not None:
5537-
for m in potential_wildcard_models:
5538-
deployment_typed_dict = DeploymentTypedDict(**m) # type: ignore
5539-
deployment_typed_dict["model_name"] = model_name
5540-
returned_models.append(deployment_typed_dict)
5569+
returned_models.extend(
5570+
self.get_wildcard_deployments_for_model_name(model_name=model_name)
5571+
)
55415572

55425573
if model_name is None:
55435574
returned_models += self.model_list
@@ -5547,6 +5578,21 @@ def get_model_list(
55475578
return returned_models
55485579
return None
55495580

5581+
def get_wildcard_deployments_for_model_name(
5582+
self, model_name: Optional[str] = None
5583+
) -> List[DeploymentTypedDict]:
5584+
"""
5585+
Gets all wildcard deployments that match the specific model_name
5586+
"""
5587+
returned_models: List[DeploymentTypedDict] = []
5588+
potential_wildcard_models = self.pattern_router.route(model_name)
5589+
if model_name is not None and potential_wildcard_models is not None:
5590+
for m in potential_wildcard_models:
5591+
deployment_typed_dict = DeploymentTypedDict(**m) # type: ignore
5592+
deployment_typed_dict["model_name"] = model_name
5593+
returned_models.append(deployment_typed_dict)
5594+
return returned_models
5595+
55505596
def get_model_access_groups(
55515597
self, model_name: Optional[str] = None, model_access_group: Optional[str] = None
55525598
) -> Dict[str, List[str]]:
@@ -5619,6 +5665,7 @@ def get_settings(self):
56195665
"fallbacks",
56205666
"context_window_fallbacks",
56215667
"model_group_retry_policy",
5668+
"model_group_alias",
56225669
]
56235670

56245671
for var in vars_to_include:

0 commit comments

Comments
 (0)