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

Replace None with 0 in CompletionUsage #1837

Open
1 task done
karolinepauls opened this issue Oct 31, 2024 · 1 comment
Open
1 task done

Replace None with 0 in CompletionUsage #1837

karolinepauls opened this issue Oct 31, 2024 · 1 comment

Comments

@karolinepauls
Copy link

karolinepauls commented Oct 31, 2024

Confirm this is a feature request for the Python library and not the underlying OpenAI API.

  • This is a feature request for the Python library

Describe the feature or improvement you're requesting

https://github.com/openai/openai-python/blob/891e1c17b7fecbae34d1915ba90c15ddece807f9/src/openai/types/completion_usage.py#L27C1-L40C50

There is no reason for any of the values to be optional, since those are all counters. Rather than None, they should be zeros. This change will make tracking usage require much less client code.

Unnecessary optionals:

  • CompletionUsage.completion_tokens_details
  • CompletionUsage.prompt_tokens_details
  • PromptTokensDetails.audio_tokens
  • PromptTokensDetails.cached_tokens
  • CompletionTokensDetails.audio_tokens
  • CompletionTokensDetails.reasoning_tokens

Additional context

Currently, you need to write something like this for type checking to work (I don't think Mypy will work with code catching AttributeError):

reasoning_tokens = (left.completion_tokens_details and left.completion_tokens_details.reasoning_tokens or 0) + (right.completion_tokens_details and right.completion_tokens_details.reasoning_tokens or 0)

It should be possible and correct to just have:

reasoning_tokens = left.completion_tokens_details.reasoning_tokens + right.completion_tokens_details.reasoning_tokens
Programmer-RD-AI added a commit to Programmer-RD-AI/openai-python that referenced this issue Jan 18, 2025
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.
@Programmer-RD-AI
Copy link

Hi,

I’ve just opened a PR #2030 to address the request to replace None with 0 for token counters in the CompletionUsage class. The main change ensures that the token fields default to 0, making it easier to track usage and reducing the need for additional checks.

I’d love to hear your feedback on the PR and whether you think any further adjustments are needed. Let me know if there’s anything else you'd like me to change!

Thanks!

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

No branches or pull requests

3 participants