refactor: Remove redundant backup file for model pricing

- Removed litellm/model_prices_and_context_window_backup.json
- Updated get_model_cost_map() to read from single source
- Refactored _load_local_model_cost_map() to support:
  * Package resources (production/pip install)
  * Project root (development)
- Updated CI/CD workflows:
  * .circleci/config.yml: Copy to litellm/ before publishing
  * .github/workflows/simple_pypi_publish.yml: Same approach
  * ci_cd/check_files_match.py: Updated file paths
- Updated test_get_model_file.py to use project root
- Renamed test_get_backup_model_cost_map → test_get_local_model_cost_map

Benefits:
- Eliminates file duplication
- Single source of truth for model pricing
- No need for PRs like #16460 to sync backup files
- Simpler maintenance

The backup was unnecessary because CI copies the file to the package before publishing.
This commit is contained in:
Chesars 2025-11-13 12:43:25 -03:00
parent e1c607e22a
commit 88ee7c507b
6 changed files with 51 additions and 24718 deletions

View File

@ -2887,9 +2887,9 @@ jobs:
- checkout
- run:
name: Copy model_prices_and_context_window File to model_prices_and_context_window_backup
name: Copy model_prices_and_context_window File to litellm package
command: |
cp model_prices_and_context_window.json litellm/model_prices_and_context_window_backup.json
cp model_prices_and_context_window.json litellm/model_prices_and_context_window.json
- run:
name: Checkout code

View File

@ -48,7 +48,7 @@ jobs:
- name: Copy model prices file
run: |
cp model_prices_and_context_window.json litellm/model_prices_and_context_window_backup.json
cp model_prices_and_context_window.json litellm/model_prices_and_context_window.json
- name: Build package
run: |

View File

@ -5,11 +5,11 @@ import shutil
def main(argv=None):
print(
"Comparing model_prices_and_context_window and litellm/model_prices_and_context_window_backup.json files... checking if they match."
"Comparing model_prices_and_context_window.json files in root and litellm/ directory... checking if they match."
)
file1 = "model_prices_and_context_window.json"
file2 = "litellm/model_prices_and_context_window_backup.json"
file2 = "litellm/model_prices_and_context_window.json"
cmp_result = filecmp.cmp(file1, file2, shallow=False)

View File

@ -9,23 +9,48 @@ export LITELLM_LOCAL_MODEL_COST_MAP=True
"""
import os
from pathlib import Path
import httpx
def _load_local_model_cost_map() -> dict:
"""Load model cost map from local filesystem.
Tries to load from:
1. Package resources (production, after pip install)
2. Project root (development)
"""
import json
# Try loading from package resources (production)
try:
import importlib.resources
with importlib.resources.open_text(
"litellm", "model_prices_and_context_window.json"
) as f:
return json.load(f)
except (FileNotFoundError, ModuleNotFoundError):
pass
# Try loading from project root (development)
try:
current_dir = Path(__file__).parent.parent.parent
model_cost_map_path = current_dir / "model_prices_and_context_window.json"
with open(model_cost_map_path, "r") as f:
return json.load(f)
except FileNotFoundError:
raise FileNotFoundError(
"Could not find model_prices_and_context_window.json in package or project root"
)
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"
):
import importlib.resources
import json
with importlib.resources.open_text(
"litellm", "model_prices_and_context_window_backup.json"
) as f:
content = json.load(f)
return content
return _load_local_model_cost_map()
try:
response = httpx.get(
@ -35,11 +60,4 @@ def get_model_cost_map(url: str) -> dict:
content = response.json()
return content
except Exception:
import importlib.resources
import json
with importlib.resources.open_text(
"litellm", "model_prices_and_context_window_backup.json"
) as f:
content = json.load(f)
return content
return _load_local_model_cost_map()

File diff suppressed because it is too large Load Diff

View File

@ -16,10 +16,17 @@ def test_get_model_cost_map():
pytest.fail(f"An exception occurred: {e}")
def test_get_backup_model_cost_map():
with importlib.resources.open_text(
"litellm", "model_prices_and_context_window_backup.json"
) as f:
print("inside backup")
def test_get_local_model_cost_map():
"""Test that we can load the local model cost map"""
from pathlib import Path
# Test loading from project root (development scenario)
project_root = Path(__file__).parent.parent.parent
model_cost_map_path = project_root / "model_prices_and_context_window.json"
with open(model_cost_map_path, "r") as f:
print("inside local model cost map")
content = json.load(f)
print("content", content)
assert content is not None
assert len(content) > 0