Skip to content

fix(collector-builder): skip invalid rows in load_model_costs - #1470

Open
Elias-Sun wants to merge 1 commit into
AgentOps-AI:mainfrom
Elias-Sun:fix/load-model-costs-skip-invalid
Open

Elias-Sun wants to merge 1 commit into
AgentOps-AI:mainfrom
Elias-Sun:fix/load-model-costs-skip-invalid

Conversation

@Elias-Sun

Copy link
Copy Markdown

Summary

Fixes #1460.

load_model_costs() catches pydantic.ValidationError and prints Model ... has invalid data. Skipping., but then falls through to costs.append(cost) anyway:

  • Invalid first row: cost was never assigned, so the loader raises UnboundLocalError and collector config generation stops.
  • Invalid row after a valid one: cost still holds the previous model, so that model is appended a second time.

The fix adds continue in the except branch so invalid rows are actually skipped, which is what the log message already says happens.

Impact on real data

Run against the tokencost model_prices.json installed locally (384 rows currently fail validation, mostly zero input cost):

entries returned unique models
before 1465 1081
after 1081 1081

So on today's data, every generated collector config contains 384 duplicate model entries. If the first row of the file ever becomes invalid, config generation fails outright.

Tests

I added TestLoadModelCostsInvalidRows to builder/tests/test_model_cost.py. It points get_tokencost_path at a temporary model_prices.json via monkeypatch and covers:

  • test_invalid_first_row_is_skipped: invalid row first, then two valid rows
  • test_invalid_middle_row_does_not_duplicate_previous: valid, invalid, valid

Run from app/opentelemetry-collector with Python 3.12 and pydantic 2.14.1:

python -m pytest builder/tests/test_model_cost.py -v

Before the fix (new tests only; the 11 existing tests passed):

FAILED builder/tests/test_model_cost.py::TestLoadModelCostsInvalidRows::test_invalid_first_row_is_skipped
    UnboundLocalError: cannot access local variable 'cost' where it is not associated with a value
    builder/costs/__init__.py:119
FAILED builder/tests/test_model_cost.py::TestLoadModelCostsInvalidRows::test_invalid_middle_row_does_not_duplicate_previous
    AssertionError: assert ['gpt-4', 'gpt-4', 'claude-3-opus'] == ['gpt-4', 'claude-3-opus']
========================= 2 failed, 11 passed in 0.06s =========================

After the fix:

builder/tests/test_model_cost.py::TestLoadModelCostsInvalidRows::test_invalid_first_row_is_skipped PASSED
builder/tests/test_model_cost.py::TestLoadModelCostsInvalidRows::test_invalid_middle_row_does_not_duplicate_previous PASSED
============================== 13 passed in 0.05s ==============================

🤖 Generated with Claude Code

The ValidationError handler logged that the model was being skipped but
then fell through to `costs.append(cost)`. An invalid first row raised
UnboundLocalError, and an invalid row after a valid one re-appended the
previous model. Continue to the next row instead.

Adds regression tests for an invalid row in the first and middle
positions.

Fixes AgentOps-AI#1460

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Invalid cost rows still abort collector config generation

1 participant