@@ -212,6 +212,8 @@ def _end_span(
212212 status_description = error_message or str (error ) or type (error ).__name__
213213 span .set_status (StatusCode .ERROR , status_description )
214214 span .record_exception (error )
215+ elif error_message :
216+ span .set_status (StatusCode .ERROR , error_message )
215217 else :
216218 span .set_status (StatusCode .OK )
217219 except Exception as e :
@@ -460,15 +462,13 @@ def end_tool_call_span(self, span: Span, tool_result: ToolResult | None, error:
460462 error: Optional exception if the tool call failed.
461463 """
462464 attributes : dict [str , AttributeValue ] = {}
465+ status : str | None = None
466+ content : list [Any ] = []
467+
463468 if tool_result is not None :
464469 status = tool_result .get ("status" )
465- status_str = str (status ) if status is not None else ""
466-
467- attributes .update (
468- {
469- "gen_ai.tool.status" : status_str ,
470- }
471- )
470+ content = tool_result .get ("content" , [])
471+ attributes ["gen_ai.tool.status" ] = str (status ) if status is not None else ""
472472
473473 if self .use_latest_genai_conventions :
474474 self ._add_event (
@@ -483,7 +483,7 @@ def end_tool_call_span(self, span: Span, tool_result: ToolResult | None, error:
483483 {
484484 "type" : "tool_call_response" ,
485485 "id" : tool_result .get ("toolUseId" , "" ),
486- "response" : tool_result . get ( " content" ) ,
486+ "response" : content ,
487487 }
488488 ],
489489 }
@@ -497,12 +497,16 @@ def end_tool_call_span(self, span: Span, tool_result: ToolResult | None, error:
497497 span ,
498498 "gen_ai.choice" ,
499499 event_attributes = {
500- "message" : serialize (tool_result . get ( " content" ) ),
500+ "message" : serialize (content ),
501501 "id" : tool_result .get ("toolUseId" , "" ),
502502 },
503503 )
504504
505- self ._end_span (span , attributes , error )
505+ if error is None and status == "error" :
506+ error_message = next ((b ["text" ] for b in content if "text" in b ), "tool returned error status" )
507+ self ._end_span (span , attributes , error_message = error_message )
508+ else :
509+ self ._end_span (span , attributes , error )
506510
507511 def start_event_loop_cycle_span (
508512 self ,
@@ -527,9 +531,7 @@ def start_event_loop_cycle_span(
527531 event_loop_cycle_id = str (invocation_state .get ("event_loop_cycle_id" ))
528532 parent_span = parent_span if parent_span else invocation_state .get ("event_loop_parent_span" )
529533
530- attributes : dict [str , AttributeValue ] = self ._get_common_attributes (
531- operation_name = "execute_event_loop_cycle"
532- )
534+ attributes : dict [str , AttributeValue ] = self ._get_common_attributes (operation_name = "execute_event_loop_cycle" )
533535 attributes ["event_loop.cycle_id" ] = event_loop_cycle_id
534536
535537 if custom_trace_attributes :
0 commit comments