Skip to content

fix(openai): prioritize api-provided token over tiktoken calculation#3142

Merged
nirga merged 3 commits into
traceloop:mainfrom
minimAluminiumalism:stream
Jul 20, 2025
Merged

fix(openai): prioritize api-provided token over tiktoken calculation#3142
nirga merged 3 commits into
traceloop:mainfrom
minimAluminiumalism:stream

Merge branch 'main' into stream

6cb10d5
Select commit
Loading
Failed to load commit list.
Cursor / Cursor BugBot completed Jul 20, 2025 in 2m 11s

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

usage = complete_response["usage"]
if usage.get("prompt_tokens"):
prompt_usage = usage["prompt_tokens"]
if usage.get("completion_tokens"):
completion_usage = usage["completion_tokens"]

Fix in CursorFix in Web


Was this report helpful? Give feedback by reacting with 👍 or 👎