@@ -194,10 +194,10 @@ async def logging_agent_middleware(
194194 """ Agent middleware that logs execution timing."""
195195 # Pre-processing: Log before agent execution
196196 print (" [Agent] Starting execution" )
197-
197+
198198 # Continue to next middleware or agent execution
199199 await next (context)
200-
200+
201201 # Post-processing: Log after agent execution
202202 print (" [Agent] Execution completed" )
203203```
@@ -225,10 +225,10 @@ async def logging_function_middleware(
225225 """ Function middleware that logs function execution."""
226226 # Pre-processing: Log before function execution
227227 print (f " [Function] Calling { context.function.name} " )
228-
228+
229229 # Continue to next middleware or function execution
230230 await next (context)
231-
231+
232232 # Post-processing: Log after function execution
233233 print (f " [Function] { context.function.name} completed " )
234234```
@@ -258,10 +258,10 @@ async def logging_chat_middleware(
258258 """ Chat middleware that logs AI interactions."""
259259 # Pre-processing: Log before AI call
260260 print (f " [Chat] Sending { len (context.messages)} messages to AI " )
261-
261+
262262 # Continue to next middleware or AI service
263263 await next (context)
264-
264+
265265 # Post-processing: Log after AI response
266266 print (" [Chat] AI response received" )
267267```
@@ -312,18 +312,18 @@ from agent_framework import AgentMiddleware, AgentRunContext
312312
313313class LoggingAgentMiddleware (AgentMiddleware ):
314314 """ Agent middleware that logs execution."""
315-
315+
316316 async def process (
317317 self ,
318318 context : AgentRunContext,
319319 next : Callable[[AgentRunContext], Awaitable[None ]],
320320 ) -> None :
321321 # Pre-processing: Log before agent execution
322322 print (" [Agent Class] Starting execution" )
323-
323+
324324 # Continue to next middleware or agent execution
325325 await next (context)
326-
326+
327327 # Post-processing: Log after agent execution
328328 print (" [Agent Class] Execution completed" )
329329```
@@ -337,18 +337,18 @@ from agent_framework import FunctionMiddleware, FunctionInvocationContext
337337
338338class LoggingFunctionMiddleware (FunctionMiddleware ):
339339 """ Function middleware that logs function execution."""
340-
340+
341341 async def process (
342342 self ,
343343 context : FunctionInvocationContext,
344344 next : Callable[[FunctionInvocationContext], Awaitable[None ]],
345345 ) -> None :
346346 # Pre-processing: Log before function execution
347347 print (f " [Function Class] Calling { context.function.name} " )
348-
348+
349349 # Continue to next middleware or function execution
350350 await next (context)
351-
351+
352352 # Post-processing: Log after function execution
353353 print (f " [Function Class] { context.function.name} completed " )
354354```
@@ -362,18 +362,18 @@ from agent_framework import ChatMiddleware, ChatContext
362362
363363class LoggingChatMiddleware (ChatMiddleware ):
364364 """ Chat middleware that logs AI interactions."""
365-
365+
366366 async def process (
367367 self ,
368368 context : ChatContext,
369369 next : Callable[[ChatContext], Awaitable[None ]],
370370 ) -> None :
371371 # Pre-processing: Log before AI call
372372 print (f " [Chat Class] Sending { len (context.messages)} messages to AI " )
373-
373+
374374 # Continue to next middleware or AI service
375375 await next (context)
376-
376+
377377 # Post-processing: Log after AI response
378378 print (" [Chat Class] AI response received" )
379379```
@@ -398,18 +398,18 @@ async with AzureAIAgentClient(async_credential=credential).create_agent(
398398 TimingFunctionMiddleware(), # Applies to all runs
399399 ],
400400) as agent:
401-
401+
402402 # This run uses agent-level middleware only
403403 result1 = await agent.run(" What's the weather in Seattle?" )
404-
404+
405405 # This run uses agent-level + run-level middleware
406406 result2 = await agent.run(
407407 " What's the weather in Portland?" ,
408408 middleware = [ # Run-level middleware (this run only)
409409 logging_chat_middleware,
410410 ]
411411 )
412-
412+
413413 # This run uses agent-level middleware only (no run-level)
414414 result3 = await agent.run(" What's the weather in Vancouver?" )
415415```
@@ -436,7 +436,7 @@ async def blocking_middleware(
436436 print (" Request blocked by middleware" )
437437 context.terminate = True
438438 return
439-
439+
440440 # If no issues, continue normally
441441 await next (context)
442442```
@@ -458,14 +458,14 @@ You can use `context.is_streaming` to differentiate between these scenarios and
458458
459459``` python
460460async def weather_override_middleware (
461- context : AgentRunContext,
461+ context : AgentRunContext,
462462 next : Callable[[AgentRunContext], Awaitable[None ]]
463463) -> None :
464464 """ Middleware that overrides weather results for both streaming and non-streaming."""
465-
465+
466466 # Execute the original agent logic
467467 await next (context)
468-
468+
469469 # Override results if present
470470 if context.result is not None :
471471 custom_message_parts = [
@@ -474,13 +474,13 @@ async def weather_override_middleware(
474474 " 22°C with gentle breezes. " ,
475475 " Great day for outdoor activities!"
476476 ]
477-
477+
478478 if context.is_streaming:
479479 # Streaming override
480480 async def override_stream () -> AsyncIterable[AgentRunResponseUpdate]:
481481 for chunk in custom_message_parts:
482482 yield AgentRunResponseUpdate(contents = [TextContent(text = chunk)])
483-
483+
484484 context.result = override_stream()
485485 else :
486486 # Non-streaming override
@@ -497,4 +497,4 @@ This middleware approach allows you to implement sophisticated response transfor
497497## Next steps
498498
499499> [ !div class="nextstepaction"]
500- > [ Agent Retrieval Augmented Generation (RAG) ] ( ./agent-rag .md )
500+ > [ Agent Background Responses ] ( ./agent-background-responses .md )
0 commit comments