Skip to content

Commit 3a99a40

Browse files
authored
fix(core): handle message coercion of RemoveMessage (#8941)
1 parent 6ab8a64 commit 3a99a40

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@langchain/core": patch
3+
---
4+
5+
Fix deserialization of RemoveMessage if represented as a plain object

langchain-core/src/messages/tests/base_message.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
AIMessage,
66
ToolMessage,
77
ToolMessageChunk,
8+
RemoveMessage,
89
AIMessageChunk,
910
coerceMessageLikeToMessage,
1011
SystemMessage,
@@ -551,6 +552,11 @@ describe("Message like coercion", () => {
551552
content: "10.2",
552553
tool_call_id: "10.2",
553554
},
555+
{
556+
role: "remove",
557+
id: "1234",
558+
content: "",
559+
},
554560
].map(coerceMessageLikeToMessage);
555561
expect(messages).toEqual([
556562
new SystemMessage({
@@ -583,6 +589,9 @@ describe("Message like coercion", () => {
583589
content: "10.2",
584590
tool_call_id: "10.2",
585591
}),
592+
new RemoveMessage({
593+
id: "1234",
594+
}),
586595
]);
587596
});
588597
it("should convert serialized messages", async () => {

langchain-core/src/messages/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
FunctionMessageChunk,
2323
} from "./function.js";
2424
import { HumanMessage, HumanMessageChunk } from "./human.js";
25+
import { RemoveMessage } from "./modifier.js";
2526
import { SystemMessage, SystemMessageChunk } from "./system.js";
2627
import {
2728
ToolCall,
@@ -132,6 +133,8 @@ function _constructMessageFromParams(
132133
tool_call_id: rest.tool_call_id as string,
133134
name: rest.name,
134135
});
136+
} else if (type === "remove" && "id" in rest && typeof rest.id === "string") {
137+
return new RemoveMessage({ ...rest, id: rest.id });
135138
} else {
136139
const error = addLangChainErrorFields(
137140
new Error(

0 commit comments

Comments
 (0)