Skip to content

Conversation

@dmytrostruk
Copy link
Member

@dmytrostruk dmytrostruk commented Sep 16, 2025

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.py
  • exception_handling_with_middleware.py
  • function_based_middleware.py
  • override_result_with_middleware.py

Agent invocation class-based middleware:

class MyAgentMiddleware(AgentMiddleware):
    async def process(
        self,
        context: AgentInvocationContext,
        next: Callable[[AgentInvocationContext], Awaitable[None]],
    ) -> None:
        # Logic before invocation
        await next(context)
        # Logic after invocation

Agent invocation function-based middleware:

async def my_agent_middleware(
    context: AgentInvocationContext,
    next: Callable[[AgentInvocationContext], Awaitable[None]],
) -> None:
    # Logic before invocation
    await next(context)
    # Logic after invocation

Function invocation class-based middleware:

class MyFunctionMiddleware(FunctionMiddleware):
    async def process(
        self,
        context: FunctionInvocationContext,
        next: Callable[[FunctionInvocationContext], Awaitable[None]],
    ) -> None:
        # Logic before invocation
        await next(context)
        # Logic after invocation

Function invocation function-based middleware:

async def my_function_middleware(
    context: FunctionInvocationContext,
    next: Callable[[FunctionInvocationContext], Awaitable[None]],
) -> None:
    # Logic before invocation
    await next(context)
    # Logic after invocation

Agent initialization:

agent = OpenAIChatClient().create_agent(
    name="WeatherAgent",
    instructions="You are a helpful weather assistant.",
    tools=get_weather,
    middlewares=[MyAgentMiddleware(), my_function_middleware]
)

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

@dmytrostruk dmytrostruk self-assigned this Sep 16, 2025
Copilot AI review requested due to automatic review settings September 16, 2025 06:18
@markwallace-microsoft
Copy link
Member

markwallace-microsoft commented Sep 16, 2025

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/main/agent_framework
   _agents.py1862586%172–174, 181, 279, 337–339, 341–344, 351, 361, 434–435, 437, 440, 563–564, 566, 569, 628, 660, 705
   _clients.py981188%183, 215–216, 260, 326, 351, 412, 438, 469, 502, 504
   _middleware.py2331095%303, 369–370, 464, 497, 499–500, 502, 505, 599
   _tools.py3493390%161, 207, 353, 355, 357, 449–451, 496, 554, 586, 617–619, 627–629, 649, 651–652, 684, 762–763, 845–846, 850, 869–871, 892–893, 901–902
   telemetry.py48910778%94, 250, 275, 280–282, 284–296, 300–311, 314–317, 320–328, 331–332, 336, 339, 350, 415, 431–432, 434–435, 437–441, 443, 450–452, 454, 492, 494, 496, 579–582, 668–671, 711, 860–862, 966, 973, 975, 977, 979, 981, 983, 985, 987, 989, 991, 995, 1005, 1031–1032, 1043, 1046–1049, 1062–1066, 1069, 1089, 1127
TOTAL8002126084% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
947 94 💤 0 ❌ 0 🔥 26.074s ⏱️

Copy link
Contributor

Copilot AI left a 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.

@markwallace-microsoft markwallace-microsoft added the documentation Improvements or additions to documentation label Sep 18, 2025
@dmytrostruk dmytrostruk added this pull request to the merge queue Sep 18, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Sep 18, 2025
@eavanvalkenburg eavanvalkenburg added this pull request to the merge queue Sep 18, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Sep 18, 2025
@dmytrostruk dmytrostruk added this pull request to the merge queue Sep 18, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Sep 18, 2025
@dmytrostruk dmytrostruk added this pull request to the merge queue Sep 18, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Sep 18, 2025
@moonbox3 moonbox3 added this pull request to the merge queue Sep 18, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Sep 18, 2025
@dmytrostruk dmytrostruk added this pull request to the merge queue Sep 18, 2025
Merged via the queue into microsoft:main with commit 99860a5 Sep 18, 2025
38 of 48 checks passed
ReubenBond pushed a commit to ReubenBond/agent-framework that referenced this pull request Oct 28, 2025
* 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants