Python: fix(python): handle callable class middleware safely in _determine_middleware_type (#6697)#7333
Open
hsusul wants to merge 1 commit into
Conversation
Contributor
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). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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