File tree 2 files changed +55
-1
lines changed
packages/vertexai/src/requests
2 files changed +55
-1
lines changed Original file line number Diff line number Diff line change @@ -33,8 +33,10 @@ import {
33
33
GenerateContentResponse ,
34
34
HarmCategory ,
35
35
HarmProbability ,
36
- SafetyRating
36
+ SafetyRating ,
37
+ VertexAIErrorCode
37
38
} from '../types' ;
39
+ import { VertexAIError } from '../errors' ;
38
40
39
41
use ( sinonChai ) ;
40
42
@@ -420,4 +422,49 @@ describe('aggregateResponses', () => {
420
422
) . to . equal ( 150 ) ;
421
423
} ) ;
422
424
} ) ;
425
+
426
+ it ( 'throws if a part has no properties' , ( ) => {
427
+ const responsesToAggregate : GenerateContentResponse [ ] = [
428
+ {
429
+ candidates : [
430
+ {
431
+ index : 0 ,
432
+ content : {
433
+ role : 'user' ,
434
+ parts : [ { } as any ] // Empty
435
+ } ,
436
+ finishReason : FinishReason . STOP ,
437
+ finishMessage : 'something' ,
438
+ safetyRatings : [
439
+ {
440
+ category : HarmCategory . HARM_CATEGORY_HARASSMENT ,
441
+ probability : HarmProbability . NEGLIGIBLE
442
+ } as SafetyRating
443
+ ]
444
+ }
445
+ ] ,
446
+ promptFeedback : {
447
+ blockReason : BlockReason . SAFETY ,
448
+ safetyRatings : [
449
+ {
450
+ category : HarmCategory . HARM_CATEGORY_DANGEROUS_CONTENT ,
451
+ probability : HarmProbability . LOW
452
+ } as SafetyRating
453
+ ]
454
+ }
455
+ }
456
+ ] ;
457
+
458
+ try {
459
+ aggregateResponses ( responsesToAggregate ) ;
460
+ } catch ( e ) {
461
+ expect ( ( e as VertexAIError ) . code ) . includes (
462
+ VertexAIErrorCode . INVALID_CONTENT
463
+ ) ;
464
+ expect ( ( e as VertexAIError ) . message ) . to . include (
465
+ 'Part should have at least one property, but there are none. This is likely caused ' +
466
+ 'by a malformed response from the backend.'
467
+ ) ;
468
+ }
469
+ } ) ;
423
470
} ) ;
Original file line number Diff line number Diff line change @@ -197,6 +197,13 @@ export function aggregateResponses(
197
197
if ( part . functionCall ) {
198
198
newPart . functionCall = part . functionCall ;
199
199
}
200
+ if ( Object . keys ( newPart ) . length === 0 ) {
201
+ throw new VertexAIError (
202
+ VertexAIErrorCode . INVALID_CONTENT ,
203
+ 'Part should have at least one property, but there are none. This is likely caused ' +
204
+ 'by a malformed response from the backend.'
205
+ ) ;
206
+ }
200
207
aggregatedResponse . candidates [ i ] . content . parts . push (
201
208
newPart as Part
202
209
) ;
You can’t perform that action at this time.
0 commit comments