openai-python
openai-python copied to clipboard
Replace None with 0 in CompletionUsage
Confirm this is a feature request for the Python library and not the underlying OpenAI API.
- [X] 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_detailsCompletionUsage.prompt_tokens_detailsPromptTokensDetails.audio_tokensPromptTokensDetails.cached_tokensCompletionTokensDetails.audio_tokensCompletionTokensDetails.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
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!