Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/opentelemetry-collector/builder/costs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def load_model_costs() -> list[ModelCost]:
cost = ModelCost.from_tokencost_json(model_name, model_data)
except pydantic.ValidationError:
print(f"Model {model_name} has invalid data. Skipping.")
continue
costs.append(cost)

print(f"Loaded {len(costs)} model costs from {json_path}")
Expand Down
36 changes: 35 additions & 1 deletion app/opentelemetry-collector/builder/tests/test_model_cost.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import pytest
from decimal import Decimal
from pathlib import Path
Expand Down Expand Up @@ -218,4 +219,37 @@ def test_load_model_costs_integration(self):

# At least one of these common models should be present
assert any(model in model_names for model in common_models), \
f"None of the common models {common_models} found in {model_names[:5]}..."
f"None of the common models {common_models} found in {model_names[:5]}..."

class TestLoadModelCostsInvalidRows:
"""Regression tests for skipping invalid rows in load_model_costs (issue #1460)."""

VALID_A = {"litellm_provider": "openai", "input_cost_per_token": 0.00001, "output_cost_per_token": 0.00003}
VALID_B = {"litellm_provider": "anthropic", "input_cost_per_token": 0.000015, "output_cost_per_token": 0.000075}
# zero input cost is converted to None by the validator, which fails the required Decimal field
INVALID = {"litellm_provider": "openai", "input_cost_per_token": 0, "output_cost_per_token": 0.00003}

def _load(self, tmp_path, monkeypatch, data):
(tmp_path / "model_prices.json").write_text(json.dumps(data))
monkeypatch.setattr("builder.costs.get_tokencost_path", lambda: tmp_path)
return load_model_costs()

def test_invalid_first_row_is_skipped(self, tmp_path, monkeypatch):
"""An invalid first row must not raise UnboundLocalError and must be omitted."""
costs = self._load(tmp_path, monkeypatch, {
"bad-model": self.INVALID,
"gpt-4": self.VALID_A,
"claude-3-opus": self.VALID_B,
})

assert [cost.model for cost in costs] == ["gpt-4", "claude-3-opus"]

def test_invalid_middle_row_does_not_duplicate_previous(self, tmp_path, monkeypatch):
"""An invalid row after a valid one must not re-append the previous model."""
costs = self._load(tmp_path, monkeypatch, {
"gpt-4": self.VALID_A,
"bad-model": self.INVALID,
"claude-3-opus": self.VALID_B,
})

assert [cost.model for cost in costs] == ["gpt-4", "claude-3-opus"]