From b156f7cf76d41fdcd4361fc4d473e59f45ac6c8e Mon Sep 17 00:00:00 2001 From: Ishaan Jaffer Date: Fri, 6 Feb 2026 14:04:06 -0800 Subject: [PATCH] add validation to get map --- .../litellm_core_utils/get_model_cost_map.py | 55 +++++++++++++------ 1 file changed, 37 insertions(+), 18 deletions(-) diff --git a/litellm/litellm_core_utils/get_model_cost_map.py b/litellm/litellm_core_utils/get_model_cost_map.py index 9b86f4ca2f0..f39fd4ebe09 100644 --- a/litellm/litellm_core_utils/get_model_cost_map.py +++ b/litellm/litellm_core_utils/get_model_cost_map.py @@ -8,25 +8,45 @@ export LITELLM_LOCAL_MODEL_COST_MAP=True ``` """ +import json import os +from importlib.resources import files import httpx +from litellm.constants import MIN_MODEL_COST_MAP_ENTRIES + + +def _load_local_model_cost_map() -> dict: + """Load the model cost map from the bundled backup file.""" + return json.loads( + files("litellm") + .joinpath("model_prices_and_context_window_backup.json") + .read_text(encoding="utf-8") + ) + + +def validate_model_cost_map(data: dict) -> bool: + """ + Returns True if the model cost map looks structurally sound. + + Checks: + - data is a dict + - has more than MIN_MODEL_COST_MAP_ENTRIES entries + """ + if not isinstance(data, dict): + return False + if len(data) < MIN_MODEL_COST_MAP_ENTRIES: + return False + return True + def get_model_cost_map(url: str) -> dict: if ( os.getenv("LITELLM_LOCAL_MODEL_COST_MAP", False) or os.getenv("LITELLM_LOCAL_MODEL_COST_MAP", False) == "True" ): - from importlib.resources import files - import json - - content = json.loads( - files("litellm") - .joinpath("model_prices_and_context_window_backup.json") - .read_text(encoding="utf-8") - ) - return content + return _load_local_model_cost_map() try: response = httpx.get( @@ -34,14 +54,13 @@ def get_model_cost_map(url: str) -> dict: ) # set a 5 second timeout for the get request response.raise_for_status() # Raise an exception if the request is unsuccessful content = response.json() + + if not validate_model_cost_map(content): + raise ValueError( + "Remote model cost map failed validation: " + f"got {type(content).__name__} with {len(content) if isinstance(content, dict) else 'N/A'} entries" + ) + return content except Exception: - from importlib.resources import files - import json - - content = json.loads( - files("litellm") - .joinpath("model_prices_and_context_window_backup.json") - .read_text(encoding="utf-8") - ) - return content + return _load_local_model_cost_map()