@@ -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 :
0 commit comments