Skip to content

Commit 8a87693

Browse files
authored
fix(ai): ensure warnings promise in streamObject is resolved with correct warnings (#8133)
## Background The `warnings` promise on `streamObject()` never resolves. Awaiting on it results in the program hanging. ## Summary - Added two unit tests for no warnings and some warnings. The unit tests both cause the unit tests to timeout (current broken behavior) - Make sure to resolve the warnings promise - Enqueue chunks for `stream-start`, which is what `createTestModel()` uses to pass warnings ## Manual Verification - Awaiting on the warnings promise no longer causes hanging
1 parent 6aaf688 commit 8a87693

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

.changeset/thin-shoes-switch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'ai': patch
3+
---
4+
5+
fix(ai) Make sure warnings promise in streamObject is resolved and properly collects and passes warnings

packages/ai/src/generate-object/stream-object.test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,4 +1798,58 @@ describe('streamObject', () => {
17981798
}
17991799
});
18001800
});
1801+
1802+
describe('warnings', () => {
1803+
it('should resolve warnings promise with undefined when no warnings are present', async () => {
1804+
const mockModel = createTestModel({
1805+
warnings: [], // No warnings
1806+
});
1807+
1808+
const result = streamObject({
1809+
model: mockModel,
1810+
schema: z.object({ content: z.string() }),
1811+
prompt: 'prompt',
1812+
});
1813+
1814+
// Consume the stream to completion
1815+
await convertAsyncIterableToArray(result.partialObjectStream);
1816+
1817+
// Wait for the warnings promise to resolve
1818+
const warnings = await result.warnings;
1819+
1820+
expect(warnings).toEqual([]);
1821+
});
1822+
1823+
it('should resolve warnings promise with warnings when warnings are present', async () => {
1824+
const expectedWarnings: LanguageModelV2CallWarning[] = [
1825+
{
1826+
type: 'unsupported-setting',
1827+
setting: 'frequency_penalty',
1828+
details: 'This model does not support the frequency_penalty setting.',
1829+
},
1830+
{
1831+
type: 'other',
1832+
message: 'Test warning message',
1833+
},
1834+
];
1835+
1836+
const mockModel = createTestModel({
1837+
warnings: expectedWarnings,
1838+
});
1839+
1840+
const result = streamObject({
1841+
model: mockModel,
1842+
schema: z.object({ content: z.string() }),
1843+
prompt: 'prompt',
1844+
});
1845+
1846+
// Consume the stream to completion
1847+
await convertAsyncIterableToArray(result.partialObjectStream);
1848+
1849+
// Wait for the warnings promise to resolve
1850+
const warnings = await result.warnings;
1851+
1852+
expect(warnings).toEqual(expectedWarnings);
1853+
});
1854+
});
18011855
});

packages/ai/src/generate-object/stream-object.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ class DefaultStreamObjectResult<PARTIAL, RESULT, ELEMENT_STREAM>
521521
case 'response-metadata':
522522
case 'finish':
523523
case 'error':
524+
case 'stream-start':
524525
controller.enqueue(chunk);
525526
break;
526527
}
@@ -715,6 +716,7 @@ class DefaultStreamObjectResult<PARTIAL, RESULT, ELEMENT_STREAM>
715716
// resolve promises that can be resolved now:
716717
self._usage.resolve(usage);
717718
self._providerMetadata.resolve(providerMetadata);
719+
self._warnings.resolve(warnings);
718720
self._response.resolve({
719721
...fullResponse,
720722
headers: response?.headers,

0 commit comments

Comments
 (0)