Skip to content

Commit 93ea639

Browse files
authored
Remove choice wrapper from response events in OpenAI Agents instrumentation (#1094)
1 parent c0706fd commit 93ea639

File tree

3 files changed

+67
-107
lines changed

3 files changed

+67
-107
lines changed

logfire/_internal/integrations/openai_agents.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -426,14 +426,7 @@ def get_response_span_events(span: ResponseSpanData):
426426
if response and response.output:
427427
for out in response.output:
428428
for message in input_to_events(out.model_dump(), tool_call_id_to_name):
429-
message.pop('event.name', None)
430-
events.append(
431-
{
432-
'event.name': 'gen_ai.choice',
433-
'index': 0,
434-
'message': {**message, 'role': 'assistant'},
435-
},
436-
)
429+
events.append({**message, 'role': 'assistant'})
437430
return events
438431

439432

tests/otel_integrations/test_openai_agents.py

Lines changed: 54 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -545,32 +545,26 @@ def random_number() -> int:
545545
'role': 'user',
546546
},
547547
{
548-
'event.name': 'gen_ai.choice',
549-
'index': 0,
550-
'message': {
551-
'role': 'assistant',
552-
'tool_calls': [
553-
{
554-
'id': 'call_vwqy7HyGGnNht9NNfxMnnouY',
555-
'type': 'function',
556-
'function': {'name': 'random_number', 'arguments': '{}'},
557-
}
558-
],
559-
},
548+
'event.name': 'gen_ai.assistant.message',
549+
'role': 'assistant',
550+
'tool_calls': [
551+
{
552+
'id': 'call_vwqy7HyGGnNht9NNfxMnnouY',
553+
'type': 'function',
554+
'function': {'name': 'random_number', 'arguments': '{}'},
555+
}
556+
],
560557
},
561558
{
562-
'event.name': 'gen_ai.choice',
563-
'index': 0,
564-
'message': {
565-
'role': 'assistant',
566-
'tool_calls': [
567-
{
568-
'id': 'call_oEA0MnUXCwKevx8txteoopNL',
569-
'type': 'function',
570-
'function': {'name': 'transfer_to_agent2', 'arguments': '{}'},
571-
}
572-
],
573-
},
559+
'event.name': 'gen_ai.assistant.message',
560+
'role': 'assistant',
561+
'tool_calls': [
562+
{
563+
'id': 'call_oEA0MnUXCwKevx8txteoopNL',
564+
'type': 'function',
565+
'function': {'name': 'transfer_to_agent2', 'arguments': '{}'},
566+
}
567+
],
574568
},
575569
],
576570
'logfire.json_schema': {
@@ -880,12 +874,9 @@ def random_number() -> int:
880874
'name': 'transfer_to_agent2',
881875
},
882876
{
883-
'event.name': 'gen_ai.choice',
884-
'index': 0,
885-
'message': {
886-
'role': 'assistant',
887-
'content': "The random number generated is 4, and it's been handed off to agent2.",
888-
},
877+
'event.name': 'gen_ai.assistant.message',
878+
'content': "The random number generated is 4, and it's been handed off to agent2.",
879+
'role': 'assistant',
889880
},
890881
],
891882
'gen_ai.usage.input_tokens': 89,
@@ -1153,11 +1144,7 @@ async def zero_guardrail(_context: Any, _agent: Agent[Any], inp: Any) -> Guardra
11531144
'raw_input': [{'content': '1+1?', 'role': 'user'}],
11541145
'events': [
11551146
{'event.name': 'gen_ai.user.message', 'content': '1+1?', 'role': 'user'},
1156-
{
1157-
'event.name': 'gen_ai.choice',
1158-
'index': 0,
1159-
'message': {'content': '1 + 1 equals 2.', 'role': 'assistant'},
1160-
},
1147+
{'event.name': 'gen_ai.assistant.message', 'content': '1 + 1 equals 2.', 'role': 'assistant'},
11611148
],
11621149
'gen_ai.usage.input_tokens': 29,
11631150
'gen_ai.usage.output_tokens': 9,
@@ -1841,11 +1828,7 @@ async def test_responses_simple(exporter: TestExporter):
18411828
'raw_input': [{'content': '2+2?', 'role': 'user'}],
18421829
'events': [
18431830
{'event.name': 'gen_ai.user.message', 'content': '2+2?', 'role': 'user'},
1844-
{
1845-
'event.name': 'gen_ai.choice',
1846-
'index': 0,
1847-
'message': {'content': '2 + 2 equals 4.', 'role': 'assistant'},
1848-
},
1831+
{'event.name': 'gen_ai.assistant.message', 'content': '2 + 2 equals 4.', 'role': 'assistant'},
18491832
],
18501833
'gen_ai.usage.input_tokens': 29,
18511834
'gen_ai.usage.output_tokens': 9,
@@ -2048,9 +2031,9 @@ async def test_responses_simple(exporter: TestExporter):
20482031
{'event.name': 'gen_ai.assistant.message', 'content': '2 + 2 equals 4.', 'role': 'assistant'},
20492032
{'event.name': 'gen_ai.user.message', 'content': '4?', 'role': 'user'},
20502033
{
2051-
'event.name': 'gen_ai.choice',
2052-
'index': 0,
2053-
'message': {'content': 'Yes, 2 + 2 equals 4.', 'role': 'assistant'},
2034+
'event.name': 'gen_ai.assistant.message',
2035+
'content': 'Yes, 2 + 2 equals 4.',
2036+
'role': 'assistant',
20542037
},
20552038
],
20562039
'gen_ai.usage.input_tokens': 47,
@@ -2318,28 +2301,25 @@ async def test_file_search(exporter: TestExporter):
23182301
'events': [
23192302
{'event.name': 'gen_ai.user.message', 'content': 'Who made Logfire?', 'role': 'user'},
23202303
{
2321-
'event.name': 'gen_ai.choice',
2322-
'index': 0,
2323-
'message': {
2324-
'role': 'assistant',
2325-
'content': """\
2304+
'event.name': 'gen_ai.unknown',
2305+
'role': 'assistant',
2306+
'content': """\
23262307
file_search_call
23272308
23282309
See JSON for details\
23292310
""",
2330-
'data': {
2331-
'id': 'fs_67ceff3ab5b081919945a1b5a1185949',
2332-
'queries': ['Who made Logfire?'],
2333-
'status': 'completed',
2334-
'type': 'file_search_call',
2335-
'results': None,
2336-
},
2311+
'data': {
2312+
'id': 'fs_67ceff3ab5b081919945a1b5a1185949',
2313+
'queries': ['Who made Logfire?'],
2314+
'status': 'completed',
2315+
'type': 'file_search_call',
2316+
'results': None,
23372317
},
23382318
},
23392319
{
2340-
'event.name': 'gen_ai.choice',
2341-
'index': 0,
2342-
'message': {'content': 'Logfire is made by Pydantic.', 'role': 'assistant'},
2320+
'event.name': 'gen_ai.assistant.message',
2321+
'content': 'Logfire is made by Pydantic.',
2322+
'role': 'assistant',
23432323
},
23442324
],
23452325
'gen_ai.usage.input_tokens': 1974,
@@ -2622,11 +2602,7 @@ async def test_file_search(exporter: TestExporter):
26222602
'role': 'assistant',
26232603
},
26242604
{'event.name': 'gen_ai.user.message', 'content': '2+2?', 'role': 'user'},
2625-
{
2626-
'event.name': 'gen_ai.choice',
2627-
'index': 0,
2628-
'message': {'content': 'The answer is 4.', 'role': 'assistant'},
2629-
},
2605+
{'event.name': 'gen_ai.assistant.message', 'content': 'The answer is 4.', 'role': 'assistant'},
26302606
],
26312607
'gen_ai.usage.input_tokens': 923,
26322608
'gen_ai.usage.output_tokens': 8,
@@ -2892,18 +2868,15 @@ def tool():
28922868
'events': [
28932869
{'event.name': 'gen_ai.user.message', 'content': 'Call the tool.', 'role': 'user'},
28942870
{
2895-
'event.name': 'gen_ai.choice',
2896-
'index': 0,
2897-
'message': {
2898-
'role': 'assistant',
2899-
'tool_calls': [
2900-
{
2901-
'id': 'call_OpJ32C09GImFzxYLe01MiOOd',
2902-
'type': 'function',
2903-
'function': {'name': 'tool', 'arguments': '{}'},
2904-
}
2905-
],
2906-
},
2871+
'event.name': 'gen_ai.assistant.message',
2872+
'role': 'assistant',
2873+
'tool_calls': [
2874+
{
2875+
'id': 'call_OpJ32C09GImFzxYLe01MiOOd',
2876+
'type': 'function',
2877+
'function': {'name': 'tool', 'arguments': '{}'},
2878+
}
2879+
],
29072880
},
29082881
],
29092882
'gen_ai.usage.input_tokens': 244,
@@ -3164,12 +3137,9 @@ def tool():
31643137
'name': 'tool',
31653138
},
31663139
{
3167-
'event.name': 'gen_ai.choice',
3168-
'index': 0,
3169-
'message': {
3170-
'content': 'It seems there was an error when trying to call the tool. If you need help with something specific, feel free to let me know!',
3171-
'role': 'assistant',
3172-
},
3140+
'event.name': 'gen_ai.assistant.message',
3141+
'content': 'It seems there was an error when trying to call the tool. If you need help with something specific, feel free to let me know!',
3142+
'role': 'assistant',
31733143
},
31743144
],
31753145
'gen_ai.usage.input_tokens': 283,
@@ -3509,9 +3479,9 @@ async def test_voice_pipeline(exporter: TestExporter, vcr_allow_bytes: None):
35093479
'role': 'user',
35103480
},
35113481
{
3512-
'event.name': 'gen_ai.choice',
3513-
'index': 0,
3514-
'message': {'content': 'Natürlich! Wobei genau benötigen Sie Hilfe?', 'role': 'assistant'},
3482+
'event.name': 'gen_ai.assistant.message',
3483+
'content': 'Natürlich! Wobei genau benötigen Sie Hilfe?',
3484+
'role': 'assistant',
35153485
},
35163486
],
35173487
'gen_ai.usage.input_tokens': 33,

tests/otel_integrations/test_openai_agents_mcp.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -387,18 +387,15 @@ def name(self):
387387
'events': [
388388
{'event.name': 'gen_ai.user.message', 'content': 'Give me a random number', 'role': 'user'},
389389
{
390-
'event.name': 'gen_ai.choice',
391-
'index': 0,
392-
'message': {
393-
'role': 'assistant',
394-
'tool_calls': [
395-
{
396-
'id': 'call_jfYaCkab5PQtyNrcrSgMdlRf',
397-
'type': 'function',
398-
'function': {'name': 'random_number', 'arguments': '{}'},
399-
}
400-
],
401-
},
390+
'event.name': 'gen_ai.assistant.message',
391+
'role': 'assistant',
392+
'tool_calls': [
393+
{
394+
'id': 'call_jfYaCkab5PQtyNrcrSgMdlRf',
395+
'type': 'function',
396+
'function': {'name': 'random_number', 'arguments': '{}'},
397+
}
398+
],
402399
},
403400
],
404401
'gen_ai.usage.input_tokens': 51,
@@ -731,9 +728,9 @@ def name(self):
731728
'name': 'random_number',
732729
},
733730
{
734-
'event.name': 'gen_ai.choice',
735-
'index': 0,
736-
'message': {'content': "Here's a random number for you: 4", 'role': 'assistant'},
731+
'event.name': 'gen_ai.assistant.message',
732+
'content': "Here's a random number for you: 4",
733+
'role': 'assistant',
737734
},
738735
],
739736
'gen_ai.usage.input_tokens': 83,

0 commit comments

Comments
 (0)