Skip to content

Python: return NotImplemented for unsupported operands in CompletionUsage.__add__ - #14707

Open
Hardik K (hardikkaurani) wants to merge 1 commit into
microsoft:mainfrom
hardikkaurani:fix/completion-usage-operand-validation
Open

Hardik K (hardikkaurani) wants to merge 1 commit into
microsoft:mainfrom
hardikkaurani:fix/completion-usage-operand-validation

Conversation

@hardikkaurani

Copy link
Copy Markdown
Contributor

Motivation and Context

In CompletionUsage.__add__, unsupported operand types (such as adding an integer, None, or an incompatible instance) previously caused an immediate AttributeError when attempting to access other.prompt_tokens.

According to standard Python binary operator conventions (Python Data Model / PEP 207), binary dunder methods should return NotImplemented when an operand type is not supported. This allows Python to fall back to the reflected operator (__radd__) or raise a standard TypeError: unsupported operand type(s) for +.

Per maintainer guidance in #14706, this PR is strictly scoped to this bugfix and excludes any new APIs (total_tokens property or __radd__/sum() support).

Description

  • In CompletionUsage.__add__, added an isinstance(other, CompletionUsage) guard returning NotImplemented for non-CompletionUsage instances.
  • Added unit test test_completion_usage_invalid_add() in test_completion_token_usage.py verifying that adding an unsupported operand raises TypeError.

Contribution Checklist

Copilot AI balanced review requested due to automatic review settings November 2, 2026 14:21
@hardikkaurani
Hardik K (hardikkaurani) requested a review from a team as a code owner November 2, 2026 14:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@hardikkaurani

Hardik K (hardikkaurani) commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor Author

Hi Eduard van Valkenburg (@eavanvalkenburg), following up on your feedback in [#14706](#14706), this PR is strictly scoped to the bug fix in CompletionUsage.add.

It adds the missing type check to return NotImplemented when encountering unsupported operands, allowing Python to raise the standard TypeError instead of an unhandled AttributeError.

As requested, all new APIs (sum() support and total_tokens) have been excluded.

@semantic-kernel-automation semantic-kernel-automation Bot added the python Pull requests for the Python Semantic Kernel label Nov 2, 2026
@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Eduard van Valkenburg (@eavanvalkenburg) can you please take a look for the review !

This branch was previously deployed

1 inactive deployment
github-app-auth — 78578e33 Deployed Nov 2, 2026 by hardikkaurani via add_label #29538
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Pull requests for the Python Semantic Kernel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants