Python: fix(python): handle callable class middleware safely in _determine_middleware_type (#6697) - #7333
Conversation
There was a problem hiding this comment.
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 avoidAttributeErrorfor 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). |
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||
|
the mistaken signature are caught by type checkers (which is good), so we need to explicitly ignore those, see the failing check. @hsusul |
|
Thanks for pointing that out! Updated the test suite in commit |
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 unhandledAttributeError: 'BadMiddleware' object has no attribute '__name__'during exception string formatting, masking the intendedMiddlewareException.Description & Review Guide
What are the major changes?
_determine_middleware_type()inpython/packages/core/agent_framework/_middleware.pyto safely resolve the middleware display name usinggetattr(middleware, "__name__", type(middleware).__name__)instead of accessingmiddleware.__name__directly.TestCallableClassMiddlewareErrorHandlinginpython/packages/core/tests/core/test_middleware_with_agent.pycovering parameter count mismatch, decorator/annotation mismatch, and undetermined type errors on callable class instances.What is the impact of these changes?
MiddlewareExceptionerrors with the class name rather than crashing with anAttributeError.What do you want reviewers to focus on?
_determine_middleware_typeand regression test assertions.Related Issue
Fixes #6697
Contribution Checklist