@@ -102,11 +102,13 @@ async def patched_execute_handoffs(cls, *args, **kwargs):
102102 handoff_span (context_wrapper , agent , handoff_agent_name )
103103
104104 # Call original method with all parameters
105- result = await original_execute_handoffs (* args , ** kwargs )
105+ try :
106+ result = await original_execute_handoffs (* args , ** kwargs )
106107
107- # End span for current agent after handoff processing is complete
108- if agent and context_wrapper and _has_active_agent_span (context_wrapper ):
109- _end_invoke_agent_span (context_wrapper , agent )
108+ finally :
109+ # End span for current agent after handoff processing is complete
110+ if agent and context_wrapper and _has_active_agent_span (context_wrapper ):
111+ _end_invoke_agent_span (context_wrapper , agent )
110112
111113 return result
112114
@@ -124,11 +126,12 @@ async def patched_execute_final_output(cls, *args, **kwargs):
124126 final_output = kwargs .get ("final_output" )
125127
126128 # Call original method with all parameters
127- result = await original_execute_final_output (* args , ** kwargs )
128-
129- # End span for current agent after final output processing is complete
130- if agent and context_wrapper and _has_active_agent_span (context_wrapper ):
131- _end_invoke_agent_span (context_wrapper , agent , final_output )
129+ try :
130+ result = await original_execute_final_output (* args , ** kwargs )
131+ finally :
132+ # End span for current agent after final output processing is complete
133+ if agent and context_wrapper and _has_active_agent_span (context_wrapper ):
134+ _end_invoke_agent_span (context_wrapper , agent , final_output )
132135
133136 return result
134137
0 commit comments