@@ -423,18 +423,6 @@ describe('result.object', () => {
423
423
const result = await streamObject ( {
424
424
model : new MockLanguageModelV1 ( {
425
425
doStream : async ( { prompt, mode } ) => {
426
- assert . deepStrictEqual ( mode , { type : 'object-json' } ) ;
427
- assert . deepStrictEqual ( prompt , [
428
- {
429
- role : 'system' ,
430
- content :
431
- 'JSON schema:\n' +
432
- '{"type":"object","properties":{"content":{"type":"string"}},"required":["content"],"additionalProperties":false,"$schema":"http://json-schema.org/draft-07/schema#"}\n' +
433
- 'You MUST answer with a JSON object that matches the JSON schema above.' ,
434
- } ,
435
- { role : 'user' , content : [ { type : 'text' , text : 'prompt' } ] } ,
436
- ] ) ;
437
-
438
426
return {
439
427
stream : convertArrayToReadableStream ( [
440
428
{ type : 'text-delta' , textDelta : '{ ' } ,
@@ -470,18 +458,6 @@ describe('result.object', () => {
470
458
const result = await streamObject ( {
471
459
model : new MockLanguageModelV1 ( {
472
460
doStream : async ( { prompt, mode } ) => {
473
- assert . deepStrictEqual ( mode , { type : 'object-json' } ) ;
474
- assert . deepStrictEqual ( prompt , [
475
- {
476
- role : 'system' ,
477
- content :
478
- 'JSON schema:\n' +
479
- '{"type":"object","properties":{"content":{"type":"string"}},"required":["content"],"additionalProperties":false,"$schema":"http://json-schema.org/draft-07/schema#"}\n' +
480
- 'You MUST answer with a JSON object that matches the JSON schema above.' ,
481
- } ,
482
- { role : 'user' , content : [ { type : 'text' , text : 'prompt' } ] } ,
483
- ] ) ;
484
-
485
461
return {
486
462
stream : convertArrayToReadableStream ( [
487
463
{ type : 'text-delta' , textDelta : '{ ' } ,
@@ -516,6 +492,39 @@ describe('result.object', () => {
516
492
expect ( TypeValidationError . isTypeValidationError ( error ) ) . toBeTruthy ( ) ;
517
493
} ) ;
518
494
} ) ;
495
+
496
+ it ( 'should not lead to unhandled promise rejections when the streamed object does not match the schema' , async ( ) => {
497
+ const result = await streamObject ( {
498
+ model : new MockLanguageModelV1 ( {
499
+ doStream : async ( { prompt, mode } ) => {
500
+ return {
501
+ stream : convertArrayToReadableStream ( [
502
+ { type : 'text-delta' , textDelta : '{ ' } ,
503
+ { type : 'text-delta' , textDelta : '"invalid": ' } ,
504
+ { type : 'text-delta' , textDelta : `"Hello, ` } ,
505
+ { type : 'text-delta' , textDelta : `world` } ,
506
+ { type : 'text-delta' , textDelta : `!"` } ,
507
+ { type : 'text-delta' , textDelta : ' }' } ,
508
+ {
509
+ type : 'finish' ,
510
+ finishReason : 'stop' ,
511
+ usage : { completionTokens : 10 , promptTokens : 3 } ,
512
+ } ,
513
+ ] ) ,
514
+ rawCall : { rawPrompt : 'prompt' , rawSettings : { } } ,
515
+ } ;
516
+ } ,
517
+ } ) ,
518
+ schema : z . object ( { content : z . string ( ) } ) ,
519
+ mode : 'json' ,
520
+ prompt : 'prompt' ,
521
+ } ) ;
522
+
523
+ // consume stream (runs in parallel)
524
+ convertAsyncIterableToArray ( result . partialObjectStream ) ;
525
+
526
+ // unhandled promise rejection should not be thrown (Vitest does this automatically)
527
+ } ) ;
519
528
} ) ;
520
529
521
530
describe ( 'options.onFinish' , ( ) => {
0 commit comments