Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
abdelkd committed Dec 22, 2024
1 parent 2df2af0 commit 676ab97
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/flat-gorillas-happen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@google/generative-ai": patch
---

Fix undefined candidate index
26 changes: 14 additions & 12 deletions src/requests/stream-reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,34 +140,35 @@ export function aggregateResponses(
};
for (const response of responses) {
if (response.candidates) {
let candidateIndex = 0;
for (const candidate of response.candidates) {
const i = candidate.index;
if (!aggregatedResponse.candidates) {
aggregatedResponse.candidates = [];
}
if (!aggregatedResponse.candidates[i]) {
aggregatedResponse.candidates[i] = {
index: candidate.index,
if (!aggregatedResponse.candidates[candidateIndex]) {
aggregatedResponse.candidates[candidateIndex] = {
index: candidateIndex,
} as GenerateContentCandidate;
}
// Keep overwriting, the last one will be final
aggregatedResponse.candidates[i].citationMetadata =
aggregatedResponse.candidates[candidateIndex].citationMetadata =
candidate.citationMetadata;
aggregatedResponse.candidates[i].groundingMetadata =
aggregatedResponse.candidates[candidateIndex].groundingMetadata =
candidate.groundingMetadata;
aggregatedResponse.candidates[i].finishReason = candidate.finishReason;
aggregatedResponse.candidates[i].finishMessage =
aggregatedResponse.candidates[candidateIndex].finishReason =
candidate.finishReason;
aggregatedResponse.candidates[candidateIndex].finishMessage =
candidate.finishMessage;
aggregatedResponse.candidates[i].safetyRatings =
aggregatedResponse.candidates[candidateIndex].safetyRatings =
candidate.safetyRatings;

/**
* Candidates should always have content and parts, but this handles
* possible malformed responses.
*/
if (candidate.content && candidate.content.parts) {
if (!aggregatedResponse.candidates[i].content) {
aggregatedResponse.candidates[i].content = {
if (!aggregatedResponse.candidates[candidateIndex].content) {
aggregatedResponse.candidates[candidateIndex].content = {
role: candidate.content.role || "user",
parts: [],
};
Expand All @@ -189,12 +190,13 @@ export function aggregateResponses(
if (Object.keys(newPart).length === 0) {
newPart.text = "";
}
aggregatedResponse.candidates[i].content.parts.push(
aggregatedResponse.candidates[candidateIndex].content.parts.push(
newPart as Part,
);
}
}
}
candidateIndex++;
}
if (response.usageMetadata) {
aggregatedResponse.usageMetadata = response.usageMetadata;
Expand Down

0 comments on commit 676ab97

Please sign in to comment.