@@ -458,15 +458,18 @@ async def _stream_phase2(
458458
459459 # --- Model (streaming final response) ---
460460 yield self ._node_state ("model" , S )
461- with self .tracer .start_as_current_span (
461+ model_span = self .tracer .start_span (
462462 "model.2" ,
463463 attributes = {
464464 "uipath.step.kind" : "model" ,
465465 "uipath.output.reply.length" : len (reply ),
466466 },
467- ):
467+ )
468+ try :
468469 async for evt in self ._emit_streaming_response (reply ):
469470 yield evt
471+ finally :
472+ model_span .end ()
470473 yield self ._node_state ("model" , C )
471474
472475 # --- Middleware: TodoList (routes to __end__) ---
@@ -500,15 +503,18 @@ async def stream(
500503 self ._suspended_turn = None
501504 self ._suspended_message_id = None
502505
503- with self .tracer .start_as_current_span (
506+ resume_span = self .tracer .start_span (
504507 "support_chat.resume" ,
505508 attributes = {
506509 "uipath.runtime.name" : "SupportChatRuntime" ,
507510 "uipath.step.kind" : "resume" ,
508511 },
509- ):
512+ )
513+ try :
510514 async for evt in self ._stream_phase2 (turn ):
511515 yield evt
516+ finally :
517+ resume_span .end ()
512518 return
513519
514520 # --- Normal flow ---
@@ -531,7 +537,7 @@ async def stream(
531537 # Shared message_id for the assistant turn (tool calls + response)
532538 message_id = str (uuid4 ())
533539
534- with self .tracer .start_as_current_span (
540+ root_span = self .tracer .start_span (
535541 "support_chat.execute" ,
536542 attributes = {
537543 "uipath.runtime.name" : "SupportChatRuntime" ,
@@ -540,7 +546,8 @@ async def stream(
540546 "uipath.input.message.length" : len (message ),
541547 "uipath.input.turn_keywords" : "," .join (turn .get ("keywords" , [])),
542548 },
543- ):
549+ )
550+ try :
544551 # --- Middleware: PatchToolCalls ---
545552 yield self ._node_state ("PatchToolCallsMiddleware.before_agent" , S )
546553 with self .tracer .start_as_current_span (
@@ -567,14 +574,15 @@ async def stream(
567574
568575 # --- Model (first call — decides to use tools) ---
569576 yield self ._node_state ("model" , S )
570- with self .tracer .start_as_current_span (
577+ model_span = self .tracer .start_span (
571578 "model" ,
572579 attributes = {
573580 "uipath.step.kind" : "model" ,
574581 "uipath.output.tool_calls_count" : len (tool_defs ),
575582 "uipath.output.tool_names" : "," .join (t ["name" ] for t in tool_defs ),
576583 },
577- ):
584+ )
585+ try :
578586 # message_start for tool-call message
579587 yield UiPathRuntimeMessageEvent (
580588 payload = UiPathConversationMessageEvent (
@@ -588,6 +596,8 @@ async def stream(
588596 # Emit tool call events from the model
589597 async for evt in self ._emit_tool_calls (message_id , tool_defs ):
590598 yield evt
599+ finally :
600+ model_span .end ()
591601 yield self ._node_state ("model" , C )
592602
593603 # --- Middleware: TodoList (routes to tools) ---
@@ -658,15 +668,18 @@ async def stream(
658668
659669 # --- Model (second call — streaming final response with tool results) ---
660670 yield self ._node_state ("model" , S )
661- with self .tracer .start_as_current_span (
671+ model2_span = self .tracer .start_span (
662672 "model.2" ,
663673 attributes = {
664674 "uipath.step.kind" : "model" ,
665675 "uipath.output.reply.length" : len (reply ),
666676 },
667- ):
677+ )
678+ try :
668679 async for evt in self ._emit_streaming_response (reply ):
669680 yield evt
681+ finally :
682+ model2_span .end ()
670683 yield self ._node_state ("model" , C )
671684
672685 # --- Middleware: TodoList (routes to __end__) ---
@@ -680,6 +693,8 @@ async def stream(
680693 ):
681694 await asyncio .sleep (0.1 )
682695 yield self ._node_state ("TodoListMiddleware.after_model" , C )
696+ finally :
697+ root_span .end ()
683698
684699 yield UiPathRuntimeResult (
685700 output = {"reply" : reply },
0 commit comments