This example code shows how you can log outputs as well. This follows a similar convention to input logging above:
from openai.types.chat import ChatCompletionAssistantMessageParam
from openinference.semconv.trace import MessageAttributes, SpanAttributes
from opentelemetry.trace import Span
def set_output_attrs(
span: Span,
response_message: ChatCompletionAssistantMessageParam,
) -> None:
# OUTPUT_VALUE shows up on the table view under the output column
# It also shows up under the `output` tab on the span
span.set_attribute(SpanAttributes.OUTPUT_VALUE, response_message.get("content", ""))
# This shows up under `output_messages` tab on the span page
# This code assumes a single response
span.set_attribute(
f"{SpanAttributes.LLM_OUTPUT_MESSAGES}.0.{MessageAttributes.MESSAGE_ROLE}",
response_message["role"],
)
span.set_attribute(
f"{SpanAttributes.LLM_OUTPUT_MESSAGES}.0.{MessageAttributes.MESSAGE_CONTENT}",
response_message.get("content", ""),
)