Skip to content

OpenAI Responses message content uses the wrong completion index #1436

Description

@yaodong-shen

Description

get_response_output_message_attributes() uses the index within message.content when extracting ResponseOutputText, instead of the message's index within response.output.

This mis-associates message text whenever another output item (commonly a reasoning item) precedes the message. The message metadata is emitted under gen_ai.completion.1.*, while its content is emitted under gen_ai.completion.0.content and can overwrite attributes belonging to the reasoning item.

Reproduced on current main at f8e907b92dabe47232978023fdcb01e2a7d4b752.

Minimal reproduction

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

from agentops.instrumentation.providers.openai.attributes.response import (
    get_response_output_attributes,
)

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",
)

print(get_response_output_attributes([reasoning, message]))

Relevant current output:

gen_ai.completion.0.id = reasoning_1
gen_ai.completion.0.content = answer
gen_ai.completion.1.id = msg_1
# gen_ai.completion.1.content is missing

Expected behavior

The text content should use the parent output message's completion index:

gen_ai.completion.0.id = reasoning_1
gen_ai.completion.1.id = msg_1
gen_ai.completion.1.content = answer

Suggested fix

Pass the outer response.output index through to get_response_output_text_attributes() instead of replacing it with the inner content-list index. Add a regression using actual OpenAI response types with a reasoning item before the message.

I can submit a focused fix with regression coverage.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions