@@ -493,7 +493,9 @@ def test_enrich_gen_ai_agent_name_from_immediate_parent() -> None:
493493 parent_span_id = "aaaaaaaaaaaaaaaa" ,
494494 start_timestamp = 1609455601.0 ,
495495 end_timestamp = 1609455602.0 ,
496- span_op = "gen_ai.execute_tool" ,
496+ attributes = {
497+ "gen_ai.operation.name" : {"type" : "string" , "value" : "execute_tool" },
498+ },
497499 )
498500
499501 spans = [parent_span , child_span ]
@@ -525,9 +527,9 @@ def test_enrich_gen_ai_agent_name_not_overwritten() -> None:
525527 parent_span_id = "aaaaaaaaaaaaaaaa" ,
526528 start_timestamp = 1609455601.0 ,
527529 end_timestamp = 1609455602.0 ,
528- span_op = "gen_ai.handoff" ,
529530 attributes = {
530531 "gen_ai.agent.name" : {"type" : "string" , "value" : "ChildAgent" },
532+ "gen_ai.operation.name" : {"type" : "string" , "value" : "handoff" },
531533 },
532534 )
533535
@@ -557,7 +559,9 @@ def test_enrich_gen_ai_agent_name_not_set_without_ancestor() -> None:
557559 parent_span_id = "aaaaaaaaaaaaaaaa" ,
558560 start_timestamp = 1609455601.0 ,
559561 end_timestamp = 1609455602.0 ,
560- span_op = "gen_ai.execute_tool" ,
562+ attributes = {
563+ "gen_ai.operation.name" : {"type" : "string" , "value" : "execute_tool" },
564+ },
561565 )
562566
563567 spans = [parent_span , child_span ]
@@ -598,7 +602,9 @@ def test_enrich_gen_ai_agent_name_not_from_sibling() -> None:
598602 parent_span_id = "aaaaaaaaaaaaaaaa" ,
599603 start_timestamp = 1609455602.5 ,
600604 end_timestamp = 1609455603.5 ,
601- span_op = "gen_ai.execute_tool" ,
605+ attributes = {
606+ "gen_ai.operation.name" : {"type" : "string" , "value" : "execute_tool" },
607+ },
602608 )
603609
604610 spans = [parent_span , sibling_with_agent , target_child ]
@@ -631,7 +637,9 @@ def test_enrich_gen_ai_agent_name_only_from_invoke_agent_parent() -> None:
631637 parent_span_id = "aaaaaaaaaaaaaaaa" ,
632638 start_timestamp = 1609455601.0 ,
633639 end_timestamp = 1609455602.0 ,
634- span_op = "gen_ai.run" ,
640+ attributes = {
641+ "gen_ai.operation.name" : {"type" : "string" , "value" : "run" },
642+ },
635643 )
636644
637645 spans = [parent_span , child_span ]
@@ -672,7 +680,9 @@ def test_enrich_gen_ai_agent_name_not_from_grandparent() -> None:
672680 parent_span_id = "bbbbbbbbbbbbbbbb" ,
673681 start_timestamp = 1609455602.0 ,
674682 end_timestamp = 1609455603.0 ,
675- span_op = "gen_ai.run" ,
683+ attributes = {
684+ "gen_ai.operation.name" : {"type" : "string" , "value" : "run" },
685+ },
676686 )
677687
678688 spans = [grandparent_span , parent_span , child_span ]
@@ -683,3 +693,35 @@ def test_enrich_gen_ai_agent_name_not_from_grandparent() -> None:
683693 assert attribute_value (grandparent , "gen_ai.agent.name" ) == "GrandparentAgent"
684694 assert attribute_value (parent , "gen_ai.agent.name" ) is None
685695 assert attribute_value (child , "gen_ai.agent.name" ) is None
696+
697+
698+ def test_enrich_gen_ai_agent_name_from_immediate_parent_fallback_to_span_op () -> None :
699+ """Test that gen_ai.agent.name is inherited from the immediate parent with gen_ai.invoke_agent operation."""
700+ parent_span = build_mock_span (
701+ project_id = 1 ,
702+ is_segment = True ,
703+ span_id = "aaaaaaaaaaaaaaaa" ,
704+ start_timestamp = 1609455600.0 ,
705+ end_timestamp = 1609455605.0 ,
706+ span_op = "gen_ai.invoke_agent" ,
707+ attributes = {
708+ "gen_ai.agent.name" : {"type" : "string" , "value" : "MyAgent" },
709+ },
710+ )
711+
712+ child_span = build_mock_span (
713+ project_id = 1 ,
714+ span_id = "bbbbbbbbbbbbbbbb" ,
715+ parent_span_id = "aaaaaaaaaaaaaaaa" ,
716+ start_timestamp = 1609455601.0 ,
717+ end_timestamp = 1609455602.0 ,
718+ span_op = "gen_ai.execute_tool" ,
719+ )
720+
721+ spans = [parent_span , child_span ]
722+ _ , enriched_spans = TreeEnricher .enrich_spans (spans )
723+ compatible_spans = [make_compatible (span ) for span in enriched_spans ]
724+
725+ parent , child = compatible_spans
726+ assert attribute_value (parent , "gen_ai.agent.name" ) == "MyAgent"
727+ assert attribute_value (child , "gen_ai.agent.name" ) == "MyAgent"
0 commit comments