fix: omit non-numeric usage fields from token usage report#1664
Open
rajkumar-prog wants to merge 3 commits into
Open
fix: omit non-numeric usage fields from token usage report#1664rajkumar-prog wants to merge 3 commits into
rajkumar-prog wants to merge 3 commits into
Conversation
CompletionTokensDetails and PromptTokensDetails are Pydantic models, not integers, so int() raises TypeError on them. Use isinstance check to only sum fields that are actually numeric, skipping nested objects.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When running evals, the OpenAI API returns nested objects such as
CompletionTokensDetailsandPromptTokensDetailsalongside plaininteger fields in the usage response. The original code attempted to
sum all fields directly, causing:
Fixes #1582
Root cause
dict(usage)produces a mix of plain integers (completion_tokens,prompt_tokens,total_tokens) and nested Pydantic models(
completion_tokens_details,prompt_tokens_details). The summationloop hit these nested objects and failed because they are not numeric.
Fix
Filter at the key level — only include keys where all events have
Noneor numeric values. Nested objects are excluded entirely fromthe report rather than being reported as a misleading
0.Changes
evals/cli/oaieval.py— filter non-numeric fields at the key levelevals/cli/oaieval_test.py— regression test verifying that nested detail objects are skipped while real token counts are summed correctlyTests
venv/bin/python -m pytest evals/cli/oaieval_test.py— passedvenv/bin/python -m pytestcould not complete locally; the venv is missing several project dependencies (mock,torch,nltk,anthropic,google-generativeai) due to conflicts with Python 3.9