perf(router): use shallow copy instead of deepcopy for model aliases

Replace copy.deepcopy() with dict.copy() in _get_all_deployments when
creating model aliases.

Safe because:
1. Only modifies top-level 'model_name' field (isolated by shallow copy)
2. Nested dicts (litellm_params, model_info) are never modified after return
3. When model_alias=None, returns original dict with NO copy, proving
   callers expect nested structures to be read-only
4. Key insight: Code that needs to modify nested structures does deepcopy FIRST.
   This proves the contract is: \"treat returned deployments as read-only for
   nested fields.\" (see line 6894: copy.deepcopy before modifying litellm_params)

Performance: ~10-100x faster than deepcopy on nested dict structures.
Tests: All 114 router unit tests pass.
This commit is contained in:
AlexsanderHamir 2025-10-15 16:25:37 -07:00
parent 35de05dc8b
commit 8c58d165a7

View file

@ -6281,7 +6281,9 @@ class Router:
model_name=model_name, model=model, team_id=team_id
):
if model_alias is not None:
alias_model = copy.deepcopy(model)
# Optimized: Use shallow copy since we only modify top-level model_name
# This is much faster than deepcopy for nested dict structures
alias_model = model.copy()
alias_model["model_name"] = model_alias
returned_models.append(alias_model)
else:
@ -6295,7 +6297,8 @@ class Router:
model_name=model_name, model=model, team_id=team_id
):
if model_alias is not None:
alias_model = copy.deepcopy(model)
# Optimized: Use shallow copy since we only modify top-level model_name
alias_model = model.copy()
alias_model["model_name"] = model_alias
returned_models.append(alias_model)
else: