77from dataclasses import dataclass , field
88from typing import Any , Literal , TypeVar , cast
99
10- from ._run_impl import NextStepInterruption , ProcessedResponse , QueueCompleteSentinel
1110from .agent import Agent
1211from .agent_output import AgentOutputSchemaBase
1312from .exceptions import (
2625)
2726from .logger import logger
2827from .run_context import RunContextWrapper
28+ from .run_internal .run_steps import (
29+ NextStepInterruption ,
30+ ProcessedResponse ,
31+ QueueCompleteSentinel ,
32+ )
2933from .run_state import RunState
3034from .stream_events import StreamEvent
3135from .tool_guardrails import ToolInputGuardrailResult , ToolOutputGuardrailResult
@@ -306,7 +310,7 @@ class RunResultStreaming(RunResultBase):
306310 )
307311
308312 # Store the asyncio tasks that we're waiting on
309- _run_impl_task : asyncio .Task [Any ] | None = field (default = None , repr = False )
313+ run_loop_task : asyncio .Task [Any ] | None = field (default = None , repr = False )
310314 _input_guardrails_task : asyncio .Task [Any ] | None = field (default = None , repr = False )
311315 _output_guardrails_task : asyncio .Task [Any ] | None = field (default = None , repr = False )
312316 _stored_exception : Exception | None = field (default = None , repr = False )
@@ -462,7 +466,7 @@ async def stream_events(self) -> AsyncIterator[StreamEvent]:
462466 else :
463467 # Ensure main execution completes before cleanup to avoid race conditions
464468 # with session operations
465- await self ._await_task_safely (self ._run_impl_task )
469+ await self ._await_task_safely (self .run_loop_task )
466470 # Safely terminate all background tasks after main execution has finished
467471 self ._cleanup_tasks ()
468472
@@ -504,9 +508,9 @@ def _check_errors(self):
504508 self ._stored_exception = tripwire_exc
505509
506510 # Check the tasks for any exceptions
507- if self ._run_impl_task and self ._run_impl_task .done ():
508- if not self ._run_impl_task .cancelled ():
509- run_impl_exc = self ._run_impl_task .exception ()
511+ if self .run_loop_task and self .run_loop_task .done ():
512+ if not self .run_loop_task .cancelled ():
513+ run_impl_exc = self .run_loop_task .exception ()
510514 if run_impl_exc and isinstance (run_impl_exc , Exception ):
511515 if isinstance (run_impl_exc , AgentsException ) and run_impl_exc .run_data is None :
512516 run_impl_exc .run_data = self ._create_error_details ()
@@ -532,8 +536,8 @@ def _check_errors(self):
532536 self ._stored_exception = out_guard_exc
533537
534538 def _cleanup_tasks (self ):
535- if self ._run_impl_task and not self ._run_impl_task .done ():
536- self ._run_impl_task .cancel ()
539+ if self .run_loop_task and not self .run_loop_task .done ():
540+ self .run_loop_task .cancel ()
537541
538542 if self ._input_guardrails_task and not self ._input_guardrails_task .done ():
539543 self ._input_guardrails_task .cancel ()
0 commit comments