Python: fix(core): ensure add_usage_details returns copies and skips booleans by ManoharPaturi · Pull Request #8613 · microsoft/agent-framework · GitHub
Skip to content

Python: fix(core): ensure add_usage_details returns copies and skips booleans - #8613

Merged
Eduard van Valkenburg (eavanvalkenburg) merged 1 commit into
microsoft:mainfrom
ManoharPaturi:fix/add-usage-details-bool-and-copy
Sep 22, 2026
Merged

Eduard van Valkenburg (eavanvalkenburg) merged 1 commit into
microsoft:mainfrom
ManoharPaturi:fix/add-usage-details-bool-and-copy

Conversation

@ManoharPaturi

@ManoharPaturi Manohar Paturi (ManoharPaturi) commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Motivation & Context

In add_usage_details, when one of the arguments is None, returning the other argument directly allowed callers to mutate the input usage dictionary in place. Returning a new UsageDetails copy matches the documented contract ("Returns: A new UsageDetails dictionary with summed values").

Additionally, because bool is a subclass of int in Python, boolean values in usage metadata passed the isinstance(val, int) check and were summed into integer token counts. Early return paths also bypassed filtering when only one usage payload was provided. Normalizing None operands to empty UsageDetails and routing all inputs through a common merge and filter path ensures boolean and non-integer values are consistently skipped and logged regardless of operand position.

Description & Review Guide

  • What are the major changes?
    Normalized None operands to UsageDetails() in add_usage_details and removed early return shortcuts, routing all inputs through the common merge path.
    Explicitly excluded bool values alongside non-integers, logging a warning and skipping them.
    Added unit tests in test_types.py covering copy semantics and filtering when operands are None.

  • What is the impact of these changes?
    Callers can no longer accidentally mutate input usage dictionaries in place, and boolean flags or non-integer metadata are consistently filtered out across all input combinations.

  • What do you want reviewers to focus on?
    The unified normalization and merge path in python/packages/core/agent_framework/_types.py and test coverage in test_types.py.

Related Issue

None

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR is linked to an issue and there is no other open PR for this issue (see Related Issue above).
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.

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 reached their quota limit.

@agent-framework-automation agent-framework-automation Bot added the python Usage: [Issues, PRs], Target: Python label Sep 21, 2026
@github-actions github-actions Bot changed the title fix(core): ensure add_usage_details returns copies and skips booleans Python: fix(core): ensure add_usage_details returns copies and skips booleans Sep 21, 2026
Comment thread python/packages/core/agent_framework/_types.py Outdated
@eavanvalkenburg

Copy link
Copy Markdown
Member

…ts consistently

In add_usage_details, passing None for one of the arguments previously returned the other argument dictionary directly by reference, and early return copies bypassed boolean and non-integer filtering. Normalizing None operands to empty UsageDetails and routing all combinations through the common merge path ensures returned dictionaries are always new copies and that booleans and non-integer values are consistently skipped and logged.

Signed-off-by: Manohar Paturi <186662190+ManoharPaturi@users.noreply.github.com>
@ManoharPaturi

Copy link
Copy Markdown
Contributor Author

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Manohar Paturi (@ManoharPaturi) Reviewed the complete diff; no high-confidence issues found.

Merged via the queue into microsoft:main with commit eb74c8c Sep 22, 2026
43 checks passed

This branch was successfully deployed

1 active deployment
github-app-auth — bddab7ea Deployed Sep 22, 2026 by ManoharPaturi via add_label #23528
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants