Repository navigation
fix(genai-util): default streaming finish_reasons to error only once the stream is observed - #3917
Conversation
…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.
Pull request dashboard statusWaiting 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): Status above doesn't look right?
|
sangwoorhie
left a comment
There was a problem hiding this comment.
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:
_isStreamingis only read when deciding the default finish reasons, so moving its initialisation fromrequestOptions.streamtosetStreaming()doesn't change anything else inInferenceInvocation.- On main the only genai-util consumer is
instrumentation-langchain, which doesn't passrequestOptions.streamor use the stream helpers, so nothing in the repo changes behaviour today. - For the adoption PRs, "observed" starts when the stream is wrapped:
wrapAsyncStreamcallssetStreaming()up front rather than on the first read. In #3916 the wrapper is attached atStream.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 |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
Which problem is this PR solving?
An
InferenceInvocationstarted withrequestOptions: { stream: true }reportsgen_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 usesmessages.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
['error']default now applies only once the stream has been observed, meaningsetStreaming()has been called. The stream wrapper andrecordStreamChunk()both call it. Requesting a stream throughrequestOptions.streamno longer counts on its own.gen_ai.request.streamis still set fromrequestOptions.stream, as before.breakfrom a wrapped streamwrapAsyncStreamfail(), which always reports['error']setFinishReasons()stream: true, reads the stream itself withoutrecordStreamChunk()orsetStreaming(), and then callsstop()without finish reasons no longer gets['error']. The attribute is left off. No instrumentation in this repo does this;instrumentation-langchaindoesn't use streaming invocations.Checklist
setStreaming()and failed streaming invocations. It fails without the fix.npm testingenai-util: 175 passing.npm run compile, eslint and prettier are clean.instrumentation-anthropictests pass, and theasResponse()streaming span no longer hasfinish_reasons.🤖 Generated with Claude Code