Repository navigation
fix(genai-util): default streaming finish_reasons to error only once the stream is observed #3917
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -127,7 +127,13 @@ export class InferenceInvocation extends BaseInvocation { | |
| private readonly _serverAddress?: string; | ||
| private readonly _serverPort?: number; | ||
| private readonly _contentCaptureMode: ContentCaptureMode; | ||
| private _isStreaming: boolean; | ||
| /** | ||
| * Whether the response was observed as a stream, through {@link setStreaming} | ||
| * (called by the stream wrapper and {@link recordStreamChunk}). Requesting a | ||
| * 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; | ||
| private _responseModel?: string; | ||
| private _finishReasons?: FinishReason[]; | ||
| private _usage?: TokenUsageDetails; | ||
|
|
@@ -159,7 +165,6 @@ export class InferenceInvocation extends BaseInvocation { | |
| this._requestModel = options.requestModel; | ||
| this._serverAddress = options.serverAddress; | ||
| this._serverPort = options.serverPort; | ||
| this._isStreaming = options.requestOptions?.stream === true; | ||
| this._contentCaptureMode = handler.getContentCaptureMode(); | ||
| this._inputMessages = options.inputMessages | ||
| ? [...options.inputMessages] | ||
|
|
@@ -171,6 +176,7 @@ export class InferenceInvocation extends BaseInvocation { | |
| * Set the request streaming attribute to true. | ||
| * | ||
| * This is a convenience method for the case when the user is streaming the response. | ||
| * Once called, a stream that ends without a finish reason reports `'error'`. | ||
| */ | ||
| public setStreaming(): this { | ||
| this.setAttribute(ATTR_GEN_AI_REQUEST_STREAM, true); | ||
|
|
@@ -360,9 +366,10 @@ export class InferenceInvocation extends BaseInvocation { | |
| _endTime: HrTime, | ||
| errorType?: string | ||
| ): void { | ||
| // Avoids putting ['error'] on a successful non-streaming span when | ||
| // setFinishReasons() wasn't called, while still defaulting to ['error'] if | ||
| // 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The semantic conventions have since been updated with the new guidelines being:
See Ref. Semconv previously dictated that missing finish_reasons be populated with |
||
| // chunk) or if fail() is called. | ||
| if ( | ||
| this._finishReasons !== undefined || | ||
| errorType !== undefined || | ||
|
|
||
There was a problem hiding this comment.
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.