From 844236da95042fa1d20fefccfbc9a1bcd5a70d6d Mon Sep 17 00:00:00 2001 From: Christian Bromann Date: Wed, 15 Oct 2025 14:47:38 +0200 Subject: [PATCH] fix(integration): fix OpenAI custom tools example in JS --- .../javascript/integrations/chat/openai.mdx | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/oss/javascript/integrations/chat/openai.mdx b/src/oss/javascript/integrations/chat/openai.mdx index 6472e2022..913e0f619 100644 --- a/src/oss/javascript/integrations/chat/openai.mdx +++ b/src/oss/javascript/integrations/chat/openai.mdx @@ -292,10 +292,10 @@ If you use a model that supports custom tools, you can use the `ChatOpenAI` clas ```typescript import { ChatOpenAI, customTool } from "@langchain/openai"; -import { createAgent } from "@langchain/classic"; +import { createAgent, HumanMessage } from "langchain"; const codeTool = customTool( - async (input) => { + async () => { // ... Add code to execute the input return "Code executed successfully"; }, @@ -309,11 +309,13 @@ const codeTool = customTool( const model = new ChatOpenAI({ model: "gpt-5" }); const agent = createAgent({ - llm: model, + model, tools: [codeTool], }); -const result = await agent.invoke("Use the tool to calculate 3^3"); +const result = await agent.invoke({ + messages: [new HumanMessage("Use the tool to execute the code")], +}); console.log(result); ``` @@ -324,7 +326,7 @@ OpenAI supports the specification of a [context-free grammar](https://platform.o ```typescript import { ChatOpenAI, customTool } from "@langchain/openai"; -import { createAgent } from "@langchain/classic"; +import { createAgent, HumanMessage } from "langchain"; const MATH_GRAMMAR = ` start: expr @@ -340,25 +342,27 @@ MUL: \"*\" `; const doMath = customTool( - async (input) => { + async () => { // ... Add code to parse and execute the input return "27"; }, { name: "do_math", description: "Evaluate a math expression", - format: { type: "grammar", grammar: MATH_GRAMMAR }, + format: { type: "grammar", definition: MATH_GRAMMAR, syntax: "lark" }, } ); const model = new ChatOpenAI({ model: "gpt-5" }); const agent = createAgent({ - llm: model, + model, tools: [doMath], }); -const result = await agent.invoke("Use the tool to calculate 3^3"); +const result = await agent.invoke({ + messages: [new HumanMessage("Use the tool to calculate 3^3")], +}); console.log(result); ```