Repository navigation
fix(instrumentation): support mapping-based usage metadata in TokenUsageExtractor - #1475
Open
Tyagiquamar wants to merge 1 commit into
Open
Tyagiquamar wants to merge 1 commit into
Tyagiquamar wants to merge 1 commit into
Conversation
…ize OTel token fields in TokenUsageExtractor When providers expose usage or usage_metadata as a dictionary/mapping rather than an attribute-based object, TokenUsageExtractor._extract_from_usage_object previously attempted getattr() access on all fields and returned None. This update: - Adds mapping-aware field access to extract token counts from dicts or objects - Supports input_tokens and output_tokens alongside prompt_tokens and completion_tokens - Computes total_tokens when absent if prompt/input and completion/output tokens exist - Adds full regression coverage for mappings, dict responses, CrewAI formats, and OTel conventions
This branch has not been deployed
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 #1432
Addresses #1418
Description
TokenUsageExtractor.extract_from_response()was previously usinggetattr()onusage_dataandresponse. When providers (e.g. Google ADK, Anthropic, or streaming chunks) returnusageorusage_metadataas adict/Mapping, all fields returnedNone, resulting in empty token usage span attributes.Furthermore, providers like Google ADK emit standard OTel GenAI convention names (
input_tokens/output_tokens) and omittotal_tokens.Changes
_extract_from_usage_objectand_extract_from_attributesto safely access fields from bothMapping(e.g.dict) and attribute-based objects.input_tokensandoutput_tokensalongsideprompt_tokensandcompletion_tokens.total_tokensis absent, automatically compute it from prompt/input and completion/output tokens.tests/unit/instrumentation/common/test_token_counting.pycovering dict usage,usage_metadatamappings, raw dict responses, OTel field names, CrewAI formats, andNonehandling.