Skip to content

Commit a18078b

Browse files
authored
fix: CI issues for openai agents (#2367)
1 parent 3188f3c commit a18078b

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

python/instrumentation/openinference-instrumentation-openai-agents/src/openinference/instrumentation/openai_agents/_processor.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,23 @@ def _get_attributes_from_response_custom_tool_call_output_param(
350350
if (call_id := obj.get("call_id")) is not None:
351351
yield f"{prefix}{MessageAttributes.MESSAGE_TOOL_CALL_ID}", call_id
352352
if (output := obj.get("output")) is not None:
353-
yield f"{prefix}{MessageAttributes.MESSAGE_CONTENT}", output
353+
if isinstance(output, str):
354+
yield f"{prefix}{MESSAGE_CONTENT}", output
355+
elif isinstance(output, list):
356+
for i, item in enumerate(output):
357+
if item["type"] == "input_text":
358+
yield (
359+
f"{prefix}{MESSAGE_CONTENTS}.{i}.{MESSAGE_CONTENT_TEXT}",
360+
item["text"] or "",
361+
)
362+
elif item["type"] == "input_image":
363+
# TODO: handle the input image type for the tool input
364+
pass
365+
elif item["type"] == "input_file":
366+
# TODO: handle the input file type for the tool input
367+
pass
368+
elif TYPE_CHECKING:
369+
assert_never(item["type"])
354370

355371

356372
def _get_attributes_from_function_call_output(
@@ -359,7 +375,24 @@ def _get_attributes_from_function_call_output(
359375
) -> Iterator[tuple[str, AttributeValue]]:
360376
yield f"{prefix}{MESSAGE_ROLE}", "tool"
361377
yield f"{prefix}{MESSAGE_TOOL_CALL_ID}", obj["call_id"]
362-
yield f"{prefix}{MESSAGE_CONTENT}", obj["output"]
378+
if (output := obj.get("output")) is not None:
379+
if isinstance(output, str):
380+
yield f"{prefix}{MESSAGE_CONTENT}", output
381+
elif isinstance(output, list):
382+
for i, item in enumerate(output):
383+
if item["type"] == "input_text":
384+
yield (
385+
f"{prefix}{MESSAGE_CONTENTS}.{i}.{MESSAGE_CONTENT_TEXT}",
386+
item["text"] or "",
387+
)
388+
elif item["type"] == "input_image":
389+
# TODO: handle the input image type for the tool input
390+
pass
391+
elif item["type"] == "input_file":
392+
# TODO: handle the input file type for the tool input
393+
pass
394+
elif TYPE_CHECKING:
395+
assert_never(item["type"])
363396

364397

365398
def _get_attributes_from_generation_span_data(

python/instrumentation/openinference-instrumentation-openai-agents/tests/test_span_attribute_helpers.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,8 @@ def test_get_attributes_from_response_function_tool_call_param(
409409
"output": "",
410410
},
411411
{
412-
"message.content": "",
413412
"message.role": "tool",
413+
"message.content": "",
414414
"message.tool_call_id": "123",
415415
},
416416
id="empty_output",
@@ -421,12 +421,23 @@ def test_get_attributes_from_response_function_tool_call_param(
421421
"output": None,
422422
},
423423
{
424-
"message.content": None,
425424
"message.role": "tool",
426425
"message.tool_call_id": "123",
427426
},
428427
id="none_output",
429428
),
429+
pytest.param(
430+
{
431+
"call_id": "123",
432+
"output": [{"type": "input_text", "text": "result"}],
433+
},
434+
{
435+
"message.role": "tool",
436+
"message.contents.0.message_content.text": "result",
437+
"message.tool_call_id": "123",
438+
},
439+
id="functional_call_output",
440+
),
430441
],
431442
)
432443
def test_get_attributes_from_function_call_output(

0 commit comments

Comments
 (0)