Skip to content

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

Open
hsusul wants to merge 1 commit into
microsoft:mainfrom
hsusul:fix-python-callable-class-middleware-attribute-error
Open

Python: fix(python): handle callable class middleware safely in _determine_middleware_type (#6697)#7333
hsusul wants to merge 1 commit into
microsoft:mainfrom
hsusul:fix-python-callable-class-middleware-attribute-error

Conversation

@hsusul

@hsusul hsusul commented Jul 26, 2026

Copy link
Copy Markdown

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
@hsusul
hsusul temporarily deployed to github-app-auth July 26, 2026 16:20 — with GitHub Actions Inactive
@hsusul
hsusul temporarily deployed to github-app-auth July 26, 2026 16:20 — with GitHub Actions Inactive
@hsusul
hsusul temporarily deployed to github-app-auth July 26, 2026 16:20 — with GitHub Actions Inactive
@agent-framework-automation agent-framework-automation Bot added the python Usage: [Issues, PRs], Target: Python label Jul 26, 2026
@hsusul
hsusul temporarily deployed to github-app-auth July 26, 2026 16:21 — with GitHub Actions Inactive

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

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

2 participants