3434from pydantic import BaseModel , Field
3535from typing_extensions import Self
3636
37- from .._agents import _LOOP_ITERATION_ACTIVE
37+ from .._agents import _LOOP_ITERATION_TOKEN_KEY
3838from .._feature_stage import ExperimentalFeature , experimental
3939from .._middleware import AgentContext , AgentMiddleware , MiddlewareTermination
4040from .._sessions import SessionContext
@@ -518,57 +518,45 @@ async def _process_non_streaming(
518518 aggregated : list [Message ] = []
519519 aggregated_usage : UsageDetails | None = None
520520 final_result : AgentResponse | None = None
521- while True :
522- loop_token = _LOOP_ITERATION_ACTIVE .set (context .agent )
523- try :
521+ stamped_options = dict (context .options ) if context .options is not None else {}
522+ stamped_options [_LOOP_ITERATION_TOKEN_KEY ] = object ()
523+ context .options = stamped_options
524+ try :
525+ while True :
524526 await call_next ()
525- finally :
526- _LOOP_ITERATION_ACTIVE .reset (loop_token )
527- iteration += 1
528-
529- result = context .result
530- if not isinstance (result , AgentResponse ):
531- raise TypeError (
532- "AgentLoopMiddleware expected an AgentResponse from a non-streaming run, "
533- f"got { type (result ).__name__ } ."
534- )
527+ iteration += 1
535528
536- final_result = result
537- aggregated .extend (result .messages )
538- if result .usage_details is not None :
539- aggregated_usage = add_usage_details (aggregated_usage , result .usage_details )
540-
541- # Escape hatch: if this iteration is asking for tool approval, stop and return the
542- # response so the caller can approve, instead of continuing or injecting next_message.
543- if self ._has_pending_approval_request (result ):
544- break
545-
546- messages_used = context .messages
547- loop_kwargs = self ._build_loop_kwargs (
548- context = context ,
549- iteration = iteration ,
550- last_result = result ,
551- messages_used = messages_used ,
552- original_messages = original_messages ,
553- progress = progress ,
554- )
529+ result = context .result
530+ if not isinstance (result , AgentResponse ):
531+ raise TypeError (
532+ "AgentLoopMiddleware expected an AgentResponse from a non-streaming run, "
533+ f"got { type (result ).__name__ } ."
534+ )
555535
556- work_iterations += 1
557- # Decide whether to stop and capture any feedback from should_continue first, so the
558- # feedback is available to both the progress and next-message callables this iteration.
559- stop , feedback = await self ._evaluate_stop (loop_kwargs , work_iterations )
560- loop_kwargs = self ._build_loop_kwargs (
561- context = context ,
562- iteration = iteration ,
563- last_result = result ,
564- messages_used = messages_used ,
565- original_messages = original_messages ,
566- progress = progress ,
567- feedback = feedback ,
568- )
569- # Capture this iteration's progress entry, then refresh loop_kwargs so the next-message
570- # resolution sees the latest entry.
571- if await self ._record_progress (result , loop_kwargs , progress ):
536+ final_result = result
537+ aggregated .extend (result .messages )
538+ if result .usage_details is not None :
539+ aggregated_usage = add_usage_details (aggregated_usage , result .usage_details )
540+
541+ # Escape hatch: if this iteration is asking for tool approval, stop and return the
542+ # response so the caller can approve, instead of continuing or injecting next_message.
543+ if self ._has_pending_approval_request (result ):
544+ break
545+
546+ messages_used = context .messages
547+ loop_kwargs = self ._build_loop_kwargs (
548+ context = context ,
549+ iteration = iteration ,
550+ last_result = result ,
551+ messages_used = messages_used ,
552+ original_messages = original_messages ,
553+ progress = progress ,
554+ )
555+
556+ work_iterations += 1
557+ # Decide whether to stop and capture any feedback from should_continue first, so the
558+ # feedback is available to both the progress and next-message callables this iteration.
559+ stop , feedback = await self ._evaluate_stop (loop_kwargs , work_iterations )
572560 loop_kwargs = self ._build_loop_kwargs (
573561 context = context ,
574562 iteration = iteration ,
@@ -578,15 +566,29 @@ async def _process_non_streaming(
578566 progress = progress ,
579567 feedback = feedback ,
580568 )
581- if stop :
582- break
583- if snapshot is not None and context .session is not None :
584- # Reset the session to the pre-loop baseline so the next run starts fresh; only the
585- # progress log (injected by _resolve_next_message) carries continuity forward.
586- self ._restore_session (context .session , snapshot )
587- next_messages = await self ._resolve_next_message (loop_kwargs , messages_used , original_messages )
588- context .messages = next_messages
589- aggregated .extend (next_messages )
569+ # Capture this iteration's progress entry, then refresh loop_kwargs so the next-message
570+ # resolution sees the latest entry.
571+ if await self ._record_progress (result , loop_kwargs , progress ):
572+ loop_kwargs = self ._build_loop_kwargs (
573+ context = context ,
574+ iteration = iteration ,
575+ last_result = result ,
576+ messages_used = messages_used ,
577+ original_messages = original_messages ,
578+ progress = progress ,
579+ feedback = feedback ,
580+ )
581+ if stop :
582+ break
583+ if snapshot is not None and context .session is not None :
584+ # Reset the session to the pre-loop baseline so the next run starts fresh; only the
585+ # progress log (injected by _resolve_next_message) carries continuity forward.
586+ self ._restore_session (context .session , snapshot )
587+ next_messages = await self ._resolve_next_message (loop_kwargs , messages_used , original_messages )
588+ context .messages = next_messages
589+ aggregated .extend (next_messages )
590+ finally :
591+ context .options .pop (_LOOP_ITERATION_TOKEN_KEY , None )
590592
591593 if not self .return_final_only :
592594 context .result = self ._aggregate_response (final_result , aggregated , aggregated_usage )
@@ -611,9 +613,11 @@ async def _generator() -> Any:
611613 iteration = 0
612614 work_iterations = 0
613615 progress : list [str ] = []
616+ stamped_options = dict (context .options ) if context .options is not None else {}
617+ stamped_options [_LOOP_ITERATION_TOKEN_KEY ] = object ()
618+ context .options = stamped_options
614619 try :
615620 while True :
616- loop_token = _LOOP_ITERATION_ACTIVE .set (context .agent )
617621 try :
618622 await call_next ()
619623 inner = context .result
@@ -633,8 +637,6 @@ async def _generator() -> Any:
633637 # raised by a downstream middleware or during stream consumption surfaces here.
634638 # Stop cleanly and keep whatever final response we have from a prior iteration.
635639 return
636- finally :
637- _LOOP_ITERATION_ACTIVE .reset (loop_token )
638640
639641 iteration += 1
640642
@@ -691,6 +693,7 @@ async def _generator() -> Any:
691693 for message in next_messages :
692694 yield self ._message_to_update (message )
693695 finally :
696+ context .options .pop (_LOOP_ITERATION_TOKEN_KEY , None )
694697 await self ._fire_turn_scoped_after_providers (context , holder ["final" ], original_messages )
695698
696699 def _finalize (updates : Sequence [AgentResponseUpdate ]) -> AgentResponse :
0 commit comments