Only write non-zero AdditionalCounts keys in OpenAI chat addUsage - #737
Conversation
There was a problem hiding this comment.
Pull request overview
This pull request refines OpenAI chat usage reporting so UsageDetails.AdditionalCounts is only allocated and populated when OpenAI provides non-zero “detail” token counts, avoiding noisy zero-valued attributes for text-only responses and improving downstream OpenTelemetry span emission behavior.
Changes:
- Updated
addUsageto lazily allocateAdditionalCountsand only write non-zero usage-detail keys. - Updated OpenAI provider black-box tests to expect
AdditionalCounts == nilfor text-only responses and to assert only non-zero additional-count keys are present.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| provider/openaiprovider/chat.go | Lazily populates UsageDetails.AdditionalCounts with non-zero OpenAI usage-detail counts only. |
| provider/openaiprovider/chat_test.go | Adjusts expectations to match the new “only non-zero additional counts” behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This comment has been minimized.
This comment has been minimized.
addUsage unconditionally allocated AdditionalCounts and wrote the audio and prediction token details, so every text-only response carried four zero entries. This made otelprovider's hasUsage report true and emit four zero-valued additional-count span attributes for plain text responses. Guard each detail so keys are added only when non-zero, leaving AdditionalCounts nil in the common text-only case. This matches the sibling Anthropic provider (which guards CacheCreationInputTokens) and the Python SDK, which writes each detail only when truthy.
cabd2ad to
502056a
Compare
Parity Review —
|
What
addUsageinprovider/openaiprovider/chat.gounconditionally allocatedAdditionalCountsand wrote the four audio/prediction token details regardless of value. Every text-only response therefore carried four zero-valued entries (PromptTokensDetails.AudioTokens,CompletionTokensDetails.AudioTokens,.AcceptedPredictionTokens,.RejectedPredictionTokens).This change guards each key with a small helper so it is added only when non-zero, leaving
AdditionalCountsnil in the common text-only case.Why
if usage.CacheCreationInputTokens != 0 { ... }) before allocatingAdditionalCounts.otelprovider'shasUsagereturns true wheneverlen(AdditionalCounts) > 0, so all-zero OpenAI usage previously reportedhasUsage == trueand emitted four zero-valued additional-count span attributes for plain text responses. With the guard, text-only responses no longer inflate the usage map or the span.How it is tested
Existing black-box tests in
chat_test.goare updated to reflect the corrected behavior: text-only responses now expect a nilAdditionalCounts, and the audio-token case asserts only the two non-zero keys are present (the two zero prediction-token keys are dropped). These assertions fail before the fix and pass after.go build ./...,go vet ./provider/openaiprovider/..., andgo test ./provider/openaiprovider/...all pass.