Skip to content

Python: fix(python): handle callable class middleware safely in _determine_middleware_type (#6697) - #7333

Merged
Eduard van Valkenburg (eavanvalkenburg) merged 2 commits into
microsoft:mainfrom
hsusul:fix-python-callable-class-middleware-attribute-error
Jul 27, 2026
Merged

Eduard van Valkenburg (eavanvalkenburg) merged 2 commits into
microsoft:mainfrom
hsusul:fix-python-callable-class-middleware-attribute-error

Conversation

@hsusul

Copy link
Copy Markdown
Contributor

Motivation & Context

Passing a callable class instance (a class implementing __call__ with fewer than 2 parameters or an unresolvable middleware type) as middleware caused _determine_middleware_type() to raise an unhandled AttributeError: 'BadMiddleware' object has no attribute '__name__' during exception string formatting, masking the intended MiddlewareException.

Description & Review Guide

  • What are the major changes?

    • Updated _determine_middleware_type() in python/packages/core/agent_framework/_middleware.py to safely resolve the middleware display name using getattr(middleware, "__name__", type(middleware).__name__) instead of accessing middleware.__name__ directly.
    • Added unit test suite TestCallableClassMiddlewareErrorHandling in python/packages/core/tests/core/test_middleware_with_agent.py covering parameter count mismatch, decorator/annotation mismatch, and undetermined type errors on callable class instances.
  • What is the impact of these changes?

    • Callable class instances passed as middleware now properly raise clear, informative MiddlewareException errors with the class name rather than crashing with an AttributeError.
  • What do you want reviewers to focus on?

    • Exception handling in _determine_middleware_type and regression test assertions.

Related Issue

Fixes #6697

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.

Copilot AI review requested due to automatic review settings July 26, 2026 16:20
@agent-framework-automation agent-framework-automation Bot added the python Usage: [Issues, PRs], Target: Python label Jul 26, 2026

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.

Pull request overview

This PR fixes a Python middleware classification edge case where passing a callable class instance (i.e., an object implementing __call__) could trigger an AttributeError while formatting an error message in _determine_middleware_type(), masking the intended MiddlewareException. The update makes middleware name resolution robust for non-function callables and adds regression tests to ensure clear, informative exceptions are raised.

Changes:

  • Resolve middleware display names safely via getattr(middleware, "__name__", type(middleware).__name__) to avoid AttributeError for callable class instances.
  • Keep _determine_middleware_type() behavior intact while ensuring all error paths can format messages reliably.
  • Add unit tests covering callable-class middleware failures: insufficient parameters, decorator/annotation type mismatch, and undetermined type.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
python/packages/core/agent_framework/_middleware.py Safely computes a middleware display name and uses it in all exception messages within _determine_middleware_type().
python/packages/core/tests/core/test_middleware_with_agent.py Adds regression tests ensuring callable class instances produce MiddlewareException messages containing the class name (no AttributeError).

@github-actions

github-actions Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/core/agent_framework
   _middleware.py4011795%61, 64, 69, 369, 910, 926, 928, 930, 1063, 1066, 1093, 1095, 1231, 1235, 1417, 1421, 1491
TOTAL45583447990% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
9410 34 💤 0 ❌ 0 🔥 2m 7s ⏱️

@eavanvalkenburg

Copy link
Copy Markdown
Member

the mistaken signature are caught by type checkers (which is good), so we need to explicitly ignore those, see the failing check. Henry Su (@hsusul)

@hsusul

Copy link
Copy Markdown
Contributor Author

Thanks for pointing that out! Updated the test suite in commit 2d716dce8 to annotate the test middleware lists as list[Any] and set _middleware_type directly on the test class.

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.

Python: [Bug]: Callable class middleware raises AttributeError instead of MiddlewareException when __call__ has fewer than 2 parameters

4 participants