Skip to content

Python: fix(python): support sum(), validate operands, and add total_tokens to CompletionUsage - #14706

Open
Hardik K (hardikkaurani) wants to merge 2 commits into
microsoft:mainfrom
hardikkaurani:fix/completion-usage-radd-and-total-tokens
Open

Hardik K (hardikkaurani) wants to merge 2 commits into
microsoft:mainfrom
hardikkaurani:fix/completion-usage-radd-and-total-tokens

Conversation

@hardikkaurani

Copy link
Copy Markdown
Contributor

Description

This PR improves CompletionUsage in semantic_kernel.connectors.ai.completion_usage:

  1. __radd__ and sum() support:
    Currently, attempting to aggregate multiple CompletionUsage objects using Python's standard sum([u1, u2, ...]) fails with:

    TypeError: unsupported operand type(s) for +: 'int' and 'CompletionUsage'

    This occurs because sum() starts with 0 as the default accumulator and delegates to __radd__. Implementing __radd__ allows sum() to work seamlessly on sequences of CompletionUsage objects.

  2. Operand type validation in __add__:
    __add__ did not check operand types before accessing attributes like other.prompt_tokens. Adding an unsupported type previously raised an uninformative AttributeError: 'int' object has no attribute 'prompt_tokens' instead of returning NotImplemented to let Python raise the standard TypeError.

  3. total_tokens property:
    Adds a computed total_tokens property ((self.prompt_tokens or 0) + (self.completion_tokens or 0)) matching provider conventions (e.g., openai.types.CompletionUsage).

Verification

  • Added regression tests in python/tests/unit/connectors/ai/test_completion_token_usage.py covering:
    • sum([u1, u2, u3])
    • total_tokens computation on non-empty and empty instances
    • TypeError when adding unsupported operand types
  • Ruff check and format pass cleanly.

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

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

Copy link
Copy Markdown
Contributor Author

Hi Eduard van Valkenburg (@eavanvalkenburg), could you please take a look at this PR when you have a moment? It adds radd support for sum(), validates operand types in add, and adds the total_tokens property on CompletionUsage. Tests and ruff checks pass cleanly. Thank you!

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

This branch was successfully deployed

1 active deployment
github-app-auth — 1f8ba268 Deployed Nov 1, 2026 by hardikkaurani via add_label #29533
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