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 deserialisation of additional_kwargs and tool_call_id #3721

Merged
merged 1 commit into from
Dec 20, 2023
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
10 changes: 10 additions & 0 deletions langchain-core/src/messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ export abstract class BaseMessage

lc_serializable = true;

get lc_aliases(): Record<string, string> {
// exclude snake case conversion to pascal case
return { additional_kwargs: "additional_kwargs" };
}

/**
* @deprecated
* Use {@link BaseMessage.content} instead.
Expand Down Expand Up @@ -464,6 +469,11 @@ export class ToolMessage extends BaseMessage {
return "ToolMessage";
}

get lc_aliases(): Record<string, string> {
// exclude snake case conversion to pascal case
return { tool_call_id: "tool_call_id" };
}

tool_call_id: string;

constructor(fields: ToolMessageFieldsWithToolCallId);
Expand Down
48 changes: 47 additions & 1 deletion langchain-core/src/messages/tests/base_message.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { test } from "@jest/globals";
import { ChatPromptTemplate } from "../../prompts/chat.js";
import { HumanMessage } from "../index.js";
import { HumanMessage, AIMessage, ToolMessage } from "../index.js";
import { load } from "../../load/index.js";

test("Test ChatPromptTemplate can format OpenAI content image messages", async () => {
const message = new HumanMessage({
Expand Down Expand Up @@ -61,3 +62,48 @@ test("Test ChatPromptTemplate can format OpenAI content image messages", async (
"Will this format with multiple messages?: YES!"
);
});

test("Deserialisation and serialisation of additional_kwargs and tool_call_id", async () => {
const config = {
importMap: { messages: { AIMessage } },
optionalImportEntrypoints: [],
optionalImportsMap: {},
secretsMap: {},
};

const message = new AIMessage({
content: "",
additional_kwargs: {
tool_calls: [
{
id: "call_tXJNP1S6LHT5tLfaNHCbYCtH",
type: "function" as const,
function: {
name: "Weather",
arguments: '{\n "location": "Prague"\n}',
},
},
],
},
});

const deserialized: AIMessage = await load(JSON.stringify(message), config);
expect(deserialized).toEqual(message);
});

test("Deserialisation and serialisation of tool_call_id", async () => {
const config = {
importMap: { messages: { ToolMessage } },
optionalImportEntrypoints: [],
optionalImportsMap: {},
secretsMap: {},
};

const message = new ToolMessage({
content: '{"value": 32}',
tool_call_id: "call_tXJNP1S6LHT5tLfaNHCbYCtH",
});

const deserialized: ToolMessage = await load(JSON.stringify(message), config);
expect(deserialized).toEqual(message);
});