Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/providers/google-vertex-ai/chatComplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ export const GoogleChatCompleteStreamChunkTransform: (
)
: null;
let message: any = { role: 'assistant', content: '' };
if (generation.content?.parts[0]?.text) {
if (generation.content?.parts?.[0]?.text) {
const contentBlocks = [];
let content = '';
for (const part of generation.content.parts) {
Expand All @@ -732,7 +732,7 @@ export const GoogleChatCompleteStreamChunkTransform: (
...(!strictOpenAiCompliance &&
contentBlocks.length && { content_blocks: contentBlocks }),
};
} else if (generation.content?.parts[0]?.functionCall) {
} else if (generation.content?.parts?.[0]?.functionCall) {
message = {
role: 'assistant',
tool_calls: generation.content.parts.map((part, idx) => {
Expand All @@ -753,7 +753,7 @@ export const GoogleChatCompleteStreamChunkTransform: (
}
}),
};
} else if (generation.content?.parts[0]?.inlineData) {
} else if (generation.content?.parts?.[0]?.inlineData) {
const part = generation.content.parts[0];
const contentBlocks = [
{
Expand Down
37 changes: 37 additions & 0 deletions src/providers/google/chatComplete.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { GoogleChatCompleteStreamChunkTransform } from './chatComplete';

describe('GoogleChatCompleteStreamChunkTransform', () => {
// Gemini emits a terminating stream chunk whose candidate carries a
// `finishReason` and a `content` object that has NO `parts` array
// (e.g. finishReason "SAFETY" / "RECITATION"). The transform must not crash.
it('does not throw when candidate content has no parts array', () => {
const chunk =
'data: ' +
JSON.stringify({
candidates: [
{ content: { role: 'model' }, finishReason: 'SAFETY', index: 0 },
],
usageMetadata: {
promptTokenCount: 10,
candidatesTokenCount: 0,
totalTokenCount: 10,
},
modelVersion: 'gemini-1.5-pro',
});

const streamState: any = {};
let output: string = '';
expect(() => {
output = GoogleChatCompleteStreamChunkTransform(
chunk,
'fb-id',
streamState,
false
);
}).not.toThrow();

const json = JSON.parse(output.replace(/^data: /, '').trim());
expect(json.choices[0].finish_reason).toBe('SAFETY');
expect(json.choices[0].delta.content).toBe('');
});
});
6 changes: 3 additions & 3 deletions src/providers/google/chatComplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ export const GoogleChatCompleteStreamChunkTransform: (
strictOpenAiCompliance
)
: null;
if (generation.content?.parts[0]?.text) {
if (generation.content?.parts?.[0]?.text) {
const contentBlocks = [];
let content = '';
for (const part of generation.content.parts) {
Expand All @@ -836,7 +836,7 @@ export const GoogleChatCompleteStreamChunkTransform: (
...(!strictOpenAiCompliance &&
contentBlocks.length && { content_blocks: contentBlocks }),
};
} else if (generation.content?.parts[0]?.functionCall) {
} else if (generation.content?.parts?.[0]?.functionCall) {
message = {
role: 'assistant',
tool_calls: generation.content.parts.map((part, idx) => {
Expand All @@ -857,7 +857,7 @@ export const GoogleChatCompleteStreamChunkTransform: (
}
}),
};
} else if (generation.content?.parts[0]?.inlineData) {
} else if (generation.content?.parts?.[0]?.inlineData) {
const part = generation.content.parts[0];
const contentBlocks = [
{
Expand Down