Skip to content

Commit 0c10984

Browse files
committed
Throw in aggregateResponses if newPart has no properties
1 parent 86827d1 commit 0c10984

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

packages/vertexai/src/requests/stream-reader.test.ts

+48-1
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ import {
3333
GenerateContentResponse,
3434
HarmCategory,
3535
HarmProbability,
36-
SafetyRating
36+
SafetyRating,
37+
VertexAIErrorCode
3738
} from '../types';
39+
import { VertexAIError } from '../errors';
3840

3941
use(sinonChai);
4042

@@ -420,4 +422,49 @@ describe('aggregateResponses', () => {
420422
).to.equal(150);
421423
});
422424
});
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+
});
423470
});

packages/vertexai/src/requests/stream-reader.ts

+7
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,13 @@ export function aggregateResponses(
197197
if (part.functionCall) {
198198
newPart.functionCall = part.functionCall;
199199
}
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+
}
200207
aggregatedResponse.candidates[i].content.parts.push(
201208
newPart as Part
202209
);

0 commit comments

Comments
 (0)