Skip to content
Open
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
34 changes: 25 additions & 9 deletions js/plugins/google-genai/src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,12 +394,18 @@ async function* generateResponseSequence(
stream: ReadableStream<GenerateContentResponse>
): AsyncGenerator<GenerateContentResponse> {
const reader = stream.getReader();
while (true) {
const { value, done } = await reader.read();
if (done) {
break;
try {
while (true) {
const { value, done } = await reader.read();
if (done) {
break;
}
yield value;
}
yield value;
} catch (error) {
throw error;
} finally {
reader.releaseLock();
}
Comment on lines +405 to 409
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This catch block is redundant as it just re-throws the error. An error thrown within the try block will propagate automatically from the async generator. You can simplify the code by removing the catch block.

  } finally {
    reader.releaseLock();
  }

}

Expand All @@ -408,12 +414,22 @@ async function getResponsePromise(
): Promise<GenerateContentResponse> {
const allResponses: GenerateContentResponse[] = [];
const reader = stream.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) {
try {
while (true) {
const { done, value } = await reader.read();
if (done) {
return aggregateResponses(allResponses);
}
allResponses.push(value);
}
} catch (error) {
if (allResponses.length) {
return aggregateResponses(allResponses);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While returning a partial response on stream error is a reasonable strategy, the original error is completely swallowed here. This can make debugging difficult for the library user, as they would receive an incomplete response without any indication of what went wrong. Consider logging the error before returning the aggregated responses to provide visibility into the underlying issue. For example:

// Inside the catch block
if (allResponses.length) {
  console.error('Stream processing failed. Returning partial response. Error:', error);
  return aggregateResponses(allResponses);
}

}
allResponses.push(value);

throw error;
} finally {
reader.releaseLock();
}
}

Expand Down