- 
                Notifications
    
You must be signed in to change notification settings  - Fork 657
 
Python: Agent and Function middleware #770
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
          
Python Test Coverage Report •
 Python Unit Test Overview
  | 
    ||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
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 implements middleware functionality for agent and function invocations in the Python Agent Framework. It allows developers to intercept and process agent calls and function executions with custom logic before and after execution.
Key changes:
- Added middleware infrastructure supporting both class-based and function-based middleware implementations
 - Integrated middleware pipelines into agent execution flow with automatic classification and routing
 - Updated agent creation APIs to accept middleware parameters
 
Reviewed Changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description | 
|---|---|
| python/samples/getting_started/middleware/function_based_middleware.py | Example demonstrating function-based middleware for security checking and logging | 
| python/samples/getting_started/middleware/class_based_middleware.py | Example demonstrating class-based middleware with the same functionality | 
| python/packages/workflow/agent_framework_workflow/_agent.py | Updated workflow agent to support new middleware-aware execution signature | 
| python/packages/main/agent_framework/_tools.py | Added middleware pipeline integration to function execution infrastructure | 
| python/packages/main/agent_framework/_middleware.py | Core middleware implementation with context objects, base classes, and execution pipelines | 
| python/packages/main/agent_framework/_clients.py | Updated client creation to accept middleware parameters and filter them from execution kwargs | 
| python/packages/main/agent_framework/_agents.py | Major refactoring to integrate middleware pipelines into agent execution flow | 
| python/packages/main/agent_framework/init.py | Exposed middleware types in the public API | 
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
        
          
                python/samples/getting_started/middleware/function_based_middleware.py
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                python/samples/getting_started/middleware/class_based_middleware.py
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                python/samples/getting_started/middleware/class_based_middleware.py
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                python/samples/getting_started/middleware/class_based_middleware.py
              
                Outdated
          
            Show resolved
            Hide resolved
        
      …ent-framework into python-middleware
        
          
                python/samples/getting_started/middleware/override_result_with_middleware.py
          
            Show resolved
            Hide resolved
        
      This reverts commit 6f82660.
* Initial middleware implementation * Small fixes * Small updates * Small updates in samples * Moved middleware functionality to decorator * Removed obsolete file * Renamed AgentInvocationContext to AzureRunContext * Added unit tests * Small settings update for test discovery in VS Code * Added unit tests * Reverted changes in environment settings * Added context result override * Renaming and updates to logic * Added more samples * Updated DEV_SETUP.md * Addressed PR feedback * Addressed PR feedback * Removed unused parameter * Small fix * Small fix in telemetry logic * Revert "Small fix in telemetry logic" This reverts commit 6f82660. * Small fix --------- Co-authored-by: Chris <66376200+crickman@users.noreply.github.com>
Motivation and Context
Related: #734
Follow-up: #802
This PR includes middleware for Agent and Function invocations. Middleware implementation can be a class or function.
Examples:
class_based_middleware.pyexception_handling_with_middleware.pyfunction_based_middleware.pyoverride_result_with_middleware.pyAgent invocation class-based middleware:
Agent invocation function-based middleware:
Function invocation class-based middleware:
Function invocation function-based middleware:
Agent initialization:
Contribution Checklist