Handle nested token usage details in oaieval#1650
Open
kayametehan wants to merge 1 commit intoopenai:mainfrom
Open
Handle nested token usage details in oaieval#1650kayametehan wants to merge 1 commit intoopenai:mainfrom
kayametehan wants to merge 1 commit intoopenai:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates the oaieval CLI’s token-usage aggregation to support newer OpenAI SDK usage payloads that include nested “details” objects (e.g., completion_tokens_details) instead of only scalar token counts, preventing TypeError during result aggregation (Fixes #1582).
Changes:
- Add a usage-metric flattener that converts nested usage objects/mappings into numeric leaf metrics with underscore-delimited keys.
- Aggregate token usage across the union of all observed usage keys across sampling events (instead of assuming a fixed key set).
- Add a regression test to ensure nested usage details are flattened and summed correctly.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
evals/cli/oaieval.py |
Adds _flatten_usage_metrics and updates add_token_usage_to_result to flatten and safely sum nested usage metrics across events. |
evals/cli/oaieval_test.py |
Adds a regression test covering nested CompletionUsage detail fields and verifying aggregated result keys/values. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Fixes #1582.
Overview
add_token_usage_to_resultcurrently assumes every usage value can be summed as a plain integer. That breaks with newer OpenAI SDK usage payloads where fields likecompletion_tokens_detailsandprompt_tokens_detailsare nested objects rather than scalar counts.What changed
prompt_tokens,completion_tokens, andtotal_tokensCompletionUsagedetailsExample
A sampling event that includes
completion_tokens_details.reasoning_tokensnow records:usage_completion_tokensusage_completion_tokens_details_reasoning_tokensusage_prompt_tokensusage_total_tokensinstead of raising a
TypeErrorduring result aggregation.