Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
5 changes: 5 additions & 0 deletions .changeset/seven-oranges-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@firebase/vertexai': patch
---

Filter out empty text parts from streaming responses.
18 changes: 18 additions & 0 deletions packages/vertexai/src/requests/stream-reader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,24 @@ describe('processStream', () => {
}
expect(foundCitationMetadata).to.be.true;
});
it('removes empty text parts', async () => {
const fakeResponse = getMockResponseStreaming(
'streaming-success-empty-text-part.txt'
);
const result = processStream(fakeResponse as Response);
const aggregatedResponse = await result.response;
console.log(aggregatedResponse.candidates?.[0].content.parts);
expect(aggregatedResponse.text()).to.equal('1');
expect(aggregatedResponse.candidates?.length).to.equal(1);
expect(aggregatedResponse.candidates?.[0].content.parts.length).to.equal(1);

// The chunk with the empty text part will still go through the stream
let numChunks = 0;
for await (const _ of result.stream) {
numChunks++;
}
expect(numChunks).to.equal(2);
});
});

describe('aggregateResponses', () => {
Expand Down
9 changes: 8 additions & 1 deletion packages/vertexai/src/requests/stream-reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ async function getResponsePromise(
);
return enhancedResponse;
}

allResponses.push(value);
}
}
Expand Down Expand Up @@ -184,7 +185,13 @@ export function aggregateResponses(
}
const newPart: Partial<Part> = {};
for (const part of candidate.content.parts) {
if (part.text) {
if (part.text !== undefined) {
// The backend can send empty text parts. If these are sent back
// (e.g. in chat history), the backend will respond with an error.
// To prevent this, ignore empty text parts.
if (part.text === '') {
continue;
}
newPart.text = part.text;
}
if (part.functionCall) {
Expand Down
2 changes: 1 addition & 1 deletion scripts/update_vertexai_responses.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# This script replaces mock response files for Vertex AI unit tests with a fresh
# clone of the shared repository of Vertex AI test data.

RESPONSES_VERSION='v5.*' # The major version of mock responses to use
RESPONSES_VERSION='v6.*' # The major version of mock responses to use
REPO_NAME="vertexai-sdk-test-data"
REPO_LINK="https://github.com/FirebaseExtended/$REPO_NAME.git"

Expand Down
Loading