Fix TypeError when total_tokens is None in PortkeyModel#781
Open
hobostay wants to merge 3 commits into
Open
Conversation
## Summary Fix a bug where `total_tokens` being None would cause a TypeError in cost calculation. This can happen with some Portkey API responses. ## Changes - **portkey_model.py**: Add check for None total_tokens and calculate from prompt_tokens + completion_tokens - **test_portkey_model.py**: Add test case for total_tokens=None scenario ## Testing Added test `test_portkey_model_total_tokens_none` that simulates the bug scenario and verifies the fix works correctly. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests.
... and 1 file with indirect coverage changes 🚀 New features to boost your workflow:
|
Member
|
wouldn't prompt and completion tokens then also be None and it would still fail? |
…tion Add a helper function to handle potential None values when calculating total_tokens from prompt_tokens + completion_tokens, addressing the concern raised by @klieret.
for more information, see https://pre-commit.ci
Contributor
Author
|
@klieret Thanks for the review! That's a valid concern. You're right that in theory prompt_tokens or completion_tokens could also be None. However, due to the execution order, they are handled before total_tokens calculation (lines 158-169), so they are guaranteed to be non-None when we reach line 174. I have now added a defensive get_token helper function to handle this edge case explicitly, making the code more robust against unexpected API responses. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fix a bug where
total_tokensbeing None would cause a TypeError incost calculation. This can happen with some Portkey API responses.
Changes
Testing
Added test
test_portkey_model_total_tokens_nonethat simulates thebug scenario and verifies the fix works correctly.
Co-Authored-By: Claude Opus 4.6 noreply@anthropic.com