Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Replace None with 0 in CompletionUsage Tokens Details #2030

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Programmer-RD-AI
Copy link

  • I understand that this repository is auto-generated and my pull request may not be merged.

Changes being requested

This pull request addresses the issue raised in #1837 by replacing None with 0 in the CompletionTokensDetails and PromptTokensDetails classes. The token fields, which represent counters, have been updated to use 0 as the default value instead of None. This eliminates unnecessary optional checks and simplifies the handling of token usage.

Specific changes include:

  • Replaced Optional[int] = None with Optional[int] = 0 for the following fields:
    • audio_tokens
    • reasoning_tokens
    • cached_tokens
  • This ensures that all token fields default to 0, making it easier to track usage without additional checks.

Additional context & links

  • This change improves the code by removing unnecessary handling of None values and ensuring all counters start at 0 by default.
  • The Optional type was not removed entirely, as some fields may still be conditionally populated depending on the request or model.
  • For further context, you can refer to the discussion in the issue [#1837](Replace None with 0 in CompletionUsage #1837).

This commit addresses the issue outlined in openai#1837 by replacing `None` with `0` for various token fields in the `CompletionTokensDetails` and `PromptTokensDetails` classes. These fields are intended to represent counters, so initializing them with `0` simplifies the code and eliminates unnecessary optional checks.

**Changes made:**
- Replaced `None` with `0` for fields like `audio_tokens`, `reasoning_tokens`, and `cached_tokens` in both `CompletionTokensDetails` and `PromptTokensDetails` classes.
- This update makes the token fields more consistent and eliminates the need for optional handling, as they will always default to `0` if not specified.

**Why `Optional` was not removed:**
- The `Optional` typing has been retained for some fields (like `audio_tokens` and `reasoning_tokens`) because, in some cases, the presence of these tokens may depend on certain conditions. By leaving them as `Optional`, we ensure flexibility in cases where the tokens might not be relevant or provided (e.g., for specific models or types of requests).

This update improves usability by simplifying the tracking of token usage, avoiding unnecessary checks, and ensuring default values of `0` for all relevant counters.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant