Skip to content

Commit

Permalink
fix (ai/core): prevent onFinish from masking stream errors (#4318)
Browse files Browse the repository at this point in the history
  • Loading branch information
lgrammel authored Jan 8, 2025
1 parent 927936a commit e4ce80c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/sweet-carrots-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'ai': patch
---

fix (ai/core): prevent onFinish from masking stream errors
Original file line number Diff line number Diff line change
Expand Up @@ -1699,7 +1699,7 @@ exports[`streamText > options.maxSteps > 4 steps: initial, continue, continue, c
]
`;

exports[`streamText > options.onFinish > options.onFinish should send correct information 1`] = `
exports[`streamText > options.onFinish > should send correct information 1`] = `
{
"experimental_providerMetadata": {
"testProvider": {
Expand Down
23 changes: 22 additions & 1 deletion packages/ai/core/generate-text/stream-text.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1640,7 +1640,7 @@ describe('streamText', () => {
});

describe('options.onFinish', () => {
it('options.onFinish should send correct information', async () => {
it('should send correct information', async () => {
let result!: Parameters<
Required<Parameters<typeof streamText>[0]>['onFinish']
>[0];
Expand Down Expand Up @@ -1692,6 +1692,27 @@ describe('streamText', () => {

expect(result).toMatchSnapshot();
});

it('should not prevent error from being forwarded', async () => {
const result = streamText({
model: new MockLanguageModelV1({
doStream: async () => {
throw new Error('test error');
},
}),
prompt: 'test-input',
onFinish() {}, // just defined; do nothing
});

expect(
await convertAsyncIterableToArray(result.fullStream),
).toStrictEqual([
{
type: 'error',
error: new Error('test error'),
},
]);
});
});

describe('result.responseMessages', () => {
Expand Down
23 changes: 13 additions & 10 deletions packages/ai/core/generate-text/stream-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -637,18 +637,21 @@ class DefaultStreamTextResult<

async flush(controller) {
try {
if (recordedSteps.length === 0) {
return; // no steps recorded (e.g. in error scenario)
}

// from last step (when there are errors there may be no last step)
const lastStep = recordedSteps[recordedSteps.length - 1];
if (lastStep) {
self.warningsPromise.resolve(lastStep.warnings);
self.requestPromise.resolve(lastStep.request);
self.responsePromise.resolve(lastStep.response);
self.toolCallsPromise.resolve(lastStep.toolCalls);
self.toolResultsPromise.resolve(lastStep.toolResults);
self.providerMetadataPromise.resolve(
lastStep.experimental_providerMetadata,
);
}

self.warningsPromise.resolve(lastStep.warnings);
self.requestPromise.resolve(lastStep.request);
self.responsePromise.resolve(lastStep.response);
self.toolCallsPromise.resolve(lastStep.toolCalls);
self.toolResultsPromise.resolve(lastStep.toolResults);
self.providerMetadataPromise.resolve(
lastStep.experimental_providerMetadata,
);

// derived:
const finishReason = recordedFinishReason ?? 'unknown';
Expand Down

0 comments on commit e4ce80c

Please sign in to comment.