Skip to content

Commit

Permalink
aws[patch]: Fix error when processing empty string chunk conent
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul committed Jul 18, 2024
1 parent 9b6a764 commit ca624aa
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
7 changes: 2 additions & 5 deletions libs/langchain-aws/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ export function handleConverseStreamContentBlockDelta(
if (!contentBlockDelta.delta) {
throw new Error("No delta found in content block.");
}
if (contentBlockDelta.delta.text) {
if (typeof contentBlockDelta.delta.text === "string") {
return new ChatGenerationChunk({
text: contentBlockDelta.delta.text,
message: new AIMessageChunk({
Expand All @@ -404,11 +404,8 @@ export function handleConverseStreamContentBlockDelta(
}),
});
} else {
const unsupportedField = Object.entries(contentBlockDelta.delta).filter(
([_, value]) => !!value
);
throw new Error(
`Unsupported content block type: ${unsupportedField[0][0]}`
`Unsupported content block type(s): ${JSON.stringify(contentBlockDelta.delta, null, 2)}`
);
}
}
Expand Down
37 changes: 36 additions & 1 deletion libs/langchain-aws/src/tests/chat_models.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import {
HumanMessage,
AIMessage,
ToolMessage,
AIMessageChunk,
} from "@langchain/core/messages";
import { convertToConverseMessages } from "../common.js";
import { convertToConverseMessages, handleConverseStreamContentBlockDelta } from "../common.js";
import { concat } from "@langchain/core/utils/stream";

test("convertToConverseMessages works", () => {
const messages = [
Expand Down Expand Up @@ -71,3 +73,36 @@ test("convertToConverseMessages works", () => {
url: "https://weather.com",
});
});

test("Streaming supports empty string chunks", async () => {
const contentBlocks = [
{
contentBlockIndex: 0,
delta: {
text: "Hello ",
}
},
{
contentBlockIndex: 0,
delta: {
text: "",
}
},
{
contentBlockIndex: 0,
delta: {
text: "world!",
}
}
]

let finalChunk: AIMessageChunk | undefined;
for (const block of contentBlocks) {
const chunk = handleConverseStreamContentBlockDelta(block).message;
finalChunk = !finalChunk ? chunk : concat(finalChunk, chunk);
}

expect(finalChunk).toBeDefined();
if (!finalChunk) return;
expect(finalChunk.content).toBe("Hello world!");
})

0 comments on commit ca624aa

Please sign in to comment.