Skip to content

fix(openai): handle missing cached/reasoning tokens in service_tier math#36946

Closed
zxuhan7 (zxuhan) wants to merge 1 commit intolangchain-ai:masterfrom
zxuhan:fix/36657-usage-metadata-service-tier
Closed

fix(openai): handle missing cached/reasoning tokens in service_tier math#36946
zxuhan7 (zxuhan) wants to merge 1 commit intolangchain-ai:masterfrom
zxuhan:fix/36657-usage-metadata-service-tier

Conversation

@zxuhan
Copy link
Copy Markdown

Closes #36657


When service_tier is priority or flex, _create_usage_metadata (and the responses-API equivalent) compute the per-tier input/output token counts by subtracting cached and reasoning tokens from the totals. Both detail dicts are populated via .get("cached_tokens") / .get("reasoning_tokens"), which store None when the API omits the field. The follow-up dict.get(key, 0) then returns None (the key exists with value None), and int - None raises TypeError.

Switching to dict.get(key) or 0 coerces the absent-or-None case to 0, matching the pattern already used elsewhere in this module after #36500. Adds regression tests for both helpers covering the empty-*_tokens_details path with service_tier set.

AI agent involvement: this contribution was drafted with assistance from an AI coding agent.

@github-actions github-actions Bot added fix For PRs that implement a fix integration PR made that is related to a provider partner package integration openai `langchain-openai` package issues & PRs size: S 50-199 LOC labels Apr 22, 2026
@github-actions
Copy link
Copy Markdown

This PR has been automatically closed because you are not assigned to the linked issue.

External contributors must be assigned to an issue before opening a PR for it. Please:

  1. Comment on the linked issue to request assignment from a maintainer
  2. Once assigned, your PR will be reopened automatically

Maintainers: reopen this PR or remove the missing-issue-link label to bypass this check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external fix For PRs that implement a fix integration PR made that is related to a provider partner package integration missing-issue-link new-contributor openai `langchain-openai` package issues & PRs size: S 50-199 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(openai): _create_usage_metadata crashes with TypeError when service_tier is set and cached_tokens is missing

1 participant