Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix mapStoredMessageToChatMessage to check on generic role when creating ChatMessage #6247

Merged
Show file tree
Hide file tree
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
23 changes: 22 additions & 1 deletion langchain-core/src/messages/tests/message_utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ import {
trimMessages,
} from "../transformers.js";
import { AIMessage } from "../ai.js";
import { ChatMessage } from "../chat.js";
import { HumanMessage } from "../human.js";
import { SystemMessage } from "../system.js";
import { BaseMessage } from "../base.js";
import { getBufferString } from "../utils.js";
import {
getBufferString,
mapChatMessagesToStoredMessages,
mapStoredMessagesToChatMessages,
} from "../utils.js";

describe("filterMessage", () => {
const getMessages = () => [
Expand Down Expand Up @@ -499,3 +504,19 @@ test("getBufferString can handle complex messages", () => {
)}`
);
});

describe("chat message conversions", () => {
it("can convert a chat message to a stored message and back", () => {
const originalMessages = [
new ChatMessage("I'm a generic message!", "human"),
new HumanMessage("I'm a human message!"),
];

const storedMessages = mapChatMessagesToStoredMessages(originalMessages);

const convertedBackMessages =
mapStoredMessagesToChatMessages(storedMessages);

expect(convertedBackMessages).toEqual(originalMessages);
});
});
2 changes: 1 addition & 1 deletion langchain-core/src/messages/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export function mapStoredMessageToChatMessage(message: StoredMessage) {
return new ToolMessage(
storedMessage.data as ToolMessageFieldsWithToolCallId
);
case "chat": {
case "generic": {
if (storedMessage.data.role === undefined) {
throw new Error("Role must be defined for chat messages");
}
Expand Down
2 changes: 1 addition & 1 deletion langchain-core/src/runnables/remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function revive(obj: any): any {
content: obj.content,
});
}
if (obj.type === "ChatMessage" || obj.type === "chat") {
if (obj.type === "ChatMessage" || obj.type === "generic") {
return new ChatMessage({
content: obj.content,
role: obj.role,
Expand Down
Loading