From d290ad71902bd8802fdc51cf7f89ea2dd1e373fc Mon Sep 17 00:00:00 2001 From: Ranuga <79456372+Programmer-RD-AI@users.noreply.github.com> Date: Sat, 18 Jan 2025 10:26:55 +0530 Subject: [PATCH] Fix: Replace None with 0 in CompletionUsage Tokens Details This commit addresses the issue outlined in #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. --- src/openai/types/completion_usage.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/openai/types/completion_usage.py b/src/openai/types/completion_usage.py index d8c4e84cf7..b845199311 100644 --- a/src/openai/types/completion_usage.py +++ b/src/openai/types/completion_usage.py @@ -14,10 +14,10 @@ class CompletionTokensDetails(BaseModel): appeared in the completion. """ - audio_tokens: Optional[int] = None + audio_tokens: Optional[int] = 0 """Audio input tokens generated by the model.""" - reasoning_tokens: Optional[int] = None + reasoning_tokens: Optional[int] = 0 """Tokens generated by the model for reasoning.""" rejected_prediction_tokens: Optional[int] = None @@ -30,10 +30,10 @@ class CompletionTokensDetails(BaseModel): class PromptTokensDetails(BaseModel): - audio_tokens: Optional[int] = None + audio_tokens: Optional[int] = 0 """Audio input tokens present in the prompt.""" - cached_tokens: Optional[int] = None + cached_tokens: Optional[int] = 0 """Cached tokens present in the prompt."""