@@ -673,7 +673,8 @@ async def _execute_node(self, node: GraphNode, invocation_state: dict[str, Any])
673673 # Execute with timeout protection and stream events
674674 try :
675675 if isinstance (node .executor , MultiAgentBase ):
676- # For nested multi-agent systems, stream their events
676+ # For nested multi-agent systems, stream their events and collect result
677+ multi_agent_result = None
677678 if self .node_timeout is not None :
678679 # Implement timeout for async generator streaming
679680 async for event in self ._stream_with_timeout (
@@ -684,14 +685,22 @@ async def _execute_node(self, node: GraphNode, invocation_state: dict[str, Any])
684685 # Forward nested multi-agent events with node context
685686 wrapped_event = MultiAgentNodeStreamEvent (node .node_id , event )
686687 yield wrapped_event .as_dict ()
688+ # Capture the final result event
689+ if "result" in event :
690+ multi_agent_result = event ["result" ]
687691 else :
688692 async for event in node .executor .stream_async (node_input , invocation_state ):
689693 # Forward nested multi-agent events with node context
690694 wrapped_event = MultiAgentNodeStreamEvent (node .node_id , event )
691695 yield wrapped_event .as_dict ()
696+ # Capture the final result event
697+ if "result" in event :
698+ multi_agent_result = event ["result" ]
699+
700+ # Use the captured result from streaming (no double execution)
701+ if multi_agent_result is None :
702+ raise ValueError (f"Node '{ node .node_id } ' did not produce a result event" )
692703
693- # Get the final result for metrics
694- multi_agent_result = await node .executor .invoke_async (node_input , invocation_state )
695704 node_result = NodeResult (
696705 result = multi_agent_result ,
697706 execution_time = multi_agent_result .execution_time ,
@@ -702,7 +711,8 @@ async def _execute_node(self, node: GraphNode, invocation_state: dict[str, Any])
702711 )
703712
704713 elif isinstance (node .executor , Agent ):
705- # For agents, stream their events
714+ # For agents, stream their events and collect result
715+ agent_response = None
706716 if self .node_timeout is not None :
707717 # Implement timeout for async generator streaming
708718 async for event in self ._stream_with_timeout (
@@ -713,14 +723,22 @@ async def _execute_node(self, node: GraphNode, invocation_state: dict[str, Any])
713723 # Forward agent events with node context
714724 wrapped_event = MultiAgentNodeStreamEvent (node .node_id , event )
715725 yield wrapped_event .as_dict ()
726+ # Capture the final result event
727+ if "result" in event :
728+ agent_response = event ["result" ]
716729 else :
717730 async for event in node .executor .stream_async (node_input , ** invocation_state ):
718731 # Forward agent events with node context
719732 wrapped_event = MultiAgentNodeStreamEvent (node .node_id , event )
720733 yield wrapped_event .as_dict ()
734+ # Capture the final result event
735+ if "result" in event :
736+ agent_response = event ["result" ]
737+
738+ # Use the captured result from streaming (no double execution)
739+ if agent_response is None :
740+ raise ValueError (f"Node '{ node .node_id } ' did not produce a result event" )
721741
722- # Get the final result for metrics
723- agent_response = await node .executor .invoke_async (node_input , ** invocation_state )
724742 usage = Usage (inputTokens = 0 , outputTokens = 0 , totalTokens = 0 )
725743 metrics = Metrics (latencyMs = 0 )
726744 if hasattr (agent_response , "metrics" ) and agent_response .metrics :
0 commit comments