This will cause an error to stop, please consider exception handling? #189

Closed
opened 2026-02-15 17:17:00 -05:00 by yindo · 16 comments
Owner

Originally created by @phpmac on GitHub (Mar 3, 2025).

/**
 * LangChain工具集成脚本
 * 实现了基础的数学运算和网络搜索功能
 */

import { ChatOpenAI } from "@langchain/openai";
import { createSupervisor } from "@langchain/langgraph-supervisor";
import { createReactAgent } from "@langchain/langgraph/prebuilt";
import { tool } from "@langchain/core/tools";
import { z } from "zod";
import dotenv from "dotenv";

dotenv.config();

const model = new ChatOpenAI({ modelName: process.env.OPENAI_MODEL });

/**
 * 加法工具
 * @param {number} a - 第一个数字
 * @param {number} b - 第二个数字
 * @returns {number} 两数之和
 */
const add = tool(
  async (args) => {
    console.debug("add", args);
    return args.a + args.b;
  },
  {
    name: "add",
    description: "将两个数字相加。",
    schema: z.object({
      a: z.number(),
      b: z.number(),
    }),
  }
);

/**
 * 乘法工具
 * @param {number} a - 第一个数字
 * @param {number} b - 第二个数字
 * @returns {number} 两数之积
 */
const multiply = tool(
  async (args) => {
    console.debug("multiply", args);
    return args.a * args.b;
  },
  {
    name: "multiply",
    description: "将两个数字相乘。",
    schema: z.object({
      a: z.number(),
      b: z.number(),
    }),
  }
);

/**
 * 网络搜索工具
 * @param {string} query - 搜索查询
 * @returns {string} 搜索结果
 */
const webSearch = tool(
  async (args) => {
    console.debug("webSearch", args);
    return (
      "以下是 2024 年 FAANG 公司的员工总数:\n" +
      "1. **Facebook (Meta)**: 67,317 名员工.\n" +
      "2. **Apple**: 164,000 名员工.\n" +
      "3. **Amazon**: 1,551,000 名员工.\n" +
      "4. **Netflix**: 14,000 名员工.\n" +
      "5. **Google (Alphabet)**: 181,269 名员工."
    );
  },
  {
    name: "web_search",
    description: "在网络上搜索信息。",
    schema: z.object({
      query: z.string(),
    }),
  }
);

// 创建数学专家代理
const mathAgent = createReactAgent({
  llm: model,
  tools: [add, multiply],
  name: "math_expert",
  prompt: "你是一位数学专家。每次只使用一个工具来解决问题。",
});

// 创建研究专家代理
const researchAgent = createReactAgent({
  llm: model,
  tools: [webSearch],
  name: "research_expert",
  prompt: "你是一位世界级的研究专家,可以使用网络搜索。不要进行任何数学计算。",
});

/**
 * 创建监督工作流
 * 用于协调研究专家和数学专家的工作
 */
const workflow = createSupervisor({
  agents: [researchAgent, mathAgent],
  llm: model,
  prompt:
    "你是一位团队主管,负责管理一位研究专家和一位数学专家。" +
    "对于当前事件相关的问题,使用研究专家(research_agent)。" +
    "对于数学问题,使用数学专家(math_agent)。",
});

// 编译并运行工作流
const app = workflow.compile();

const stream = await app.stream({
  messages: [
    {
      role: "user",
      // content: "2*2 最终结果是什么",
      content: "1+1 然后 2*2 最终结果是什么",
    },
  ],
});

for await (const chunk of stream) {
  if (chunk.supervisor) {
    console.debug(chunk.supervisor);
  } else {
    console.debug(chunk);
  }
}


error message:

file:///Users/a/Downloads/my-remix-app/node_modules/@langchain/core/dist/language_models/chat_models.js:64
        return chatGeneration.message;
                              ^

