You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -177,7 +177,7 @@ Agent Framework can be customized using three different types of middleware:
177
177
2.**Function middleware**: Intercepts function (tool) calls made during agent execution, enabling input validation, result transformation, and execution control.
178
178
3.**Chat middleware**: Intercepts the underlying chat requests sent to AI models, providing access to the raw messages, options, and responses.
179
179
180
-
All types support both function-based and class-based implementations. When multiple middleware of the same type are registered, they form a chain where each calls the `next` callable to continue processing.
180
+
All types support both function-based and class-based implementations. When multiple middleware of the same type are registered, they form a chain where each calls the `call_next` callback to continue processing. `call_next` does not take the context as an argument; middleware mutates the shared context object directly and then awaits `call_next()`.
181
181
182
182
> [!NOTE]
183
183
> Middleware order with mixed registration scopes:
@@ -192,29 +192,31 @@ Agent middleware intercepts and modifies agent run execution. It uses the `Agent
192
192
193
193
-`agent`: The agent being invoked
194
194
-`messages`: List of chat messages in the conversation
195
-
-`is_streaming`: Boolean indicating if the response is streaming
195
+
-`session`: The current agent session, if any
196
+
-`options`: Agent run options for this invocation
197
+
-`stream`: Boolean indicating if the response is streaming
196
198
-`metadata`: Dictionary for storing additional data between middleware
197
199
-`result`: The agent's response (can be modified)
198
-
-`terminate`: Flag to stop further processing
199
-
-`kwargs`: Additional keyword arguments passed to the agent run method
200
+
-`kwargs`: Legacy runtime keyword arguments passed to the agent run method
201
+
-`client_kwargs`: Client-specific runtime values for downstream chat clients
202
+
-`function_invocation_kwargs`: Runtime values that will be forwarded to tools
200
203
201
-
The `next` callable continues the middleware chain or executes the agent if it's the last middleware.
204
+
The `call_next` callback continues the middleware chain or executes the agent if it's the last middleware.
202
205
203
206
### Function-based
204
207
205
208
```python
206
-
asyncdeflogging_agent_middleware(
209
+
asyncdefinject_tool_runtime_defaults(
207
210
context: AgentContext,
208
-
next: Callable[[AgentContext], Awaitable[None]],
211
+
call_next: Callable[[], Awaitable[None]],
209
212
) -> None:
210
-
"""Agent middleware that logs execution timing."""
211
-
# Pre-processing: Log before agent execution
213
+
"""Agent middleware that sets tool-only runtime defaults."""
@@ -407,30 +405,34 @@ async with AzureAIAgentClient(credential=credential).as_agent(
407
405
408
406
## Middleware Termination
409
407
410
-
Middleware can terminate execution early using `context.terminate`. This is useful for security checks, rate limiting, or validation failures.
408
+
Middleware can terminate execution early by setting `context.result` and raising `MiddlewareTermination`. This is useful for security checks, rate limiting, or validation failures.
411
409
412
410
```python
411
+
from agent_framework import AgentContext, AgentResponse, Message, MiddlewareTermination
412
+
413
413
asyncdefblocking_middleware(
414
414
context: AgentContext,
415
-
next: Callable[[AgentContext], Awaitable[None]],
415
+
call_next: Callable[[], Awaitable[None]],
416
416
) -> None:
417
417
"""Middleware that blocks execution based on conditions."""
418
418
# Check for blocked content
419
419
last_message = context.messages[-1] if context.messages elseNone
420
420
if last_message and last_message.text:
421
421
if"blocked"in last_message.text.lower():
422
422
print("Request blocked by middleware")
423
-
context.terminate =True
424
-
return
423
+
context.result = AgentResponse(
424
+
messages=[Message(role="assistant", text="This request was blocked by middleware.")]
0 commit comments