Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -462,9 +462,9 @@ def get_response_output_message_attributes(index: int, message: "ResponseOutputM
attributes = _extract_attributes_from_mapping_with_index(message, RESPONSE_OUTPUT_MESSAGE_ATTRIBUTES, index)

if message.content:
for i, content in enumerate(message.content):
for content in message.content:
if isinstance(content, ResponseOutputText):
attributes.update(get_response_output_text_attributes(content, i))
attributes.update(get_response_output_text_attributes(content, index))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Preserve every text block in a response message

A ResponseOutputMessage.content list can legally contain multiple ResponseOutputText blocks. Passing the same parent index for each block makes every helper call emit gen_ai.completion.{index}.content, so attributes.update() silently replaces all earlier text with the last block. The helper also restarts each block's annotation index at j=0, causing later annotations to overwrite earlier ones under the same completion. Please aggregate the text blocks in order and keep annotation indices unique across the whole message (with offsets consistent with the aggregated content), and add a regression containing two annotated output_text blocks.


else:
logger.debug(
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/instrumentation/openai_core/test_response_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import os
from unittest.mock import MagicMock, patch

from openai.types.responses import ResponseOutputMessage, ResponseOutputText, ResponseReasoningItem

from agentops.instrumentation.providers.openai.attributes.response import (
get_response_kwarg_attributes,
get_response_response_attributes,
Expand Down Expand Up @@ -400,6 +402,35 @@ def test_get_response_output_message_attributes(self):
# Verify basic expected attributes
assert isinstance(result, dict)

def test_response_message_content_uses_parent_output_index(self):
"""Message text must share the parent output item's completion index."""
reasoning = ResponseReasoningItem(
id="reasoning_1",
summary=[],
type="reasoning",
status="completed",
)
message = ResponseOutputMessage(
id="msg_1",
content=[
ResponseOutputText(
annotations=[],
text="answer",
type="output_text",
)
],
role="assistant",
status="completed",
type="message",
)

attributes = get_response_output_attributes([reasoning, message])

assert attributes[MessageAttributes.COMPLETION_ID.format(i=0)] == "reasoning_1"
assert MessageAttributes.COMPLETION_CONTENT.format(i=0) not in attributes
assert attributes[MessageAttributes.COMPLETION_ID.format(i=1)] == "msg_1"
assert attributes[MessageAttributes.COMPLETION_CONTENT.format(i=1)] == "answer"

def test_get_response_output_text_attributes(self):
"""Test extraction of attributes from output text"""
# Create a mock text content
Expand Down