Fixed bug usage.prompt_tokens_details=None #2773
Open
+48
−3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Changes being requested
Problem
When Gemini API responses omitted prompt_tokens_details or completion_tokens_details from the usage object, the OpenAI Python library was setting them to None instead of objects with None values:
'prompt_tokens_details': None # ❗️WrongSolution
Added preprocessing in src/openai/_models.py to ensure missing fields are initialized as empty dicts before Pydantic validation:
Created _preprocess_completion_usage() function
'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': None} # ✅ CorrectAdditional context & links
After code changes, you can see audio_tokens and cached_tokens become available in None instead of
"prompt_tokens_details": Nonebelow demo.6B30DF76-43F3-4148-81E9-718E4672065F.MOV
To test out this changes, use this file
Fixes #2544