From 6500503ea53b46712a6cc3c7326499b993749dd5 Mon Sep 17 00:00:00 2001 From: Ryan Crabbe Date: Fri, 6 Feb 2026 09:58:51 -0800 Subject: [PATCH 1/2] perf: reuse LiteLLM_Params in get_router_model_info to avoid redundant construction Pass Deployment object directly instead of converting to dict with .model_dump(), then reuse the existing LiteLLM_Params instance via isinstance check. This eliminates redundant Pydantic model construction in the deployment callback path. ~8% improvement in deployment_callback_on_success total time. --- litellm/router.py | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/litellm/router.py b/litellm/router.py index 6dc3278225..e35d6a2c2f 100644 --- a/litellm/router.py +++ b/litellm/router.py @@ -5305,7 +5305,7 @@ class Router: return else: deployment_model_info = self.get_router_model_info( - deployment=deployment_info.model_dump(), + deployment=deployment_info, received_model_name=model_group, ) # get tpm/rpm from deployment info @@ -6557,7 +6557,7 @@ class Router: @overload def get_router_model_info( - self, deployment: dict, received_model_name: str, id: None = None + self, deployment: Union[dict, "Deployment"], received_model_name: str, id: None = None ) -> ModelMapInfo: pass @@ -6569,7 +6569,7 @@ class Router: def get_router_model_info( self, - deployment: Optional[dict], + deployment: Optional[Union[dict, "Deployment"]], received_model_name: str, id: Optional[str] = None, ) -> ModelMapInfo: @@ -6589,7 +6589,7 @@ class Router: if id is not None: _deployment = self.get_deployment(model_id=id) if _deployment is not None: - deployment = _deployment.model_dump(exclude_none=True) + deployment = _deployment if deployment is None: raise ValueError("Deployment not found") @@ -6601,10 +6601,22 @@ class Router: model = base_model - ## GET PROVIDER + ## GET PROVIDER - reuse LiteLLM_Params if already constructed + litellm_params_data = deployment.get("litellm_params") + litellm_params: LiteLLM_Params + if isinstance(litellm_params_data, LiteLLM_Params): + litellm_params = litellm_params_data + elif isinstance(litellm_params_data, dict) and "model" in litellm_params_data: + litellm_params = LiteLLM_Params(**litellm_params_data) + else: + raise ValueError( + f"Deployment missing valid litellm_params. " + f"Got: {type(litellm_params_data).__name__}, " + f"deployment_id: {deployment.get('model_info', {}).get('id', 'unknown')}" + ) _model, custom_llm_provider, _, _ = litellm.get_llm_provider( - model=deployment.get("litellm_params", {}).get("model", ""), - litellm_params=LiteLLM_Params(**deployment.get("litellm_params", {})), + model=litellm_params.model, + litellm_params=litellm_params, ) ## SET MODEL TO 'model=' - if base_model is None + not azure From fc130376d96374406567dee9597a0ecd8b9b42e4 Mon Sep 17 00:00:00 2001 From: Ryan Crabbe Date: Fri, 6 Feb 2026 10:13:54 -0800 Subject: [PATCH 2/2] test: add test for get_router_model_info with Deployment object Verifies that get_router_model_info accepts a Deployment object directly and properly handles the LiteLLM_Params isinstance check. --- .../test_router_helper_utils.py | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/router_unit_tests/test_router_helper_utils.py b/tests/router_unit_tests/test_router_helper_utils.py index 673c606a7d..99926b39c9 100644 --- a/tests/router_unit_tests/test_router_helper_utils.py +++ b/tests/router_unit_tests/test_router_helper_utils.py @@ -2198,3 +2198,33 @@ def test_get_valid_args(): # Verify it contains keyword-only arguments too # These are common Router.__init__ parameters assert "assistants_config" in valid_args or "search_tools" in valid_args + + +def test_get_router_model_info_with_deployment_object(): + """Test get_router_model_info accepts Deployment object directly and reuses LiteLLM_Params""" + router = Router( + model_list=[ + { + "model_name": "gpt-4", + "litellm_params": {"model": "gpt-4", "api_key": "test-key"}, + "model_info": {"id": "test-id"}, + } + ] + ) + + # Get the Deployment object (not dict) + deployment = router.get_deployment(model_id="test-id") + assert deployment is not None + assert isinstance(deployment, Deployment) + assert isinstance(deployment.litellm_params, LiteLLM_Params) + + # Pass Deployment directly (not .model_dump()) - this exercises the isinstance check + # that reuses the existing LiteLLM_Params instead of reconstructing it + model_info = router.get_router_model_info( + deployment=deployment, + received_model_name="gpt-4", + ) + + # Verify we got valid model info back + assert model_info is not None + assert isinstance(model_info, dict)