The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(router): stop get_router_model_info from wiping cached pricing
Devin AI48de8106efc0
diff --git a/litellm/router.py b/litellm/router.pyindex fb2af41dcf2..baa4724b8eb 100644--- a/litellm/router.py+++ b/litellm/router.py@@ -29,6 +29,7 @@ import anyioimport httpximport openaifrom openai import AsyncOpenAI+from pydantic import BaseModelfrom typing_extensions import overloadimport litellm@@ -9080,12 +9081,19 @@ class Router:model_info: Final = litellm.get_model_info(model=model_info_name)## CHECK USER SET MODEL INFO- user_model_info: Final = deployment.get("model_info") or {}+ raw_user_model_info: Final = deployment.get("model_info") or {}+ user_model_info: Final = (+ raw_user_model_info.model_dump(exclude_none=True)+ if isinstance(raw_user_model_info, BaseModel)+ else {key: value for key, value in raw_user_model_info.items() if value is not None}+ )- if model_info is not None:- model_info.update(cast(ModelInfo, user_model_info))+ if model_info is None:+ return model_info- return model_info+ # get_model_info() hands back an lru_cache'd dict; merging into a copy keeps+ # deployment overrides out of the shared entry+ return cast(ModelMapInfo, {**model_info, **user_model_info})def get_model_info(self, id: str) -> dict | None:"""diff --git a/tests/test_litellm/test_router.py b/tests/test_litellm/test_router.pyindex bdbf33fb0e1..b3c348a1221 100644--- a/tests/test_litellm/test_router.py+++ b/tests/test_litellm/test_router.py@@ -7954,3 +7954,45 @@ def test_ensure_deployment_affinity_callback_is_idempotent():finally:for cb in router.optional_callbacks or []:litellm.logging_callback_manager.remove_callback_from_all_lists(cb)+++def test_get_router_model_info_does_not_wipe_cached_pricing():+ """A Deployment's model_info declares the mirrored pricing fields with None defaults;+ merging it must not write those Nones into the lru_cache'd dict get_model_info() owns,+ or /model/info loses built-in prices for every model a worker serves."""+ from litellm.types.router import Deployment, LiteLLM_Params, ModelInfo++ litellm.get_model_info.cache_clear()+ expected = copy.deepcopy(litellm.get_model_info(model="anthropic/claude-sonnet-4-5"))++ router = litellm.Router(model_list=[])+ merged = router.get_router_model_info(+ deployment=Deployment(+ model_name="sonnet",+ litellm_params=LiteLLM_Params(model="claude-sonnet-4-5", custom_llm_provider="anthropic"),+ model_info=ModelInfo(id="sonnet-1"),+ ),+ received_model_name="sonnet",+ )++ assert litellm.get_model_info(model="anthropic/claude-sonnet-4-5") == expected+ for field in ("input_cost_per_token", "output_cost_per_token", "cache_read_input_token_cost"):+ assert merged[field] == expected[field]+++def test_get_router_model_info_keeps_explicit_pricing_overrides():+ from litellm.types.router import Deployment, LiteLLM_Params, ModelInfo++ litellm.get_model_info.cache_clear()+ router = litellm.Router(model_list=[])+ merged = router.get_router_model_info(+ deployment=Deployment(+ model_name="sonnet",+ litellm_params=LiteLLM_Params(model="claude-sonnet-4-5", custom_llm_provider="anthropic"),+ model_info=ModelInfo(id="sonnet-1", input_cost_per_token=1e-08),+ ),+ received_model_name="sonnet",+ )++ assert merged["input_cost_per_token"] == 1e-08+ assert litellm.get_model_info(model="anthropic/claude-sonnet-4-5")["input_cost_per_token"] != 1e-08