From 19afe2b20c1d44ad558a13414dc21605598d18ea Mon Sep 17 00:00:00 2001 From: Chesars Date: Mon, 4 May 2026 21:54:49 -0300 Subject: [PATCH] fix(get_model_cost_map): harden source-checkout detection and log resolved path Address PR feedback: - Witness the repo root with pyproject.toml so moving get_model_cost_map.py cannot silently break detection (parents[2] alone was depth-fragile). - Emit verbose_logger.debug at each branch so the resolved file is observable when diagnosing pricing issues. --- litellm/litellm_core_utils/get_model_cost_map.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/litellm/litellm_core_utils/get_model_cost_map.py b/litellm/litellm_core_utils/get_model_cost_map.py index ccff0ba739d..35c393c47d3 100644 --- a/litellm/litellm_core_utils/get_model_cost_map.py +++ b/litellm/litellm_core_utils/get_model_cost_map.py @@ -37,12 +37,20 @@ class GetModelCostMap: @staticmethod def load_local_model_cost_map() -> dict: """Load the local model cost map: repo-root JSON in a source checkout, bundled package copy in an installed wheel.""" - repo_root_json = ( - Path(__file__).resolve().parents[2] / "model_prices_and_context_window.json" - ) - if repo_root_json.is_file(): + candidate_root = Path(__file__).resolve().parents[2] + repo_root_json = candidate_root / "model_prices_and_context_window.json" + # Require pyproject.toml as a witness so we don't misidentify an arbitrary + # parent directory as the repo root if this file is ever moved. + if repo_root_json.is_file() and (candidate_root / "pyproject.toml").is_file(): + verbose_logger.debug( + "LiteLLM: loading local model cost map from repo root %s", + repo_root_json, + ) return json.loads(repo_root_json.read_text(encoding="utf-8")) + verbose_logger.debug( + "LiteLLM: loading local model cost map from bundled package resource" + ) return json.loads( files("litellm") .joinpath("model_prices_and_context_window_backup.json")