fix(openai): prioritize api-provided token over tiktoken calculation#3142
BugBot Review
BugBot Analysis Progress (2m 12s elapsed)
✅ Gathered PR context (1s)
✅ Analyzed code changes (0s)
✅ Completed bug detection — 1 potential bug found (2m 8s)
✅ Validation and filtering completed (0s)
✅ Posted analysis results — 1 bug reported (2s)
✅ Analysis completed successfully (0s)
Final Result: BugBot completed review and found 1 potential issue
Request ID: serverGenReqId_405321bd-5a89-4a5d-8827-2258c7671292
Details
Bug: Token Count Validation Fails
The if conditions checking for prompt_tokens and completion_tokens from the API response incorrectly treat 0 as a falsy value. This causes the code to bypass valid zero token counts and unnecessarily fall back to tiktoken calculation. The conditions should check is not None to properly handle legitimate zero token counts.
packages/opentelemetry-instrumentation-openai/opentelemetry/instrumentation/openai/shared/chat_wrappers.py#L527-L531
Was this report helpful? Give feedback by reacting with 👍 or 👎