Python: include all usage detail fields in OTEL span attributes (fixes #5511)#6194
Python: include all usage detail fields in OTEL span attributes (fixes #5511)#6194hanhan761 wants to merge 8 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Extends response usage attribute capture to include all integer usage metrics beyond just input/output tokens, emitting them as gen_ai.usage.* OpenTelemetry attributes.
Changes:
- Iterates over all
usage_detailsitems instead of only readinginput_token_countandoutput_token_count. - Maps known keys to existing
OtelAttr.INPUT_TOKENS/OtelAttr.OUTPUT_TOKENSattributes. - Emits any additional integer usage fields as
gen_ai.usage.<key>attributes.
|
All review comments addressed:
@eavanvalkenburg Ready for re-review. |
|
Addressed: restored legacy provider field names alongside the new OTel-standard ones for backward compatibility.
Existing integrations using the legacy keys continue to work unchanged. |
|
@hanhan761 there are checks failing, Bandit mistakes tokens for passwords, so that needs to ignored. |
|
If possible, could you also add these usage results to the OpenAI client? |
that is already in this PR @TaoChenOSU |
Ahh, you're right. And sorry, I forgot to post my comment yesterday. |
|
@hanhan761 there are still checks failing, please have a look and preferable also run all the checks locally! |
Head branch was pushed to by a user without write access
|
still failing @hanhan761 please let us know if you can see this through, we want to get this fixed! make sure to use the dev setup guide for the way things are set up by the CI so that it is easier to validate locally |
- Add cache_creation_input_token_count, cache_read_input_token_count, and reasoning_output_token_count to UsageDetails TypedDict - Add OTel standard attribute constants (cache_creation, cache_read, reasoning) - Replace GEN_AI_USAGE_PREFIX with explicit _USAGE_FIELD_TO_OTEL_ATTR mapping - Update providers (Anthropic, OpenAI Responses, OpenAI Chat Completions) to use standard field names instead of provider-specific keys - Update related tests and samples
Keep old provider-specific field names (anthropic.cache_*, openai.*, completion/*, prompt/*) alongside the new OTel-standard field names so existing integrations that depend on the legacy keys continue to work.
4eeba8c to
739aac5
Compare
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
why are all these tests removed?
|
Thank you @hanhan761 for opening this and for the work/investigation here. I've opened #6493 as a smaller replacement that keeps the existing provider-specific usage keys while adding the OTel standard mappings, so I'm closing this PR in favor of that one. |
Summary
_build_response_attributes()\ in \observability.py\ only extracts \input_token_count\ and \output_token_count\ from
esponse.usage_details. Any additional token counts set by providers (e.g. \cached_input_tokens,
easoning_tokens) are silently dropped from OTEL span attributes.
Changes
Replaced the hardcoded extraction of \input_token_count/\output_token_count\ with a loop over all integer-valued keys in \usage_details:
easoning_tokens) are set as \gen_ai.usage.\
Related
Fixes #5511