TypeError: Cannot read properties of undefined (reading 'message')
    at ChatOpenAI.invoke (file:///Users/a/Downloads/my-remix-app/node_modules/@langchain/core/dist/language_models/chat_models.js:64:31)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async RunnableSequence.invoke (file:///Users/a/Downloads/my-remix-app/node_modules/@langchain/core/dist/runnables/base.js:1280:27)
    at RunnableCallable.callModel (/Users/a/Downloads/my-remix-app/node_modules/@langchain/langgraph/src/prebuilt/react_agent_executor.ts:482:23)
    at RunnableCallable.invoke (/Users/a/Downloads/my-remix-app/node_modules/@langchain/langgraph/src/utils.ts:85:21)
    at async RunnableSequence.invoke (file:///Users/a/Downloads/my-remix-app/node_modules/@langchain/core/dist/runnables/base.js:1274:33)
    at _runWithRetry (/Users/a/Downloads/my-remix-app/node_modules/@langchain/langgraph/src/pregel/retry.ts:94:16)
    at PregelRunner._executeTasksWithRetry (/Users/a/Downloads/my-remix-app/node_modules/@langchain/langgraph/src/pregel/runner.ts:329:27)
    at PregelRunner.tick (/Users/a/Downloads/my-remix-app/node_modules/@langchain/langgraph/src/pregel/runner.ts:90:35)
    at CompiledStateGraph._runLoop (/Users/a/Downloads/my-remix-app/node_modules/@langchain/langgraph/src/pregel/index.ts:1709:9) {
  pregelTaskId: 'a31822b9-4134-5ce9-a1fa-adbaa766d17a'
}

Node.js v20.15.1

Because "1+1 then 2*2 what is the final result" any input will cause the program to break, I think it should be handled, any suggestions?

因为 "1+1 然后 2*2 最终结果是什么" 任意输入会导致程序中段,我认为应该处理,有什么建议吗?

Originally created by @phpmac on GitHub (Mar 3, 2025). ``` /** * LangChain工具集成脚本 * 实现了基础的数学运算和网络搜索功能 */ import { ChatOpenAI } from "@langchain/openai"; import { createSupervisor } from "@langchain/langgraph-supervisor"; import { createReactAgent } from "@langchain/langgraph/prebuilt"; import { tool } from "@langchain/core/tools"; import { z } from "zod"; import dotenv from "dotenv"; dotenv.config(); const model = new ChatOpenAI({ modelName: process.env.OPENAI_MODEL }); /** * 加法工具 * @param {number} a - 第一个数字 * @param {number} b - 第二个数字 * @returns {number} 两数之和 */ const add = tool( async (args) => { console.debug("add", args); return args.a + args.b; }, { name: "add", description: "将两个数字相加。", schema: z.object({ a: z.number(), b: z.number(), }), } ); /** * 乘法工具 * @param {number} a - 第一个数字 * @param {number} b - 第二个数字 * @returns {number} 两数之积 */ const multiply = tool( async (args) => { console.debug("multiply", args); return args.a * args.b; }, { name: "multiply", description: "将两个数字相乘。", schema: z.object({ a: z.number(), b: z.number(), }), } ); /** * 网络搜索工具 * @param {string} query - 搜索查询 * @returns {string} 搜索结果 */ const webSearch = tool( async (args) => { console.debug("webSearch", args); return ( "以下是 2024 年 FAANG 公司的员工总数:\n" + "1. **Facebook (Meta)**: 67,317 名员工.\n" + "2. **Apple**: 164,000 名员工.\n" + "3. **Amazon**: 1,551,000 名员工.\n" + "4. **Netflix**: 14,000 名员工.\n" + "5. **Google (Alphabet)**: 181,269 名员工." ); }, { name: "web_search", description: "在网络上搜索信息。", schema: z.object({ query: z.string(), }), } ); // 创建数学专家代理 const mathAgent = createReactAgent({ llm: model, tools: [add, multiply], name: "math_expert", prompt: "你是一位数学专家。每次只使用一个工具来解决问题。", }); // 创建研究专家代理 const researchAgent = createReactAgent({ llm: model, tools: [webSearch], name: "research_expert", prompt: "你是一位世界级的研究专家,可以使用网络搜索。不要进行任何数学计算。", }); /** * 创建监督工作流 * 用于协调研究专家和数学专家的工作 */ const workflow = createSupervisor({ agents: [researchAgent, mathAgent], llm: model, prompt: "你是一位团队主管,负责管理一位研究专家和一位数学专家。" + "对于当前事件相关的问题,使用研究专家(research_agent)。" + "对于数学问题,使用数学专家(math_agent)。", }); // 编译并运行工作流 const app = workflow.compile(); const stream = await app.stream({ messages: [ { role: "user", // content: "2*2 最终结果是什么", content: "1+1 然后 2*2 最终结果是什么", }, ], }); for await (const chunk of stream) { if (chunk.supervisor) { console.debug(chunk.supervisor); } else { console.debug(chunk); } } ``` error message: ``` file:///Users/a/Downloads/my-remix-app/node_modules/@langchain/core/dist/language_models/chat_models.js:64 return chatGeneration.message; ^ TypeError: Cannot read properties of undefined (reading 'message') at ChatOpenAI.invoke (file:///Users/a/Downloads/my-remix-app/node_modules/@langchain/core/dist/language_models/chat_models.js:64:31) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async RunnableSequence.invoke (file:///Users/a/Downloads/my-remix-app/node_modules/@langchain/core/dist/runnables/base.js:1280:27) at RunnableCallable.callModel (/Users/a/Downloads/my-remix-app/node_modules/@langchain/langgraph/src/prebuilt/react_agent_executor.ts:482:23) at RunnableCallable.invoke (/Users/a/Downloads/my-remix-app/node_modules/@langchain/langgraph/src/utils.ts:85:21) at async RunnableSequence.invoke (file:///Users/a/Downloads/my-remix-app/node_modules/@langchain/core/dist/runnables/base.js:1274:33) at _runWithRetry (/Users/a/Downloads/my-remix-app/node_modules/@langchain/langgraph/src/pregel/retry.ts:94:16) at PregelRunner._executeTasksWithRetry (/Users/a/Downloads/my-remix-app/node_modules/@langchain/langgraph/src/pregel/runner.ts:329:27) at PregelRunner.tick (/Users/a/Downloads/my-remix-app/node_modules/@langchain/langgraph/src/pregel/runner.ts:90:35) at CompiledStateGraph._runLoop (/Users/a/Downloads/my-remix-app/node_modules/@langchain/langgraph/src/pregel/index.ts:1709:9) { pregelTaskId: 'a31822b9-4134-5ce9-a1fa-adbaa766d17a' } Node.js v20.15.1 ``` Because "1+1 then 2*2 what is the final result" any input will cause the program to break, I think it should be handled, any suggestions? 因为 "1+1 然后 2*2 最终结果是什么" 任意输入会导致程序中段,我认为应该处理,有什么建议吗?
yindo added the langgraph-supervisor label 2026-02-15 17:17:00 -05:00
yindo closed this issue 2026-02-15 17:17:00 -05:00
Author
Owner

@phpmac commented on GitHub (Mar 3, 2025):

Image
@phpmac commented on GitHub (Mar 3, 2025): <img width="1624" alt="Image" src="https://github.com/user-attachments/assets/60180fde-fba1-427f-aee5-a9a00d65df20" />
Author
Owner

@benjamincburns commented on GitHub (Mar 4, 2025):

@vbarda might be one for you. Here's a translation of the code sample:

/**
 * LangChain Tool Integration Script
 * Implements basic mathematical operations and web search functionality
 */

import { ChatOpenAI } from "@langchain/openai";
import { createSupervisor } from "@langchain/langgraph-supervisor";
import { createReactAgent } from "@langchain/langgraph/prebuilt";
import { tool } from "@langchain/core/tools";
import { z } from "zod";
import dotenv from "dotenv";

dotenv.config();

const model = new ChatOpenAI({ modelName: process.env.OPENAI_MODEL });

/**
 * Addition tool
 * @param {number} a - First number
 * @param {number} b - Second number
 * @returns {number} Sum of the two numbers
 */
const add = tool(
  async (args) => {
    console.debug("add", args);
    return args.a + args.b;
  },
  {
    name: "add",
    description: "Add two numbers together.",
    schema: z.object({
      a: z.number(),
      b: z.number(),
    }),
  }
);

/**
 * Multiplication tool
 * @param {number} a - First number
 * @param {number} b - Second number
 * @returns {number} Product of the two numbers
 */
const multiply = tool(
  async (args) => {
    console.debug("multiply", args);
    return args.a * args.b;
  },
  {
    name: "multiply",
    description: "Multiply two numbers together.",
    schema: z.object({
      a: z.number(),
      b: z.number(),
    }),
  }
);

/**
 * Web search tool
 * @param {string} query - Search query
 * @returns {string} Search results
 */
const webSearch = tool(
  async (args) => {
    console.debug("webSearch", args);
    return (
      "Here are the total number of employees at FAANG companies in 2024:\n" +
      "1. **Facebook (Meta)**: 67,317 employees.\n" +
      "2. **Apple**: 164,000 employees.\n" +
      "3. **Amazon**: 1,551,000 employees.\n" +
      "4. **Netflix**: 14,000 employees.\n" +
      "5. **Google (Alphabet)**: 181,269 employees."
    );
  },
  {
    name: "web_search",
    description: "Search for information on the web.",
    schema: z.object({
      query: z.string(),
    }),
  }
);

// Create math expert agent
const mathAgent = createReactAgent({
  llm: model,
  tools: [add, multiply],
  name: "math_expert",
  prompt: "You are a mathematics expert. Use only one tool at a time to solve problems.",
});

// Create research expert agent
const researchAgent = createReactAgent({
  llm: model,
  tools: [webSearch],
  name: "research_expert",
  prompt: "You are a world-class research expert who can use web search. Do not perform any mathematical calculations.",
});

/**
 * Create supervisor workflow
 * Used to coordinate the work of the research expert and math expert
 */
const workflow = createSupervisor({
  agents: [researchAgent, mathAgent],
  llm: model,
  prompt:
    "You are a team supervisor responsible for managing a research expert and a math expert. " +
    "For questions related to current events, use the research expert (research_agent). " +
    "For mathematical problems, use the math expert (math_agent).",
});

// Compile and run the workflow
const app = workflow.compile();

const stream = await app.stream({
  messages: [
    {
      role: "user",
      // content: "What is the final result of 2*2",
      content: "1+1 and then 2*2 what is the final result",
    },
  ],
});

for await (const chunk of stream) {
  if (chunk.supervisor) {
    console.debug(chunk.supervisor);
  } else {
    console.debug(chunk);
  }
}

@phpmac 感谢你报告这个问题。根据你分享的代码,这个错误确实看起来很奇怪。请问你能告诉我你正在使用的 LangGraph 和 LangChain 库的版本吗?为了确保问题的准确性,请先测试最新版本并确认问题是否仍然存在。

@benjamincburns commented on GitHub (Mar 4, 2025): @vbarda might be one for you. Here's a translation of the code sample: ``` /** * LangChain Tool Integration Script * Implements basic mathematical operations and web search functionality */ import { ChatOpenAI } from "@langchain/openai"; import { createSupervisor } from "@langchain/langgraph-supervisor"; import { createReactAgent } from "@langchain/langgraph/prebuilt"; import { tool } from "@langchain/core/tools"; import { z } from "zod"; import dotenv from "dotenv"; dotenv.config(); const model = new ChatOpenAI({ modelName: process.env.OPENAI_MODEL }); /** * Addition tool * @param {number} a - First number * @param {number} b - Second number * @returns {number} Sum of the two numbers */ const add = tool( async (args) => { console.debug("add", args); return args.a + args.b; }, { name: "add", description: "Add two numbers together.", schema: z.object({ a: z.number(), b: z.number(), }), } ); /** * Multiplication tool * @param {number} a - First number * @param {number} b - Second number * @returns {number} Product of the two numbers */ const multiply = tool( async (args) => { console.debug("multiply", args); return args.a * args.b; }, { name: "multiply", description: "Multiply two numbers together.", schema: z.object({ a: z.number(), b: z.number(), }), } ); /** * Web search tool * @param {string} query - Search query * @returns {string} Search results */ const webSearch = tool( async (args) => { console.debug("webSearch", args); return ( "Here are the total number of employees at FAANG companies in 2024:\n" + "1. **Facebook (Meta)**: 67,317 employees.\n" + "2. **Apple**: 164,000 employees.\n" + "3. **Amazon**: 1,551,000 employees.\n" + "4. **Netflix**: 14,000 employees.\n" + "5. **Google (Alphabet)**: 181,269 employees." ); }, { name: "web_search", description: "Search for information on the web.", schema: z.object({ query: z.string(), }), } ); // Create math expert agent const mathAgent = createReactAgent({ llm: model, tools: [add, multiply], name: "math_expert", prompt: "You are a mathematics expert. Use only one tool at a time to solve problems.", }); // Create research expert agent const researchAgent = createReactAgent({ llm: model, tools: [webSearch], name: "research_expert", prompt: "You are a world-class research expert who can use web search. Do not perform any mathematical calculations.", }); /** * Create supervisor workflow * Used to coordinate the work of the research expert and math expert */ const workflow = createSupervisor({ agents: [researchAgent, mathAgent], llm: model, prompt: "You are a team supervisor responsible for managing a research expert and a math expert. " + "For questions related to current events, use the research expert (research_agent). " + "For mathematical problems, use the math expert (math_agent).", }); // Compile and run the workflow const app = workflow.compile(); const stream = await app.stream({ messages: [ { role: "user", // content: "What is the final result of 2*2", content: "1+1 and then 2*2 what is the final result", }, ], }); for await (const chunk of stream) { if (chunk.supervisor) { console.debug(chunk.supervisor); } else { console.debug(chunk); } } ``` @phpmac 感谢你报告这个问题。根据你分享的代码,这个错误确实看起来很奇怪。请问你能告诉我你正在使用的 LangGraph 和 LangChain 库的版本吗?为了确保问题的准确性,请先测试最新版本并确认问题是否仍然存在。
Author
Owner

@phpmac commented on GitHub (Mar 4, 2025):

{
  "name": "my-remix-app",
  "private": true,
  "sideEffects": false,
  "type": "module",
  "scripts": {
    "build": "remix vite:build",
    "dev": "remix vite:dev",
    "lint": "eslint --ignore-path .gitignore --cache --cache-location ./node_modules/.cache/eslint .",
    "start": "remix-serve ./build/server/index.js",
    "tree": "npx tsx scripts/tree.ts",
    "typecheck": "tsc"
  },
  "prisma": {
    "seed": "npx tsx prisma/seed.ts"
  },
  "dependencies": {
    "@huggingface/inference": "^2.8.1",
    "@huggingface/transformers": "^3.3.3",
    "@langchain/community": "^0.3.33",
    "@langchain/core": "^0.3.42",
    "@langchain/langgraph": "^0.2.49",
    "@langchain/langgraph-supervisor": "^0.0.7",
    "@langchain/openai": "^0.4.4",
    "@prisma/client": "^6.4.1",
    "@prisma/extension-accelerate": "^1.2.2",
    "@remix-pwa/worker-runtime": "^2.1.4",
    "@remix-run/node": "^2.15.3",
    "@remix-run/react": "^2.15.3",
    "@remix-run/serve": "^2.15.3",
    "@types/nodemailer": "^6.4.17",
    "@xenova/transformers": "^2.17.2",
    "bcryptjs": "^3.0.2",
    "dayjs": "^1.11.13",
    "isbot": "^4.1.0",
    "nodemailer": "^6.10.0",
    "pgvector": "^0.2.0",
    "prisma": "^6.4.1",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-markdown": "^10.0.0",
    "react-router-dom": "^6.29.0",
    "rehype-raw": "^7.0.0",
    "remark-gfm": "^4.0.1",
    "remix-auth": "^4.1.0",
    "remix-auth-form": "^3.0.0",
    "remix-utils": "^7.0.0-pre.7"
  },
  "devDependencies": {
    "@remix-pwa/dev": "^3.1.0",
    "@remix-run/dev": "^2.15.3",
    "@types/react": "^18.2.20",
    "@types/react-dom": "^18.2.7",
    "@types/ws": "^8.5.14",
    "@typescript-eslint/eslint-plugin": "^6.7.4",
    "@typescript-eslint/parser": "^6.7.4",
    "autoprefixer": "^10.4.19",
    "eslint": "^8.38.0",
    "eslint-import-resolver-typescript": "^3.6.1",
    "eslint-plugin-import": "^2.28.1",
    "eslint-plugin-jsx-a11y": "^6.7.1",
    "eslint-plugin-react": "^7.33.2",
    "eslint-plugin-react-hooks": "^4.6.0",
    "postcss": "^8.4.38",
    "tailwindcss": "^3.4.4",
    "typescript": "^5.1.6",
    "vite": "^5.1.0",
    "vite-tsconfig-paths": "^4.2.1"
  },
  "engines": {
    "node": ">=20.0.0"
  }
}

@vbarda might be one for you. Here's a translation of the code sample:

/**
 * LangChain Tool Integration Script
 * Implements basic mathematical operations and web search functionality
 */

import { ChatOpenAI } from "@langchain/openai";
import { createSupervisor } from "@langchain/langgraph-supervisor";
import { createReactAgent } from "@langchain/langgraph/prebuilt";
import { tool } from "@langchain/core/tools";
import { z } from "zod";
import dotenv from "dotenv";

dotenv.config();

const model = new ChatOpenAI({ modelName: process.env.OPENAI_MODEL });

/**
 * Addition tool
 * @param {number} a - First number
 * @param {number} b - Second number
 * @returns {number} Sum of the two numbers
 */
const add = tool(
  async (args) => {
    console.debug("add", args);
    return args.a + args.b;
  },
  {
    name: "add",
    description: "Add two numbers together.",
    schema: z.object({
      a: z.number(),
      b: z.number(),
    }),
  }
);

/**
 * Multiplication tool
 * @param {number} a - First number
 * @param {number} b - Second number
 * @returns {number} Product of the two numbers
 */
const multiply = tool(
  async (args) => {
    console.debug("multiply", args);
    return args.a * args.b;
  },
  {
    name: "multiply",
    description: "Multiply two numbers together.",
    schema: z.object({
      a: z.number(),
      b: z.number(),
    }),
  }
);

/**
 * Web search tool
 * @param {string} query - Search query
 * @returns {string} Search results
 */
const webSearch = tool(
  async (args) => {
    console.debug("webSearch", args);
    return (
      "Here are the total number of employees at FAANG companies in 2024:\n" +
      "1. **Facebook (Meta)**: 67,317 employees.\n" +
      "2. **Apple**: 164,000 employees.\n" +
      "3. **Amazon**: 1,551,000 employees.\n" +
      "4. **Netflix**: 14,000 employees.\n" +
      "5. **Google (Alphabet)**: 181,269 employees."
    );
  },
  {
    name: "web_search",
    description: "Search for information on the web.",
    schema: z.object({
      query: z.string(),
    }),
  }
);

// Create math expert agent
const mathAgent = createReactAgent({
  llm: model,
  tools: [add, multiply],
  name: "math_expert",
  prompt: "You are a mathematics expert. Use only one tool at a time to solve problems.",
});

// Create research expert agent
const researchAgent = createReactAgent({
  llm: model,
  tools: [webSearch],
  name: "research_expert",
  prompt: "You are a world-class research expert who can use web search. Do not perform any mathematical calculations.",
});

/**
 * Create supervisor workflow
 * Used to coordinate the work of the research expert and math expert
 */
const workflow = createSupervisor({
  agents: [researchAgent, mathAgent],
  llm: model,
  prompt:
    "You are a team supervisor responsible for managing a research expert and a math expert. " +
    "For questions related to current events, use the research expert (research_agent). " +
    "For mathematical problems, use the math expert (math_agent).",
});

// Compile and run the workflow
const app = workflow.compile();

const stream = await app.stream({
  messages: [
    {
      role: "user",
      // content: "What is the final result of 2*2",
      content: "1+1 and then 2*2 what is the final result",
    },
  ],
});

for await (const chunk of stream) {
  if (chunk.supervisor) {
    console.debug(chunk.supervisor);
  } else {
    console.debug(chunk);
  }
}

@phpmac 感谢你报告这个问题。根据你分享的代码,这个错误确实看起来很奇怪。请问你能告诉我你正在使用的 LangGraph 和 LangChain 库的版本吗?为了确保问题的准确性,请先测试最新版本并确认问题是否仍然存在。

@phpmac commented on GitHub (Mar 4, 2025): ``` { "name": "my-remix-app", "private": true, "sideEffects": false, "type": "module", "scripts": { "build": "remix vite:build", "dev": "remix vite:dev", "lint": "eslint --ignore-path .gitignore --cache --cache-location ./node_modules/.cache/eslint .", "start": "remix-serve ./build/server/index.js", "tree": "npx tsx scripts/tree.ts", "typecheck": "tsc" }, "prisma": { "seed": "npx tsx prisma/seed.ts" }, "dependencies": { "@huggingface/inference": "^2.8.1", "@huggingface/transformers": "^3.3.3", "@langchain/community": "^0.3.33", "@langchain/core": "^0.3.42", "@langchain/langgraph": "^0.2.49", "@langchain/langgraph-supervisor": "^0.0.7", "@langchain/openai": "^0.4.4", "@prisma/client": "^6.4.1", "@prisma/extension-accelerate": "^1.2.2", "@remix-pwa/worker-runtime": "^2.1.4", "@remix-run/node": "^2.15.3", "@remix-run/react": "^2.15.3", "@remix-run/serve": "^2.15.3", "@types/nodemailer": "^6.4.17", "@xenova/transformers": "^2.17.2", "bcryptjs": "^3.0.2", "dayjs": "^1.11.13", "isbot": "^4.1.0", "nodemailer": "^6.10.0", "pgvector": "^0.2.0", "prisma": "^6.4.1", "react": "^18.2.0", "react-dom": "^18.2.0", "react-markdown": "^10.0.0", "react-router-dom": "^6.29.0", "rehype-raw": "^7.0.0", "remark-gfm": "^4.0.1", "remix-auth": "^4.1.0", "remix-auth-form": "^3.0.0", "remix-utils": "^7.0.0-pre.7" }, "devDependencies": { "@remix-pwa/dev": "^3.1.0", "@remix-run/dev": "^2.15.3", "@types/react": "^18.2.20", "@types/react-dom": "^18.2.7", "@types/ws": "^8.5.14", "@typescript-eslint/eslint-plugin": "^6.7.4", "@typescript-eslint/parser": "^6.7.4", "autoprefixer": "^10.4.19", "eslint": "^8.38.0", "eslint-import-resolver-typescript": "^3.6.1", "eslint-plugin-import": "^2.28.1", "eslint-plugin-jsx-a11y": "^6.7.1", "eslint-plugin-react": "^7.33.2", "eslint-plugin-react-hooks": "^4.6.0", "postcss": "^8.4.38", "tailwindcss": "^3.4.4", "typescript": "^5.1.6", "vite": "^5.1.0", "vite-tsconfig-paths": "^4.2.1" }, "engines": { "node": ">=20.0.0" } } ``` > [@vbarda](https://github.com/vbarda) might be one for you. Here's a translation of the code sample: > > ``` > /** > * LangChain Tool Integration Script > * Implements basic mathematical operations and web search functionality > */ > > import { ChatOpenAI } from "@langchain/openai"; > import { createSupervisor } from "@langchain/langgraph-supervisor"; > import { createReactAgent } from "@langchain/langgraph/prebuilt"; > import { tool } from "@langchain/core/tools"; > import { z } from "zod"; > import dotenv from "dotenv"; > > dotenv.config(); > > const model = new ChatOpenAI({ modelName: process.env.OPENAI_MODEL }); > > /** > * Addition tool > * @param {number} a - First number > * @param {number} b - Second number > * @returns {number} Sum of the two numbers > */ > const add = tool( > async (args) => { > console.debug("add", args); > return args.a + args.b; > }, > { > name: "add", > description: "Add two numbers together.", > schema: z.object({ > a: z.number(), > b: z.number(), > }), > } > ); > > /** > * Multiplication tool > * @param {number} a - First number > * @param {number} b - Second number > * @returns {number} Product of the two numbers > */ > const multiply = tool( > async (args) => { > console.debug("multiply", args); > return args.a * args.b; > }, > { > name: "multiply", > description: "Multiply two numbers together.", > schema: z.object({ > a: z.number(), > b: z.number(), > }), > } > ); > > /** > * Web search tool > * @param {string} query - Search query > * @returns {string} Search results > */ > const webSearch = tool( > async (args) => { > console.debug("webSearch", args); > return ( > "Here are the total number of employees at FAANG companies in 2024:\n" + > "1. **Facebook (Meta)**: 67,317 employees.\n" + > "2. **Apple**: 164,000 employees.\n" + > "3. **Amazon**: 1,551,000 employees.\n" + > "4. **Netflix**: 14,000 employees.\n" + > "5. **Google (Alphabet)**: 181,269 employees." > ); > }, > { > name: "web_search", > description: "Search for information on the web.", > schema: z.object({ > query: z.string(), > }), > } > ); > > // Create math expert agent > const mathAgent = createReactAgent({ > llm: model, > tools: [add, multiply], > name: "math_expert", > prompt: "You are a mathematics expert. Use only one tool at a time to solve problems.", > }); > > // Create research expert agent > const researchAgent = createReactAgent({ > llm: model, > tools: [webSearch], > name: "research_expert", > prompt: "You are a world-class research expert who can use web search. Do not perform any mathematical calculations.", > }); > > /** > * Create supervisor workflow > * Used to coordinate the work of the research expert and math expert > */ > const workflow = createSupervisor({ > agents: [researchAgent, mathAgent], > llm: model, > prompt: > "You are a team supervisor responsible for managing a research expert and a math expert. " + > "For questions related to current events, use the research expert (research_agent). " + > "For mathematical problems, use the math expert (math_agent).", > }); > > // Compile and run the workflow > const app = workflow.compile(); > > const stream = await app.stream({ > messages: [ > { > role: "user", > // content: "What is the final result of 2*2", > content: "1+1 and then 2*2 what is the final result", > }, > ], > }); > > for await (const chunk of stream) { > if (chunk.supervisor) { > console.debug(chunk.supervisor); > } else { > console.debug(chunk); > } > } > ``` > > [@phpmac](https://github.com/phpmac) 感谢你报告这个问题。根据你分享的代码,这个错误确实看起来很奇怪。请问你能告诉我你正在使用的 LangGraph 和 LangChain 库的版本吗?为了确保问题的准确性,请先测试最新版本并确认问题是否仍然存在。
Author
Owner

@benjamincburns commented on GitHub (Mar 5, 2025):

@phpmac 我看到你使用的是 @langchain/langgraph-supervisor v0.0.7 版本。你能尝试升级到 v0.0.9 吗?我们在这个版本中修复了一些与你遇到的问题相关的问题。请注意,你可能还需要将 @langchain/langgraph 也升级到最新版本。

如果这样解决了你的问题,请告诉我们。如果问题仍然存在,我们会进一步调查。谢谢!

@benjamincburns commented on GitHub (Mar 5, 2025): @phpmac 我看到你使用的是 `@langchain/langgraph-supervisor` v0.0.7 版本。你能尝试升级到 v0.0.9 吗?我们在这个版本中修复了一些与你遇到的问题相关的问题。请注意,你可能还需要将 `@langchain/langgraph` 也升级到最新版本。 如果这样解决了你的问题,请告诉我们。如果问题仍然存在,我们会进一步调查。谢谢!
Author
Owner

@benjamincburns commented on GitHub (Mar 5, 2025):

@phpmac 抱歉 - 我刚才编辑了我的上一条消息 - 你需要的是 0.0.9 版本,而不是 0.0.8。

@benjamincburns commented on GitHub (Mar 5, 2025): @phpmac 抱歉 - 我刚才编辑了我的上一条消息 - 你需要的是 0.0.9 版本,而不是 0.0.8。
Author
Owner

@phpmac commented on GitHub (Mar 5, 2025):

@phpmac 抱歉 - 我刚才编辑了我的上一条消息 - 你需要的是 0.0.9 版本,而不是 0.0.8。

哈哈,你怎么使用中文回答,sir

@phpmac commented on GitHub (Mar 5, 2025): > @phpmac 抱歉 - 我刚才编辑了我的上一条消息 - 你需要的是 0.0.9 版本,而不是 0.0.8。 哈哈,你怎么使用中文回答,sir
Author
Owner

@benjamincburns commented on GitHub (Mar 5, 2025):

你的代码和最初的消息大多是用中文写的,所以我假设中文是你的母语。我认为这样我们可以更清楚地交流。如果你愿意,我们也可以用英语聊天!

@benjamincburns commented on GitHub (Mar 5, 2025): 你的代码和最初的消息大多是用中文写的,所以我假设中文是你的母语。我认为这样我们可以更清楚地交流。如果你愿意,我们也可以用英语聊天!
Author
Owner

@phpmac commented on GitHub (Mar 5, 2025):

似乎还是一样的错误

file:///Users/a/Downloads/my-remix-app/node_modules/@langchain/core/dist/language_models/chat_models.js:64
        return chatGeneration.message;
                              ^

TypeError: Cannot read properties of undefined (reading 'message')
    at ChatOpenAI.invoke (file:///Users/a/Downloads/my-remix-app/node_modules/@langchain/core/dist/language_models/chat_models.js:64:31)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async RunnableSequence.invoke (file:///Users/a/Downloads/my-remix-app/node_modules/@langchain/core/dis
    "@langchain/langgraph": "^0.2.53",
    "@langchain/langgraph-supervisor": "^0.0.9",
@phpmac commented on GitHub (Mar 5, 2025): 似乎还是一样的错误 ``` file:///Users/a/Downloads/my-remix-app/node_modules/@langchain/core/dist/language_models/chat_models.js:64 return chatGeneration.message; ^ TypeError: Cannot read properties of undefined (reading 'message') at ChatOpenAI.invoke (file:///Users/a/Downloads/my-remix-app/node_modules/@langchain/core/dist/language_models/chat_models.js:64:31) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async RunnableSequence.invoke (file:///Users/a/Downloads/my-remix-app/node_modules/@langchain/core/dis ``` ``` "@langchain/langgraph": "^0.2.53", "@langchain/langgraph-supervisor": "^0.0.9", ```
Author
Owner

@phpmac commented on GitHub (Mar 5, 2025):

Have you tested my code?

@phpmac commented on GitHub (Mar 5, 2025): Have you tested my code?
Author
Owner

@vbarda commented on GitHub (Mar 6, 2025):

@phpmac i tested your code and it runs correctly for me. i suspect that you have an old version of @langchain/openai -- please update to latest and see if it fixes the issue

@vbarda commented on GitHub (Mar 6, 2025): @phpmac i tested your code and it runs correctly for me. i suspect that you have an old version of `@langchain/openai` -- please update to latest and see if it fixes the issue
Author
Owner

@phpmac commented on GitHub (Mar 7, 2025):

我现在测试出来问题,

如果是 deepseek/deepseek-chat 会一直循环,没有报错,

如果是 openai/gpt-4o-mini 会提示

(node:63584) ExperimentalWarning: CommonJS module /opt/homebrew/lib/node_modules/npm/node_modules/debug/src/node.js is loading ES Module /opt/homebrew/lib/node_modules/npm/node_modules/supports-color/index.js using require().
Support for loading ES Module in require() is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
(node:63614) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
[
  {
    messages: [
      HumanMessage {
        "id": "4abeb1c1-c59f-49f1-b599-b01baa0cca05",
        "content": "1+1 然后 2*2 最终结果是什么",
        "additional_kwargs": {},
        "response_metadata": {}
      },
      AIMessage {
        "id": "gen-1741327719-D8cld8SXfMdlHOfvMXke",
        "content": "",
        "name": "supervisor",
        "additional_kwargs": {
          "tool_calls": [
            {
              "index": 0,
              "id": "call_0HuPt9HXRQAXNcoDs8Q3jGMp",
              "type": "function",
              "function": "[Object]"
            },
            {
              "index": 1,
              "id": "call_jci5ECQRbPKwpxGwwv6BTmWH",
              "type": "function",
              "function": "[Object]"
            }
          ]
        },
        "response_metadata": {
          "tokenUsage": {
            "promptTokens": 114,
            "completionTokens": 46,
            "totalTokens": 160
          },
          "finish_reason": "tool_calls",
          "model_name": "openai/gpt-4o-mini",
          "usage": {
            "prompt_tokens": 114,
            "completion_tokens": 46,
            "total_tokens": 160
          },
          "system_fingerprint": "fp_06737a9306"
        },
        "tool_calls": [
          {
            "name": "transfer_to_math_expert",
            "args": {},
            "type": "tool_call",
            "id": "call_0HuPt9HXRQAXNcoDs8Q3jGMp"
          },
          {
            "name": "transfer_to_math_expert",
            "args": {},
            "type": "tool_call",
            "id": "call_jci5ECQRbPKwpxGwwv6BTmWH"
          }
        ],
        "invalid_tool_calls": [],
        "usage_metadata": {
          "output_tokens": 46,
          "input_tokens": 114,
          "total_tokens": 160,
          "input_token_details": {},
          "output_token_details": {}
        }
      }
    ]
  },
  {
    messages: [
      ToolMessage {
        "id": "06dfbe83-9ba1-4e96-b5b9-85d3b8dc1739",
        "content": "Successfully transferred to math_expert",
        "name": "transfer_to_math_expert",
        "additional_kwargs": {},
        "response_metadata": {},
        "tool_call_id": "call_0HuPt9HXRQAXNcoDs8Q3jGMp"
      }
    ]
  }
]
file:///Users/a/Downloads/remix/node_modules/@langchain/core/dist/language_models/chat_models.js:64
        return chatGeneration.message;
                              ^

TypeError: Cannot read properties of undefined (reading 'message')
    at ChatOpenAI.invoke (file:///Users/a/Downloads/remix/node_modules/@langchain/core/dist/language_models/chat_models.js:64:31)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async RunnableSequence.invoke (file:///Users/a/Downloads/remix/node_modules/@langchain/core/dist/runnables/base.js:1280:27)
    at async RunnableCallable.callModel (/Users/a/Downloads/remix/node_modules/@langchain/langgraph/src/prebuilt/react_agent_executor.ts:482:23)
    at async RunnableCallable.invoke (/Users/a/Downloads/remix/node_modules/@langchain/langgraph/src/utils.ts:85:21)
    at async RunnableSequence.invoke (file:///Users/a/Downloads/remix/node_modules/@langchain/core/dist/runnables/base.js:1274:33)
    at async _runWithRetry (/Users/a/Downloads/remix/node_modules/@langchain/langgraph/src/pregel/retry.ts:94:16)
    at async PregelRunner._executeTasksWithRetry (/Users/a/Downloads/remix/node_modules/@langchain/langgraph/src/pregel/runner.ts:329:27)
    at async PregelRunner.tick (/Users/a/Downloads/remix/node_modules/@langchain/langgraph/src/pregel/runner.ts:90:35)
    at async CompiledStateGraph._runLoop (/Users/a/Downloads/remix/node_modules/@langchain/langgraph/src/pregel/index.ts:1709:9) {
  pregelTaskId: 'e7005d3e-3d7f-5b80-89a9-83a4398567a2'
}

Node.js v23.2.0

vbarda 你在使用哪个模型测试?

@phpmac commented on GitHub (Mar 7, 2025): 我现在测试出来问题, 如果是 deepseek/deepseek-chat 会一直循环,没有报错, 如果是 openai/gpt-4o-mini 会提示 ``` (node:63584) ExperimentalWarning: CommonJS module /opt/homebrew/lib/node_modules/npm/node_modules/debug/src/node.js is loading ES Module /opt/homebrew/lib/node_modules/npm/node_modules/supports-color/index.js using require(). Support for loading ES Module in require() is an experimental feature and might change at any time (Use `node --trace-warnings ...` to show where the warning was created) (node:63614) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead. (Use `node --trace-deprecation ...` to show where the warning was created) [ { messages: [ HumanMessage { "id": "4abeb1c1-c59f-49f1-b599-b01baa0cca05", "content": "1+1 然后 2*2 最终结果是什么", "additional_kwargs": {}, "response_metadata": {} }, AIMessage { "id": "gen-1741327719-D8cld8SXfMdlHOfvMXke", "content": "", "name": "supervisor", "additional_kwargs": { "tool_calls": [ { "index": 0, "id": "call_0HuPt9HXRQAXNcoDs8Q3jGMp", "type": "function", "function": "[Object]" }, { "index": 1, "id": "call_jci5ECQRbPKwpxGwwv6BTmWH", "type": "function", "function": "[Object]" } ] }, "response_metadata": { "tokenUsage": { "promptTokens": 114, "completionTokens": 46, "totalTokens": 160 }, "finish_reason": "tool_calls", "model_name": "openai/gpt-4o-mini", "usage": { "prompt_tokens": 114, "completion_tokens": 46, "total_tokens": 160 }, "system_fingerprint": "fp_06737a9306" }, "tool_calls": [ { "name": "transfer_to_math_expert", "args": {}, "type": "tool_call", "id": "call_0HuPt9HXRQAXNcoDs8Q3jGMp" }, { "name": "transfer_to_math_expert", "args": {}, "type": "tool_call", "id": "call_jci5ECQRbPKwpxGwwv6BTmWH" } ], "invalid_tool_calls": [], "usage_metadata": { "output_tokens": 46, "input_tokens": 114, "total_tokens": 160, "input_token_details": {}, "output_token_details": {} } } ] }, { messages: [ ToolMessage { "id": "06dfbe83-9ba1-4e96-b5b9-85d3b8dc1739", "content": "Successfully transferred to math_expert", "name": "transfer_to_math_expert", "additional_kwargs": {}, "response_metadata": {}, "tool_call_id": "call_0HuPt9HXRQAXNcoDs8Q3jGMp" } ] } ] file:///Users/a/Downloads/remix/node_modules/@langchain/core/dist/language_models/chat_models.js:64 return chatGeneration.message; ^ TypeError: Cannot read properties of undefined (reading 'message') at ChatOpenAI.invoke (file:///Users/a/Downloads/remix/node_modules/@langchain/core/dist/language_models/chat_models.js:64:31) at process.processTicksAndRejections (node:internal/process/task_queues:105:5) at async RunnableSequence.invoke (file:///Users/a/Downloads/remix/node_modules/@langchain/core/dist/runnables/base.js:1280:27) at async RunnableCallable.callModel (/Users/a/Downloads/remix/node_modules/@langchain/langgraph/src/prebuilt/react_agent_executor.ts:482:23) at async RunnableCallable.invoke (/Users/a/Downloads/remix/node_modules/@langchain/langgraph/src/utils.ts:85:21) at async RunnableSequence.invoke (file:///Users/a/Downloads/remix/node_modules/@langchain/core/dist/runnables/base.js:1274:33) at async _runWithRetry (/Users/a/Downloads/remix/node_modules/@langchain/langgraph/src/pregel/retry.ts:94:16) at async PregelRunner._executeTasksWithRetry (/Users/a/Downloads/remix/node_modules/@langchain/langgraph/src/pregel/runner.ts:329:27) at async PregelRunner.tick (/Users/a/Downloads/remix/node_modules/@langchain/langgraph/src/pregel/runner.ts:90:35) at async CompiledStateGraph._runLoop (/Users/a/Downloads/remix/node_modules/@langchain/langgraph/src/pregel/index.ts:1709:9) { pregelTaskId: 'e7005d3e-3d7f-5b80-89a9-83a4398567a2' } Node.js v23.2.0 ``` [vbarda](https://github.com/vbarda) 你在使用哪个模型测试?
Author
Owner

@phpmac commented on GitHub (Mar 7, 2025):

@phpmac i tested your code and it runs correctly for me. i suspect that you have an old version of @langchain/openai -- please update to latest and see if it fixes the issue

openai 版本是最新的

@phpmac commented on GitHub (Mar 7, 2025): > [@phpmac](https://github.com/phpmac) i tested your code and it runs correctly for me. i suspect that you have an old version of `@langchain/openai` -- please update to latest and see if it fixes the issue openai 版本是最新的
Author
Owner

@vbarda commented on GitHub (Mar 11, 2025):

@phpmac i was just testing on 4o. could you try installing in a fresh project and see if it fixes the issue for you?

@vbarda commented on GitHub (Mar 11, 2025): @phpmac i was just testing on `4o`. could you try installing in a fresh project and see if it fixes the issue for you?
Author
Owner

@phpmac commented on GitHub (Mar 11, 2025):

@phpmac i was just testing on 4o. could you try installing in a fresh project and see if it fixes the issue for you?

yes,4o it's works,but deepseek and other is wrong,But it doesn't matter, I have already abandoned the other models, let's move on, thank you for your efforts

@phpmac commented on GitHub (Mar 11, 2025): > [@phpmac](https://github.com/phpmac) i was just testing on `4o`. could you try installing in a fresh project and see if it fixes the issue for you? yes,4o it's works,but deepseek and other is wrong,But it doesn't matter, I have already abandoned the other models, let's move on, thank you for your efforts
Author
Owner

@vbarda commented on GitHub (Mar 11, 2025):

glad it's working with 4o - feel free to open issues in langchainjs repo for deepseek / other models

@vbarda commented on GitHub (Mar 11, 2025): glad it's working with 4o - feel free to open issues in `langchainjs` repo for deepseek / other models
Author
Owner

@benjamincburns commented on GitHub (Mar 13, 2025):

@phpmac 如果你还没有的话,我建议你将你遇到的 DeepSeek 集成问题在 LangChainJS 仓库中开一个 issue。这个集成对我们来说越来越重要,所以最好能记录下这个问题以便我们修复它。

@benjamincburns commented on GitHub (Mar 13, 2025): @phpmac 如果你还没有的话,我建议你将你遇到的 DeepSeek 集成问题在 LangChainJS 仓库中开一个 issue。这个集成对我们来说越来越重要,所以最好能记录下这个问题以便我们修复它。
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraphjs#189