Skip to content

fix(genai-util): default streaming finish_reasons to error only once the stream is observed - #3917

Open
eternalcuriouslearner wants to merge 1 commit into
open-telemetry:mainfrom
eternalcuriouslearner:fix/genai-util-streaming-finish-reasons
Open

eternalcuriouslearner wants to merge 1 commit into
open-telemetry:mainfrom
eternalcuriouslearner:fix/genai-util-streaming-finish-reasons

Conversation

@eternalcuriouslearner

Copy link
Copy Markdown
Contributor

Which problem is this PR solving?

An InferenceInvocation started with requestOptions: { stream: true } reports gen_ai.response.finish_reasons: ['error'] when it is stopped successfully without a finish reason, even if the stream was never read through the invocation.

This happens in #3916 (instrumentation-anthropic). When a caller uses messages.create({ stream: true }).asResponse(), they receive the raw HTTP response and read the body themselves. The instrumentation never sees the stream, so it has no finish reason to report. The successful span still claims ['error'], and dashboards that filter on finish reason count it as a failed generation.

Short description of the changes

  • The ['error'] default now applies only once the stream has been observed, meaning setStreaming() has been called. The stream wrapper and recordStreamChunk() both call it. Requesting a stream through requestOptions.stream no longer counts on its own.
  • gen_ai.request.stream is still set from requestOptions.stream, as before.
  • These cases are unchanged:
    • an early break from a wrapped stream
    • an empty stream that is consumed through wrapAsyncStream
    • fail(), which always reports ['error']
    • an explicit setFinishReasons()
  • Behaviour change: a caller that passes stream: true, reads the stream itself without recordStreamChunk() or setStreaming(), and then calls stop() without finish reasons no longer gets ['error']. The attribute is left off. No instrumentation in this repo does this; instrumentation-langchain doesn't use streaming invocations.
  • There is no public API change.

Checklist

  • Added a unit test covering requested-but-unread, chunk-observed, setStreaming() and failed streaming invocations. It fails without the fix.
  • npm test in genai-util: 175 passing. npm run compile, eslint and prettier are clean.
  • Checked locally against refactor(instrumentation-anthropic): use genai-util TelemetryHandler #3916: all 44 instrumentation-anthropic tests pass, and the asResponse() streaming span no longer has finish_reasons.

🤖 Generated with Claude Code

…the stream is observed

`requestOptions.stream: true` alone made a successful `stop()` report
`gen_ai.response.finish_reasons: ['error']`. A caller that requests a stream
but never reads it through the invocation (e.g. hands the raw HTTP response
to the user) has no finish reason to report, so the span claimed an error
for a successful call.

The `['error']` default now applies only after the stream has been observed
via `setStreaming()`, which the stream wrapper and `recordStreamChunk()`
call. Early breaks, empty wrapped streams and failures are unchanged.
@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Pull request dashboard status

Waiting on the author · refreshed 2026-11-30 23:50 UTC

Respond to 2 review items (e.g. link a commit, explain why not, ask a follow-up):

  • Inline threads: 1, 2
Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Should this be with reviewers? Comment /dashboard route:reviewers to route it to them.
  • Anything wrong — including the routing? Report it with what you expected; it helps us improve the dashboard.

@eternalcuriouslearner
eternalcuriouslearner marked this pull request as ready for review November 15, 2026 22:31
@eternalcuriouslearner
eternalcuriouslearner requested a review from a team as a code owner November 15, 2026 22:31

@sangwoorhie sangwoorhie left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Checked against current main (a209d83): the commit cherry-picks cleanly, the genai-util tests pass (175), and the new test fails with src/ reverted to main.

On the scope of the behaviour change:

  • _isStreaming is only read when deciding the default finish reasons, so moving its initialisation from requestOptions.stream to setStreaming() doesn't change anything else in InferenceInvocation.
  • On main the only genai-util consumer is instrumentation-langchain, which doesn't pass requestOptions.stream or use the stream helpers, so nothing in the repo changes behaviour today.
  • For the adoption PRs, "observed" starts when the stream is wrapped: wrapAsyncStream calls setStreaming() up front rather than on the first read. In #3916 the wrapper is attached at Stream.iterator(), so the two line up there; an instrumentation that wraps a stream eagerly would still get ['error'] if it later stops an unread stream.

// a stream ends early (e.g. break before the final chunk) or if fail() is called.
// Avoids putting ['error'] on a successful span when setFinishReasons()
// wasn't called and no stream was observed, while still defaulting to
// ['error'] if an observed stream ends early (e.g. break before the final

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The semantic conventions have since been updated with the new guidelines being:

[30] gen_ai.response.finish_reasons: Values correspond to generations in the same order as the returned choices/candidates.
For interrupted generations, instrumentations SHOULD report the provider-reported interruption reason when available and interrupted otherwise.
Each position SHOULD contain the finish reason for the corresponding choice/candidate. If an expected finish reason is missing, instrumentations SHOULD report interrupted when the caller closes or disposes of the stream without an error, and error if generation fails or the stream ends unexpectedly.
error indicates that the generation ended abnormally, whether reported by the provider or inferred by the instrumentation.

See Ref.

Semconv previously dictated that missing finish_reasons be populated with error instead. This behavior of defaulting to error needs updating now.

* stream alone does not set it: a caller that never reads the stream has no
* finish reason to report, missing or otherwise.
*/
private _isStreaming = false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

IIUC, the purpose of this variable is now changed - so the name should be updated as well. A better suited name for this can be _streamObserved.

Please move it toward the end of the other private vars.

*/
public setStreaming(): this {
this.setAttribute(ATTR_GEN_AI_REQUEST_STREAM, true);
this._isStreaming = true;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Correct me if I'm wrong, but flipping _isStreaming (or if renamed to _streamObserved) to true means it is confirmed that a stream is observered.

My suggestion would be to then remove this line from setStreaming - letting setStreaming remain a purely convenience method and keep implementation consistent with AgentInvocation.

Instead, we should switch _isStreaming/ _streamObserved to true in methods like recordStreamChunk or wrapAsyncStream.

Let me know if this makes sense.

This branch has not been deployed

No deployments
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.

4 participants