@@ -263,10 +263,10 @@ async def test_handoff_span(sentry_init, capture_events, mock_usage):
263263
264264 (transaction ,) = events
265265 spans = transaction ["spans" ]
266- handoff_span = spans [2 ]
267266
268- # Verify handoff span was created
269- assert handoff_span is not None
267+ # There should be exactly one handoff span
268+ (handoff_span ,) = [span for span in spans if span ["op" ] == "gen_ai.handoff" ]
269+
270270 assert (
271271 handoff_span ["description" ] == "handoff from primary_agent to secondary_agent"
272272 )
@@ -354,12 +354,25 @@ def simple_test_tool(message: str) -> str:
354354
355355 (transaction ,) = events
356356 spans = transaction ["spans" ]
357- (
358- agent_span ,
359- ai_client_span1 ,
360- tool_span ,
361- ai_client_span2 ,
362- ) = spans
357+
358+ assert len (spans ) == 4
359+
360+ # Find each span by its characteristics
361+ agent_span = next (s for s in spans if s ["description" ] == "invoke_agent test_agent" )
362+ tool_span = next (
363+ s for s in spans if s ["description" ] == "execute_tool simple_test_tool"
364+ )
365+ ai_client_span1 = next (
366+ s
367+ for s in spans
368+ if s ["description" ] == "chat gpt-4"
369+ and "gen_ai.response.tool_calls" in s ["data" ]
370+ )
371+ ai_client_span2 = next (
372+ s
373+ for s in spans
374+ if s ["description" ] == "chat gpt-4" and "gen_ai.response.text" in s ["data" ]
375+ )
363376
364377 available_tools = safe_serialize (
365378 [
@@ -383,7 +396,6 @@ def simple_test_tool(message: str) -> str:
383396 assert transaction ["transaction" ] == "test_agent workflow"
384397 assert transaction ["contexts" ]["trace" ]["origin" ] == "auto.ai.openai_agents"
385398
386- assert agent_span ["description" ] == "invoke_agent test_agent"
387399 assert agent_span ["origin" ] == "auto.ai.openai_agents"
388400 assert agent_span ["data" ]["gen_ai.agent.name" ] == "test_agent"
389401 assert agent_span ["data" ]["gen_ai.operation.name" ] == "invoke_agent"
@@ -394,7 +406,6 @@ def simple_test_tool(message: str) -> str:
394406 assert agent_span ["data" ]["gen_ai.request.top_p" ] == 1.0
395407 assert agent_span ["data" ]["gen_ai.system" ] == "openai"
396408
397- assert ai_client_span1 ["description" ] == "chat gpt-4"
398409 assert ai_client_span1 ["data" ]["gen_ai.operation.name" ] == "chat"
399410 assert ai_client_span1 ["data" ]["gen_ai.system" ] == "openai"
400411 assert ai_client_span1 ["data" ]["gen_ai.agent.name" ] == "test_agent"
@@ -442,7 +453,6 @@ def simple_test_tool(message: str) -> str:
442453 ]
443454 )
444455
445- assert tool_span ["description" ] == "execute_tool simple_test_tool"
446456 assert tool_span ["data" ]["gen_ai.agent.name" ] == "test_agent"
447457 assert tool_span ["data" ]["gen_ai.operation.name" ] == "execute_tool"
448458 assert (
@@ -464,7 +474,6 @@ def simple_test_tool(message: str) -> str:
464474 assert tool_span ["data" ]["gen_ai.tool.output" ] == "Tool executed with: hello"
465475 assert tool_span ["data" ]["gen_ai.tool.type" ] == "function"
466476
467- assert ai_client_span2 ["description" ] == "chat gpt-4"
468477 assert ai_client_span2 ["data" ]["gen_ai.agent.name" ] == "test_agent"
469478 assert ai_client_span2 ["data" ]["gen_ai.operation.name" ] == "chat"
470479 assert (
0 commit comments