Skip to content

Commit df0cf7e

Browse files
committed
Update logic for skipping a respons during streaming
1 parent 4de8193 commit df0cf7e

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,13 @@ async function* generateResponseSequence(
100100
enhancedResponse = createEnhancedContentResponse(value);
101101
}
102102

103-
// Don't yield an empty parts response, skip it.
104-
if (!enhancedResponse.candidates?.[0].content?.parts) {
103+
const firstCandidate = enhancedResponse.candidates?.[0];
104+
// Don't yield a response with no useful data for the developer.
105+
if (
106+
!firstCandidate?.content?.parts &&
107+
!firstCandidate?.finishReason &&
108+
!firstCandidate?.citationMetadata
109+
) {
105110
continue;
106111
}
107112

@@ -215,14 +220,11 @@ export function aggregateResponses(
215220
}
216221
for (const part of candidate.content.parts) {
217222
const newPart: Part = { ...part };
218-
if (part.text !== undefined) {
219-
// The backend can send empty text parts. If these are sent back
220-
// (e.g. in chat history), the backend will respond with an error.
221-
// To prevent this, ignore empty text parts.
222-
if (part.text === '') {
223-
continue;
224-
}
225-
newPart.text = part.text;
223+
// The backend can send empty text parts. If these are sent back
224+
// (e.g. in chat history), the backend will respond with an error.
225+
// To prevent this, ignore empty text parts.
226+
if (part.text === '') {
227+
continue;
226228
}
227229
if (Object.keys(newPart).length > 0) {
228230
aggregatedResponse.candidates[i].content.parts.push(

0 commit comments

Comments
 (0)