mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-08 22:21:35 +00:00
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:
parent
35de05dc8b
commit
8c58d165a7
1 changed files with 5 additions and 2 deletions
|
|
@ -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:
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue