Skip to content

Commit

Permalink
Fix deserialisation of additional_kwargs and tool_call_id (#3721)
Browse files Browse the repository at this point in the history
  • Loading branch information
dqbd authored Dec 20, 2023
1 parent c457030 commit d7c1cb7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
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);
});

2 comments on commit d7c1cb7

@vercel
Copy link

@vercel vercel bot commented on d7c1cb7 Dec 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

langchainjs-docs – ./docs/core_docs/

langchainjs-docs-git-main-langchain.vercel.app
langchainjs-docs-langchain.vercel.app
langchainjs-docs-ruddy.vercel.app
js.langchain.com

@vercel
Copy link

@vercel vercel bot commented on d7c1cb7 Dec 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.