Skip to content

Commit c0706fd

Browse files
authored
Record tool names in tool responses in OpenAI Agents instrumentation (#1093)
1 parent 9ebc07c commit c0706fd

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

logfire/_internal/integrations/openai_agents.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ def get_response_span_events(span: ResponseSpanData):
408408
events: list[dict[str, Any]] = []
409409
response = span.response
410410
inputs = span.input
411+
tool_call_id_to_name: dict[str, str] = {}
411412
if response and (instructions := getattr(response, 'instructions', None)):
412413
events += [
413414
{
@@ -421,10 +422,10 @@ def get_response_span_events(span: ResponseSpanData):
421422
inputs = [{'role': 'user', 'content': inputs}]
422423
for inp in inputs: # type: ignore
423424
inp: dict[str, Any]
424-
events += input_to_events(inp)
425+
events += input_to_events(inp, tool_call_id_to_name)
425426
if response and response.output:
426427
for out in response.output:
427-
for message in input_to_events(out.model_dump()):
428+
for message in input_to_events(out.model_dump(), tool_call_id_to_name):
428429
message.pop('event.name', None)
429430
events.append(
430431
{
@@ -436,7 +437,7 @@ def get_response_span_events(span: ResponseSpanData):
436437
return events
437438

438439

439-
def input_to_events(inp: dict[str, Any]):
440+
def input_to_events(inp: dict[str, Any], tool_call_id_to_name: dict[str, str]):
440441
try:
441442
events: list[dict[str, Any]] = []
442443
role: str | None = inp.get('role')
@@ -454,6 +455,7 @@ def input_to_events(inp: dict[str, Any]):
454455
continue
455456
events.append(unknown_event(content_item)) # pragma: no cover
456457
elif typ == 'function_call':
458+
tool_call_id_to_name[inp['call_id']] = inp['name']
457459
events.append(
458460
{
459461
'event.name': 'gen_ai.assistant.message',
@@ -474,6 +476,7 @@ def input_to_events(inp: dict[str, Any]):
474476
'role': 'tool',
475477
'id': inp['call_id'],
476478
'content': inp['output'],
479+
'name': tool_call_id_to_name.get(inp['call_id'], inp.get('name', 'unknown')),
477480
}
478481
)
479482
else:

tests/otel_integrations/test_openai_agents.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,12 +870,14 @@ def random_number() -> int:
870870
'role': 'tool',
871871
'id': 'call_vwqy7HyGGnNht9NNfxMnnouY',
872872
'content': '4',
873+
'name': 'random_number',
873874
},
874875
{
875876
'event.name': 'gen_ai.tool.message',
876877
'role': 'tool',
877878
'id': 'call_oEA0MnUXCwKevx8txteoopNL',
878879
'content': "{'assistant': 'agent2'}",
880+
'name': 'transfer_to_agent2',
879881
},
880882
{
881883
'event.name': 'gen_ai.choice',
@@ -3159,6 +3161,7 @@ def tool():
31593161
'role': 'tool',
31603162
'id': 'call_OpJ32C09GImFzxYLe01MiOOd',
31613163
'content': "An error occurred while running the tool. Please try again. Error: Ouch, don't do that again!",
3164+
'name': 'tool',
31623165
},
31633166
{
31643167
'event.name': 'gen_ai.choice',

tests/otel_integrations/test_openai_agents_mcp.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,7 @@ def name(self):
728728
'role': 'tool',
729729
'id': 'call_jfYaCkab5PQtyNrcrSgMdlRf',
730730
'content': '{"type":"text","text":"4","annotations":null}',
731+
'name': 'random_number',
731732
},
732733
{
733734
'event.name': 'gen_ai.choice',

0 commit comments

Comments
 (0)