Skip to content

Commit dad5849

Browse files
authored
google-common[minor]: Fix single token response issue (#5024)
1 parent aec025f commit dad5849

File tree

1 file changed

+15
-1
lines changed
  • libs/langchain-google-common/src/utils

1 file changed

+15
-1
lines changed

libs/langchain-google-common/src/utils/gemini.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,21 @@ export function responseToChatGenerations(
501501
response: GoogleLLMResponse
502502
): ChatGeneration[] {
503503
const parts = responseToParts(response);
504-
const ret = parts.map((part) => partToChatGeneration(part));
504+
let ret = parts.map((part) => partToChatGeneration(part));
505+
if (ret.every((item) => typeof item.message.content === "string")) {
506+
const combinedContent = ret.map((item) => item.message.content).join("");
507+
const combinedText = ret.map((item) => item.text).join("");
508+
ret = [
509+
new ChatGenerationChunk({
510+
message: new AIMessageChunk({
511+
content: combinedContent,
512+
additional_kwargs: ret[ret.length - 1].message.additional_kwargs,
513+
}),
514+
text: combinedText,
515+
generationInfo: ret[ret.length - 1].generationInfo,
516+
}),
517+
];
518+
}
505519
return ret;
506520
}
507521

0 commit comments

Comments
 (0)