From 99cca13e418cdd3d20abf5b6ec4318a1059a16c1 Mon Sep 17 00:00:00 2001 From: dajiaohuang Date: Thu, 24 Sep 2026 16:00:51 +0800 Subject: [PATCH] fix: skip invalid model cost rows (#1460) --- .../builder/costs/__init__.py | 1 + .../builder/tests/test_model_cost.py | 31 ++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/app/opentelemetry-collector/builder/costs/__init__.py b/app/opentelemetry-collector/builder/costs/__init__.py index b8c638e90..5bcce211f 100644 --- a/app/opentelemetry-collector/builder/costs/__init__.py +++ b/app/opentelemetry-collector/builder/costs/__init__.py @@ -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}") diff --git a/app/opentelemetry-collector/builder/tests/test_model_cost.py b/app/opentelemetry-collector/builder/tests/test_model_cost.py index 678ab2c62..e77f7c69a 100644 --- a/app/opentelemetry-collector/builder/tests/test_model_cost.py +++ b/app/opentelemetry-collector/builder/tests/test_model_cost.py @@ -1,3 +1,4 @@ +import json import pytest from decimal import Decimal from pathlib import Path @@ -174,6 +175,34 @@ def test_validator_methods(self): dumped = model.model_dump() assert dumped["input"] == "0.001" + def test_load_model_costs_skips_invalid_rows(self, tmp_path, monkeypatch): + """Invalid rows are skipped without aborting or duplicating a prior row.""" + model_prices = { + "invalid-first": { + "litellm_provider": "test", + "input_cost_per_token": 0, + "output_cost_per_token": 0.00003, + }, + "valid-model": { + "litellm_provider": "test", + "input_cost_per_token": 0.00001, + "output_cost_per_token": 0.00003, + }, + "invalid-after-valid": { + "litellm_provider": "test", + "input_cost_per_token": 0, + "output_cost_per_token": 0.00003, + }, + } + (tmp_path / "model_prices.json").write_text( + json.dumps(model_prices), encoding="utf-8" + ) + monkeypatch.setattr("builder.costs.get_tokencost_path", lambda: tmp_path) + + costs = load_model_costs() + + assert [cost.model for cost in costs] == ["valid-model"] + def test_get_tokencost_path_integration(self): """Integration test for get_tokencost_path with the real package.""" path = get_tokencost_path() @@ -218,4 +247,4 @@ 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]}..." \ No newline at end of file + f"None of the common models {common_models} found in {model_names[:5]}..."