Skip to content

Commit 6611a81

Browse files
fix(openai-agents): emit message content and reasoning for LLM spans
Signed-off-by: Syed Mohsin Bukhari <syed.mohsin-bukhari@chaos.com>
1 parent 075e413 commit 6611a81

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
ResponseOutputMessageParam,
3434
ResponseOutputRefusal,
3535
ResponseOutputText,
36+
ResponseReasoningItem,
3637
ResponseUsage,
3738
Tool,
3839
)
@@ -651,7 +652,12 @@ def _get_attributes_from_response_output(
651652
elif item.type == "computer_call":
652653
... # TODO
653654
elif item.type == "reasoning":
654-
... # TODO
655+
if isinstance(item, ResponseReasoningItem) and item.summary:
656+
prefix = f"{LLM_OUTPUT_MESSAGES}.{msg_idx}."
657+
yield f"{prefix}{MESSAGE_ROLE}", "assistant"
658+
summary_text = "\n".join(s.text for s in item.summary)
659+
yield f"{prefix}{MESSAGE_CONTENT}", summary_text
660+
msg_idx += 1
655661
elif item.type == "image_generation_call":
656662
... # TODO
657663
elif item.type == "code_interpreter_call":
@@ -721,15 +727,20 @@ def _get_attributes_from_message(
721727
prefix: str = "",
722728
) -> Iterator[tuple[str, AttributeValue]]:
723729
yield f"{prefix}{MESSAGE_ROLE}", obj.role
730+
text_parts: list[str] = []
724731
for i, item in enumerate(obj.content):
725732
if isinstance(item, ResponseOutputText):
726733
yield f"{prefix}{MESSAGE_CONTENTS}.{i}.{MESSAGE_CONTENT_TYPE}", "text"
727734
yield f"{prefix}{MESSAGE_CONTENTS}.{i}.{MESSAGE_CONTENT_TEXT}", item.text
735+
text_parts.append(item.text)
728736
elif isinstance(item, ResponseOutputRefusal):
729737
yield f"{prefix}{MESSAGE_CONTENTS}.{i}.{MESSAGE_CONTENT_TYPE}", "text"
730738
yield f"{prefix}{MESSAGE_CONTENTS}.{i}.{MESSAGE_CONTENT_TEXT}", item.refusal
739+
text_parts.append(item.refusal)
731740
elif TYPE_CHECKING:
732741
assert_never(item)
742+
if text_parts:
743+
yield f"{prefix}{MESSAGE_CONTENT}", "\n".join(text_parts)
733744

734745

735746
def _get_attributes_from_usage(

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
ResponseOutputRefusal,
3535
ResponseOutputText,
3636
ResponseOutputTextParam,
37+
ResponseReasoningItem,
3738
ResponseReasoningItemParam,
3839
ResponseUsage,
3940
Tool,
@@ -232,9 +233,7 @@
232233
summary=[{"type": "summary_text", "text": "Test reasoning"}],
233234
)
234235
],
235-
{
236-
# TODO: Implement reasoning item attributes
237-
},
236+
{},
238237
id="reasoning_item",
239238
),
240239
pytest.param(
@@ -1277,6 +1276,7 @@ def test_get_attributes_from_message_content_list(
12771276
}
12781277
),
12791278
"llm.model_name": "gpt-4",
1279+
"llm.output_messages.0.message.content": "Hi",
12801280
"llm.output_messages.0.message.contents.0.message_content.text": "Hi",
12811281
"llm.output_messages.0.message.contents.0.message_content.type": "text",
12821282
"llm.output_messages.0.message.role": "assistant",
@@ -1470,6 +1470,7 @@ def test_get_attributes_from_message_content_list(
14701470
}
14711471
),
14721472
"llm.model_name": "gpt-4",
1473+
"llm.output_messages.0.message.content": "Hi\nI cannot help with that",
14731474
"llm.output_messages.0.message.contents.0.message_content.text": "Hi",
14741475
"llm.output_messages.0.message.contents.0.message_content.type": "text",
14751476
"llm.output_messages.0.message.contents.1.message_content.text": "I cannot help with that", # noqa: E501
@@ -1753,6 +1754,7 @@ def test_get_attributes_from_tools(
17531754
"llm.output_messages.0.message.role": "assistant",
17541755
"llm.output_messages.0.message.contents.0.message_content.type": "text",
17551756
"llm.output_messages.0.message.contents.0.message_content.text": "Hi",
1757+
"llm.output_messages.0.message.content": "Hi",
17561758
},
17571759
id="message_output",
17581760
),
@@ -1798,6 +1800,7 @@ def test_get_attributes_from_tools(
17981800
"llm.output_messages.0.message.role": "assistant",
17991801
"llm.output_messages.0.message.contents.0.message_content.type": "text",
18001802
"llm.output_messages.0.message.contents.0.message_content.text": "Hi",
1803+
"llm.output_messages.0.message.content": "Hi",
18011804
"llm.output_messages.1.message.role": "assistant",
18021805
"llm.output_messages.1.message.tool_calls.0.tool_call.id": "123",
18031806
"llm.output_messages.1.message.tool_calls.0.tool_call.function.name": "test_func",
@@ -1861,12 +1864,31 @@ def test_get_attributes_from_tools(
18611864
"llm.output_messages.0.message.role": "assistant",
18621865
"llm.output_messages.0.message.contents.0.message_content.type": "text",
18631866
"llm.output_messages.0.message.contents.0.message_content.text": "Hi",
1867+
"llm.output_messages.0.message.content": "Hi",
18641868
"llm.output_messages.1.message.role": "assistant",
18651869
"llm.output_messages.1.message.contents.0.message_content.type": "text",
18661870
"llm.output_messages.1.message.contents.0.message_content.text": "World",
1871+
"llm.output_messages.1.message.content": "World",
18671872
},
18681873
id="multiple_messages",
18691874
),
1875+
pytest.param(
1876+
[
1877+
ResponseReasoningItem(
1878+
id="reason-123",
1879+
type="reasoning",
1880+
summary=[
1881+
{"type": "summary_text", "text": "Step one reasoning"},
1882+
{"type": "summary_text", "text": "Step two reasoning"},
1883+
],
1884+
)
1885+
],
1886+
{
1887+
"llm.output_messages.0.message.role": "assistant",
1888+
"llm.output_messages.0.message.content": "Step one reasoning\nStep two reasoning",
1889+
},
1890+
id="reasoning_output",
1891+
),
18701892
pytest.param(
18711893
[],
18721894
{},
@@ -1982,6 +2004,7 @@ def test_get_attributes_from_function_tool_call(
19822004
"message.role": "assistant",
19832005
"message.contents.0.message_content.type": "text",
19842006
"message.contents.0.message_content.text": "Hi",
2007+
"message.content": "Hi",
19852008
},
19862009
id="text_message",
19872010
),
@@ -2002,6 +2025,7 @@ def test_get_attributes_from_function_tool_call(
20022025
"message.role": "assistant",
20032026
"message.contents.0.message_content.type": "text",
20042027
"message.contents.0.message_content.text": "I cannot help with that",
2028+
"message.content": "I cannot help with that",
20052029
},
20062030
id="refusal_message",
20072031
),
@@ -2029,6 +2053,7 @@ def test_get_attributes_from_function_tool_call(
20292053
"message.contents.0.message_content.text": "Hi",
20302054
"message.contents.1.message_content.type": "text",
20312055
"message.contents.1.message_content.text": "I cannot help with that",
2056+
"message.content": "Hi\nI cannot help with that",
20322057
},
20332058
id="mixed_content_message",
20342059
),
@@ -2070,6 +2095,7 @@ def test_get_attributes_from_function_tool_call(
20702095
"message.contents.0.message_content.text": "Hello",
20712096
"message.contents.1.message_content.type": "text",
20722097
"message.contents.1.message_content.text": "World",
2098+
"message.content": "Hello\nWorld",
20732099
},
20742100
id="multiple_text_messages",
20752101
),

0 commit comments

Comments
 (0)