Skip to content

Add gen_ai.output.messages span attribute for chat completion content#6193

Open
jewoodev wants to merge 1 commit into
spring-projects:mainfrom
jewoodev:fix/6051-output-messages-span-tag
Open

Add gen_ai.output.messages span attribute for chat completion content#6193
jewoodev wants to merge 1 commit into
spring-projects:mainfrom
jewoodev:fix/6051-output-messages-span-tag

Conversation

@jewoodev
Copy link
Copy Markdown
Contributor

@jewoodev jewoodev commented May 28, 2026

Why

Chat completion content is currently only emitted through the opt-in SLF4J logging handler. This adds the output-side span attribute for OpenTelemetry-native backends by exposing textual completions as gen_ai.output.messages.

How

The new ChatModelCompletionSpanContentObservationHandler tags the active span directly instead of adding completion content to Observation.Context, keeping potentially sensitive or unbounded content isolated to the tracing channel. It is only auto-configured when tracing is present and spring.ai.chat.observations.log-completion=true is enabled.

This initial implementation only emits text output messages when every generation can be represented with text content and a finish_reason. Non-text output, such as tool calls, is skipped for now to avoid emitting a malformed or misaligned gen_ai.output.messages attribute.

I reused the existing log-completion opt-in for this change; if separate controls for log output and span content are preferred, I can split this into a dedicated property.

Test

Covered by unit tests for the handler and auto-configuration slice tests for the tracer-present/absent and opt-in matrix.

Part of #6051

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant