Skip to content

Fix TypeError when total_tokens is None in PortkeyModel#781

Open
hobostay wants to merge 3 commits into
SWE-agent:mainfrom
hobostay:fix/total-tokens-none-bug
Open

Fix TypeError when total_tokens is None in PortkeyModel#781
hobostay wants to merge 3 commits into
SWE-agent:mainfrom
hobostay:fix/total-tokens-none-bug

Conversation

@hobostay
Copy link
Copy Markdown
Contributor

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

## 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
Copy link
Copy Markdown

codecov Bot commented Mar 11, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
src/minisweagent/models/portkey_model.py 82.88% <100.00%> (+0.47%) ⬆️

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@klieret
Copy link
Copy Markdown
Member

klieret commented Mar 11, 2026

wouldn't prompt and completion tokens then also be None and it would still fail?

hobostay and others added 2 commits March 12, 2026 12:06
…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.
@hobostay
Copy link
Copy Markdown
Contributor Author

hobostay commented Mar 12, 2026

@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.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants