Skip to content

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

@Hybirdss

Description

@Hybirdss

Checked other resources

  • This is a bug, not a usage question.
  • I added a clear and descriptive title that summarizes this issue.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
  • This is not related to the langchain-community package.
  • I posted a self-contained, minimal, reproducible example. A maintainer can copy it and run it AS IS.

Package (Required)

langchain-openai

Describe the bug

Found this while reading the service_tier token arithmetic in _create_usage_metadata. When service_tier is "priority" or "flex" and the API response doesn't include cached_tokens in prompt_tokens_details, the subtraction crashes:

from langchain_openai.chat_models.base import _create_usage_metadata

_create_usage_metadata(
    {"prompt_tokens": 100, "completion_tokens": 50, "total_tokens": 150,
     "prompt_tokens_details": {}, "completion_tokens_details": {}},
    service_tier="priority"
)
# TypeError: unsupported operand type(s) for -: 'int' and 'NoneType'

The dict stores None for missing cached_tokens via .get("cached_tokens"), then .get(key, 0) returns None (not 0) because the key exists with value None. Same bug in _create_usage_metadata_responses.

Related to #36500 which fixed the same pattern in other token paths but missed the service_tier arithmetic.

System Info

langchain-openai 1.1.12, Python 3.12

Metadata

Metadata

Assignees

No one assigned

    Labels

    externalopenai`langchain-openai` package issues & PRs

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions