langgraph-supervisor invoke fails with 'ParentCommand Error' when using nested supervisors #178

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

Originally created by @mwamedacen on GitHub (Feb 26, 2025).

Proof of concept 1 (extending first readme example):

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";

const model = new ChatOpenAI({ modelName: "gpt-4o", temperature: 0 });

// Create specialized agents
const add = tool(
  async (args) => args.a + args.b,
  {
    name: "add",
    description: "Add two numbers.",
    schema: z.object({
      a: z.number(),
      b: z.number()
    }),
    verbose: true // to valide the answer does not come from llm's inference
  }
);

const multiply = tool(
  async (args) => args.a * args.b,
  {
    name: "multiply", 
    description: "Multiply two numbers.",
    schema: z.object({
      a: z.number(),
      b: z.number()
    })
  }
);

const mathAddAgent = createReactAgent({
  llm: model,
  tools: [add, multiply],
  name: "math_add_expert",
  prompt: "You are a math add expert. Always use add tool."
});

const mathMultiplyAgent = createReactAgent({
    llm: model,
    tools: [multiply],
    name: "math_mulity_expert",
    prompt: "You are a math multiply expert. Always use multiply tool."
  });

const mathAgent = createSupervisor({
    agents: [mathAddAgent, mathMultiplyAgent],
    llm: model,
    prompt: 
      "You are a team supervisor managing a math add expert and a math multiply expert. " +
      "For math problems, use math_add_expert. " +
      "For math problems, use math_multiply_expert."
  }).compile({name: 'math_agent'})


const webSearch = tool(
    async (args) => {
      return (
        "Here are the headcounts for each of the 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 the web for information.",
      schema: z.object({
        query: z.string()
      })
    }
  );
  
// Create supervisor workflow
const workflow = createSupervisor({
    agents: [mathAgent, webSearch],
    llm: model,
    prompt: 
      "You are a team supervisor managing a research expert and a math expert. " +
      "For current events, use research_agent. " +
      "For math problems, use math_agent."
  });

const app = workflow.compile();

const result = await app.invoke({
  messages: [
    {
      role: "user",
      content: "what's 1 + 1 ? don't answer yourself always delegate to specialised agents"
    }
  ]
});

console.log(result.messages[result.messages.length - 1].content);

Error:

Waiting for the debugger to disconnect...
file:///Users/mwamedacen/Desktop/projects/blockanalytica/node_modules/.pnpm/@langchain+langgraph@0.2.49_@langchain+core@0.3.40_openai@4.85.4_ws@8.18.0_zod@3.24.2__/node_modules/@langchain/langgraph/dist/graph/state.js:595
            throw new ParentCommand(command);
                  ^

ParentCommand
    at RunnableCallable._controlBranch [as func] (file:///Users/mwamedacen/Desktop/projects/blockanalytica/node_modules/.pnpm/@langchain+langgraph@0.2.49_@langchain+core@0.3.40_openai@4.85.4_ws@8.18.0_zod@3.24.2__/node_modules/@langchain/langgraph/dist/graph/state.js:595:19)
    at file:///Users/mwamedacen/Desktop/projects/blockanalytica/node_modules/.pnpm/@langchain+langgraph@0.2.49_@langchain+core@0.3.40_openai@4.85.4_ws@8.18.0_zod@3.24.2__/node_modules/@langchain/langgraph/dist/utils.js:79:113
    at AsyncLocalStorage.run (node:internal/async_local_storage/async_hooks:91:14)
    at AsyncLocalStorageProvider.runWithConfig (file:///Users/mwamedacen/Desktop/projects/blockanalytica/node_modules/.pnpm/@langchain+core@0.3.40_openai@4.85.4_ws@8.18.0_zod@3.24.2_/node_modules/@langchain/core/dist/singletons/async_local_storage/index.js:53:24)
    at RunnableCallable.invoke (file:///Users/mwamedacen/Desktop/projects/blockanalytica/node_modules/.pnpm/@langchain+langgraph@0.2.49_@langchain+core@0.3.40_openai@4.85.4_ws@8.18.0_zod@3.24.2__/node_modules/@langchain/langgraph/dist/utils.js:79:68)
    at Branch._route (file:///Users/mwamedacen/Desktop/projects/blockanalytica/node_modules/.pnpm/@langchain+langgraph@0.2.49_@langchain+core@0.3.40_openai@4.85.4_ws@8.18.0_zod@3.24.2__/node_modules/@langchain/langgraph/dist/graph/graph.js:65:43)
    at RunnableCallable.func (file:///Users/mwamedacen/Desktop/projects/blockanalytica/node_modules/.pnpm/@langchain+langgraph@0.2.49_@langchain+core@0.3.40_openai@4.85.4_ws@8.18.0_zod@3.24.2__/node_modules/@langchain/langgraph/dist/graph/graph.js:48:39)
    at file:///Users/mwamedacen/Desktop/projects/blockanalytica/node_modules/.pnpm/@langchain+langgraph@0.2.49_@langchain+core@0.3.40_openai@4.85.4_ws@8.18.0_zod@3.24.2__/node_modules/@langchain/langgraph/dist/utils.js:79:113
    at AsyncLocalStorage.run (node:internal/async_local_storage/async_hooks:91:14)
    at AsyncLocalStorageProvider.runWithConfig (file:///Users/mwamedacen/Desktop/projects/blockanalytica/node_modules/.pnpm/@langchain+core@0.3.40_openai@4.85.4_ws@8.18.0_zod@3.24.2_/node_modules/@langchain/core/dist/singletons/async_local_storage/index.js:53:24) {
  lc_error_code: undefined,
  command: Command {
    lg_name: 'Command',
    lc_direct_tool_output: true,
    graph: 'math_agent:1380b467-2203-5352-8d60-58c23285c129|supervisor:fef44ef8-3a56-5f86-85a1-c0f95b7cc477',
    update: [
      [
        'messages',
        [
          HumanMessage {
            lc_serializable: true,
            lc_kwargs: [Object],
            lc_namespace: [Array],
            content: "what's 1 + 1 ? don't answer yourself, delegate to specialised agents",
            name: undefined,
            additional_kwargs: {},
            response_metadata: {},
            id: '842602ac-8088-4213-8343-5e24be8e6353'
          },
          AIMessage {
            lc_serializable: true,
            lc_kwargs: [Object],
            lc_namespace: [Array],
            content: '',
            name: 'supervisor',
            additional_kwargs: [Object],
            response_metadata: [Object],
            id: 'chatcmpl-B5H7EHupGq7MCtK6cFXbel20mdfMV',
            tool_calls: [Array],
            invalid_tool_calls: [],
            usage_metadata: [Object]
          },
          ToolMessage {
            lc_serializable: true,
            lc_kwargs: [Object],
            lc_namespace: [Array],
            content: 'Successfully transferred to math_agent',
            name: 'transfer_to_math_agent',
            additional_kwargs: {},
            response_metadata: {},
            id: 'fd254135-2293-4f0b-92ec-c91be981198b',
            lc_direct_tool_output: true,
            status: undefined,
            tool_call_id: 'call_puCy3lRU4lmAVWnGUjQLP1cA',
            artifact: undefined
          }
        ]
      ],
      [ 'structuredResponse', null ],
      [
        'messages',
        [
          HumanMessage {
            lc_serializable: true,
            lc_kwargs: [Object],
            lc_namespace: [Array],
            content: "what's 1 + 1 ? don't answer yourself, delegate to specialised agents",
            name: undefined,
            additional_kwargs: {},
            response_metadata: {},
            id: '842602ac-8088-4213-8343-5e24be8e6353'
          },
          AIMessage {
            lc_serializable: true,
            lc_kwargs: [Object],
            lc_namespace: [Array],
            content: '',
            name: 'supervisor',
            additional_kwargs: [Object],
            response_metadata: [Object],
            id: 'chatcmpl-B5H7EHupGq7MCtK6cFXbel20mdfMV',
            tool_calls: [Array],
            invalid_tool_calls: [],
            usage_metadata: [Object]
          },
          ToolMessage {
            lc_serializable: true,
            lc_kwargs: [Object],
            lc_namespace: [Array],
            content: 'Successfully transferred to math_agent',
            name: 'transfer_to_math_agent',
            additional_kwargs: {},
            response_metadata: {},
            id: 'fd254135-2293-4f0b-92ec-c91be981198b',
            lc_direct_tool_output: true,
            status: undefined,
            tool_call_id: 'call_puCy3lRU4lmAVWnGUjQLP1cA',
            artifact: undefined
          }
        ]
      ],
      [ 'structuredResponse', null ],
      [
        'messages',
        [
          HumanMessage {
            lc_serializable: true,
            lc_kwargs: [Object],
            lc_namespace: [Array],
            content: "what's 1 + 1 ? don't answer yourself, delegate to specialised agents",
            name: undefined,
            additional_kwargs: {},
            response_metadata: {},
            id: '842602ac-8088-4213-8343-5e24be8e6353'
          },
          AIMessage {
            lc_serializable: true,
            lc_kwargs: [Object],
            lc_namespace: [Array],
            content: '',
            name: 'supervisor',
            additional_kwargs: [Object],
            response_metadata: [Object],
            id: 'chatcmpl-B5H7EHupGq7MCtK6cFXbel20mdfMV',
            tool_calls: [Array],
            invalid_tool_calls: [],
            usage_metadata: [Object]
          },
          ToolMessage {
            lc_serializable: true,
            lc_kwargs: [Object],
            lc_namespace: [Array],
            content: 'Successfully transferred to math_agent',
            name: 'transfer_to_math_agent',
            additional_kwargs: {},
            response_metadata: {},
            id: 'fd254135-2293-4f0b-92ec-c91be981198b',
            lc_direct_tool_output: true,
            status: undefined,
            tool_call_id: 'call_puCy3lRU4lmAVWnGUjQLP1cA',
            artifact: undefined
          },
          AIMessage {
            lc_serializable: true,
            lc_kwargs: [Object],
            lc_namespace: [Array],
            content: '',
            name: 'supervisor',
            additional_kwargs: [Object],
            response_metadata: [Object],
            id: 'chatcmpl-B5H7FcMGK3hDAH4DdwdYD0uBsO5gb',
            tool_calls: [Array],
            invalid_tool_calls: [],
            usage_metadata: [Object]
          }
        ]
      ],
      [ 'structuredResponse', null ],
      [
        'messages',
        [
          ToolMessage {
            lc_serializable: true,
            lc_kwargs: [Object],
            lc_namespace: [Array],
            content: 'Successfully transferred to math_add_expert',
            name: 'transfer_to_math_add_expert',
            additional_kwargs: {},
            response_metadata: {},
            id: undefined,
            lc_direct_tool_output: true,
            status: undefined,
            tool_call_id: 'call_ZvD8brjwFkA7tk72Yi4dHmqi',
            artifact: undefined
          }
        ]
      ]
    ],
    resume: undefined,
    goto: [ 'math_add_expert' ]
  },
  pregelTaskId: '1380b467-2203-5352-8d60-58c23285c129'
}

Node.js v22.9.0

Proof of concept 2 (extending multi supervisor example):

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";

const model = new ChatOpenAI({ modelName: "gpt-4o", temperature: 0 });

// Create specialized agents
const add = tool(
  async (args) => args.a + args.b,
  {
    name: "add",
    description: "Add two numbers.",
    schema: z.object({
      a: z.number(),
      b: z.number()
    }),
    verbose: true
  }
);

const multiply = tool(
  async (args) => args.a * args.b,
  {
    name: "multiply", 
    description: "Multiply two numbers.",
    schema: z.object({
      a: z.number(),
      b: z.number()
    }),
    verbose: true
  }
);

const webSearch = tool(
  async (args) => {
    return (
      "Here are the headcounts for each of the 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 the web for information.",
    schema: z.object({
      query: z.string()
    }),
    verbose: true
  }
);

const writingTool = tool(
  async (args) => {
    return `Drafted article about: ${args.topic}`;
  },
  {
    name: "write_article",
    description: "Write an article on a given topic.",
    schema: z.object({
      topic: z.string()
    }),
    verbose: true
  }
);

const publishingTool = tool(
  async (args) => {
    return `Published article: ${args.article}`;
  },
  {
    name: "publish_article",
    description: "Publish a written article.",
    schema: z.object({
      article: z.string()
    }),
    verbose: true
  }
);

const mathAgent = createReactAgent({
  llm: model,
  tools: [add, multiply],
  name: "math_expert",
  prompt: "You are a math expert. Always use one tool at a time."
});

const researchAgent = createReactAgent({
  llm: model,
  tools: [webSearch],
  name: "research_expert",
  prompt: "You are a world class researcher with access to web search. Do not do any math."
});

const writingAgent = createReactAgent({
  llm: model,
  tools: [writingTool],
  name: "writing_expert",
  prompt: "You are an expert writer. Focus on creating engaging content."
});

const publishingAgent = createReactAgent({
  llm: model,
  tools: [publishingTool],
  name: "publishing_expert",
  prompt: "You are a publishing expert. Handle the publication process professionally."
});

// Create team supervisors
const researchTeam = createSupervisor({
  agents: [researchAgent, mathAgent],
  llm: model,
  prompt: "You are a research team supervisor. Coordinate research and math tasks effectively."
}).compile({ name: "research_team" });

const writingTeam = createSupervisor({
  agents: [writingAgent, publishingAgent],
  llm: model,
  prompt: "You are a writing team supervisor. Coordinate content creation and publication."
}).compile({ name: "writing_team" });

// Create top-level supervisor
const topLevelSupervisor = createSupervisor({
  agents: [researchTeam, writingTeam],
  llm: model,
  prompt: "You are an executive supervisor coordinating research and writing teams. Delegate tasks appropriately."
}).compile({ name: "top_level_supervisor" });

// Run the workflow
const result = await topLevelSupervisor.invoke({
  messages: [
    {
      role: "user",
      content: "what's 1+1 ? Don't guess the answer rely on research_team"
    }
  ]
});

console.log(result.messages[result.messages.length - 1].content);

Error:

ile:///Users/mwamedacen/Desktop/projects/blockanalytica/node_modules/.pnpm/@langchain+langgraph@0.2.49_@langchain+core@0.3.40_openai@4.85.4_ws@8.18.0_zod@3.24.2__/node_modules/@langchain/langgraph/dist/graph/state.js:595
            throw new ParentCommand(command);
                  ^

ParentCommand
    at RunnableCallable._controlBranch [as func] (file:///Users/mwamedacen/Desktop/projects/blockanalytica/node_modules/.pnpm/@langchain+langgraph@0.2.49_@langchain+core@0.3.40_openai@4.85.4_ws@8.18.0_zod@3.24.2__/node_modules/@langchain/langgraph/dist/graph/state.js:595:19)
    at file:///Users/mwamedacen/Desktop/projects/blockanalytica/node_modules/.pnpm/@langchain+langgraph@0.2.49_@langchain+core@0.3.40_openai@4.85.4_ws@8.18.0_zod@3.24.2__/node_modules/@langchain/langgraph/dist/utils.js:79:113
    at AsyncLocalStorage.run (node:internal/async_local_storage/async_hooks:91:14)
    at AsyncLocalStorageProvider.runWithConfig (file:///Users/mwamedacen/Desktop/projects/blockanalytica/node_modules/.pnpm/@langchain+core@0.3.40_openai@4.85.4_ws@8.18.0_zod@3.24.2_/node_modules/@langchain/core/dist/singletons/async_local_storage/index.js:53:24)
    at RunnableCallable.invoke (file:///Users/mwamedacen/Desktop/projects/blockanalytica/node_modules/.pnpm/@langchain+langgraph@0.2.49_@langchain+core@0.3.40_openai@4.85.4_ws@8.18.0_zod@3.24.2__/node_modules/@langchain/langgraph/dist/utils.js:79:68)
    at Branch._route (file:///Users/mwamedacen/Desktop/projects/blockanalytica/node_modules/.pnpm/@langchain+langgraph@0.2.49_@langchain+core@0.3.40_openai@4.85.4_ws@8.18.0_zod@3.24.2__/node_modules/@langchain/langgraph/dist/graph/graph.js:65:43)
    at RunnableCallable.func (file:///Users/mwamedacen/Desktop/projects/blockanalytica/node_modules/.pnpm/@langchain+langgraph@0.2.49_@langchain+core@0.3.40_openai@4.85.4_ws@8.18.0_zod@3.24.2__/node_modules/@langchain/langgraph/dist/graph/graph.js:48:39)
    at file:///Users/mwamedacen/Desktop/projects/blockanalytica/node_modules/.pnpm/@langchain+langgraph@0.2.49_@langchain+core@0.3.40_openai@4.85.4_ws@8.18.0_zod@3.24.2__/node_modules/@langchain/langgraph/dist/utils.js:79:113
    at AsyncLocalStorage.run (node:internal/async_local_storage/async_hooks:91:14)
    at AsyncLocalStorageProvider.runWithConfig (file:///Users/mwamedacen/Desktop/projects/blockanalytica/node_modules/.pnpm/@langchain+core@0.3.40_openai@4.85.4_ws@8.18.0_zod@3.24.2_/node_modules/@langchain/core/dist/singletons/async_local_storage/index.js:53:24) {
  lc_error_code: undefined,
  command: Command {
    lg_name: 'Command',
    lc_direct_tool_output: true,
    graph: 'research_team:52740bcf-fc8b-5987-ac8c-3465b699ea33|supervisor:f3105a4a-526a-5673-a6d0-e3ea3b8f1789',
    update: [
      [
        'messages',
        [
          HumanMessage {
            lc_serializable: true,
            lc_kwargs: [Object],
            lc_namespace: [Array],
            content: "what's 1+1 ? Don't guess the answer rely on research_team",
            name: undefined,
            additional_kwargs: {},
            response_metadata: {},
            id: '085adb01-f101-4898-ac01-2c4b2636146a'
          },
          AIMessage {
            lc_serializable: true,
            lc_kwargs: [Object],
            lc_namespace: [Array],
            content: '',
            name: 'supervisor',
            additional_kwargs: [Object],
            response_metadata: [Object],
            id: 'chatcmpl-B5HQVFWX9ZJtKJEQ06IybWIl2RW6V',
            tool_calls: [Array],
            invalid_tool_calls: [],
            usage_metadata: [Object]
          },
          ToolMessage {
            lc_serializable: true,
            lc_kwargs: [Object],
            lc_namespace: [Array],
            content: 'Successfully transferred to research_team',
            name: 'transfer_to_research_team',
            additional_kwargs: {},
            response_metadata: {},
            id: '25e86432-0bbd-4c21-97ca-d4dee518eb1d',
            lc_direct_tool_output: true,
            status: undefined,
            tool_call_id: 'call_x9pJIb7Xt3zocInFXRcrr0gc',
            artifact: undefined
          }
        ]
      ],
      [ 'structuredResponse', null ],
      [
        'messages',
        [
          HumanMessage {
            lc_serializable: true,
            lc_kwargs: [Object],
            lc_namespace: [Array],
            content: "what's 1+1 ? Don't guess the answer rely on research_team",
            name: undefined,
            additional_kwargs: {},
            response_metadata: {},
            id: '085adb01-f101-4898-ac01-2c4b2636146a'
          },
          AIMessage {
            lc_serializable: true,
            lc_kwargs: [Object],
            lc_namespace: [Array],
            content: '',
            name: 'supervisor',
            additional_kwargs: [Object],
            response_metadata: [Object],
            id: 'chatcmpl-B5HQVFWX9ZJtKJEQ06IybWIl2RW6V',
            tool_calls: [Array],
            invalid_tool_calls: [],
            usage_metadata: [Object]
          },
          ToolMessage {
            lc_serializable: true,
            lc_kwargs: [Object],
            lc_namespace: [Array],
            content: 'Successfully transferred to research_team',
            name: 'transfer_to_research_team',
            additional_kwargs: {},
            response_metadata: {},
            id: '25e86432-0bbd-4c21-97ca-d4dee518eb1d',
            lc_direct_tool_output: true,
            status: undefined,
            tool_call_id: 'call_x9pJIb7Xt3zocInFXRcrr0gc',
            artifact: undefined
          }
        ]
      ],
      [ 'structuredResponse', null ],
      [
        'messages',
        [
          HumanMessage {
            lc_serializable: true,
            lc_kwargs: [Object],
            lc_namespace: [Array],
            content: "what's 1+1 ? Don't guess the answer rely on research_team",
            name: undefined,
            additional_kwargs: {},
            response_metadata: {},
            id: '085adb01-f101-4898-ac01-2c4b2636146a'
          },
          AIMessage {
            lc_serializable: true,
            lc_kwargs: [Object],
            lc_namespace: [Array],
            content: '',
            name: 'supervisor',
            additional_kwargs: [Object],
            response_metadata: [Object],
            id: 'chatcmpl-B5HQVFWX9ZJtKJEQ06IybWIl2RW6V',
            tool_calls: [Array],
            invalid_tool_calls: [],
            usage_metadata: [Object]
          },
          ToolMessage {
            lc_serializable: true,
            lc_kwargs: [Object],
            lc_namespace: [Array],
            content: 'Successfully transferred to research_team',
            name: 'transfer_to_research_team',
            additional_kwargs: {},
            response_metadata: {},
            id: '25e86432-0bbd-4c21-97ca-d4dee518eb1d',
            lc_direct_tool_output: true,
            status: undefined,
            tool_call_id: 'call_x9pJIb7Xt3zocInFXRcrr0gc',
            artifact: undefined
          },
          AIMessage {
            lc_serializable: true,
            lc_kwargs: [Object],
            lc_namespace: [Array],
            content: '',
            name: 'supervisor',
            additional_kwargs: [Object],
            response_metadata: [Object],
            id: 'chatcmpl-B5HQW4u28Gkc9j1vHnp4R2Vjdbkk2',
            tool_calls: [Array],
            invalid_tool_calls: [],
            usage_metadata: [Object]
          }
        ]
      ],
      [ 'structuredResponse', null ],
      [
        'messages',
        [
          ToolMessage {
            lc_serializable: true,
            lc_kwargs: [Object],
            lc_namespace: [Array],
            content: 'Successfully transferred to math_expert',
            name: 'transfer_to_math_expert',
            additional_kwargs: {},
            response_metadata: {},
            id: undefined,
            lc_direct_tool_output: true,
            status: undefined,
            tool_call_id: 'call_RWt6E0DzJWUhQcZfzfUBuSRN',
            artifact: undefined
          }
        ]
      ]
    ],
    resume: undefined,
    goto: [ 'math_expert' ]
  },
  pregelTaskId: '52740bcf-fc8b-5987-ac8c-3465b699ea33'
}

Node.js v22.9.0
Originally created by @mwamedacen on GitHub (Feb 26, 2025). Proof of concept 1 (extending first readme example): ```typescript 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"; const model = new ChatOpenAI({ modelName: "gpt-4o", temperature: 0 }); // Create specialized agents const add = tool( async (args) => args.a + args.b, { name: "add", description: "Add two numbers.", schema: z.object({ a: z.number(), b: z.number() }), verbose: true // to valide the answer does not come from llm's inference } ); const multiply = tool( async (args) => args.a * args.b, { name: "multiply", description: "Multiply two numbers.", schema: z.object({ a: z.number(), b: z.number() }) } ); const mathAddAgent = createReactAgent({ llm: model, tools: [add, multiply], name: "math_add_expert", prompt: "You are a math add expert. Always use add tool." }); const mathMultiplyAgent = createReactAgent({ llm: model, tools: [multiply], name: "math_mulity_expert", prompt: "You are a math multiply expert. Always use multiply tool." }); const mathAgent = createSupervisor({ agents: [mathAddAgent, mathMultiplyAgent], llm: model, prompt: "You are a team supervisor managing a math add expert and a math multiply expert. " + "For math problems, use math_add_expert. " + "For math problems, use math_multiply_expert." }).compile({name: 'math_agent'}) const webSearch = tool( async (args) => { return ( "Here are the headcounts for each of the 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 the web for information.", schema: z.object({ query: z.string() }) } ); // Create supervisor workflow const workflow = createSupervisor({ agents: [mathAgent, webSearch], llm: model, prompt: "You are a team supervisor managing a research expert and a math expert. " + "For current events, use research_agent. " + "For math problems, use math_agent." }); const app = workflow.compile(); const result = await app.invoke({ messages: [ { role: "user", content: "what's 1 + 1 ? don't answer yourself always delegate to specialised agents" } ] }); console.log(result.messages[result.messages.length - 1].content); ``` Error: ```bash Waiting for the debugger to disconnect... file:///Users/mwamedacen/Desktop/projects/blockanalytica/node_modules/.pnpm/@langchain+langgraph@0.2.49_@langchain+core@0.3.40_openai@4.85.4_ws@8.18.0_zod@3.24.2__/node_modules/@langchain/langgraph/dist/graph/state.js:595 throw new ParentCommand(command); ^ ParentCommand at RunnableCallable._controlBranch [as func] (file:///Users/mwamedacen/Desktop/projects/blockanalytica/node_modules/.pnpm/@langchain+langgraph@0.2.49_@langchain+core@0.3.40_openai@4.85.4_ws@8.18.0_zod@3.24.2__/node_modules/@langchain/langgraph/dist/graph/state.js:595:19) at file:///Users/mwamedacen/Desktop/projects/blockanalytica/node_modules/.pnpm/@langchain+langgraph@0.2.49_@langchain+core@0.3.40_openai@4.85.4_ws@8.18.0_zod@3.24.2__/node_modules/@langchain/langgraph/dist/utils.js:79:113 at AsyncLocalStorage.run (node:internal/async_local_storage/async_hooks:91:14) at AsyncLocalStorageProvider.runWithConfig (file:///Users/mwamedacen/Desktop/projects/blockanalytica/node_modules/.pnpm/@langchain+core@0.3.40_openai@4.85.4_ws@8.18.0_zod@3.24.2_/node_modules/@langchain/core/dist/singletons/async_local_storage/index.js:53:24) at RunnableCallable.invoke (file:///Users/mwamedacen/Desktop/projects/blockanalytica/node_modules/.pnpm/@langchain+langgraph@0.2.49_@langchain+core@0.3.40_openai@4.85.4_ws@8.18.0_zod@3.24.2__/node_modules/@langchain/langgraph/dist/utils.js:79:68) at Branch._route (file:///Users/mwamedacen/Desktop/projects/blockanalytica/node_modules/.pnpm/@langchain+langgraph@0.2.49_@langchain+core@0.3.40_openai@4.85.4_ws@8.18.0_zod@3.24.2__/node_modules/@langchain/langgraph/dist/graph/graph.js:65:43) at RunnableCallable.func (file:///Users/mwamedacen/Desktop/projects/blockanalytica/node_modules/.pnpm/@langchain+langgraph@0.2.49_@langchain+core@0.3.40_openai@4.85.4_ws@8.18.0_zod@3.24.2__/node_modules/@langchain/langgraph/dist/graph/graph.js:48:39) at file:///Users/mwamedacen/Desktop/projects/blockanalytica/node_modules/.pnpm/@langchain+langgraph@0.2.49_@langchain+core@0.3.40_openai@4.85.4_ws@8.18.0_zod@3.24.2__/node_modules/@langchain/langgraph/dist/utils.js:79:113 at AsyncLocalStorage.run (node:internal/async_local_storage/async_hooks:91:14) at AsyncLocalStorageProvider.runWithConfig (file:///Users/mwamedacen/Desktop/projects/blockanalytica/node_modules/.pnpm/@langchain+core@0.3.40_openai@4.85.4_ws@8.18.0_zod@3.24.2_/node_modules/@langchain/core/dist/singletons/async_local_storage/index.js:53:24) { lc_error_code: undefined, command: Command { lg_name: 'Command', lc_direct_tool_output: true, graph: 'math_agent:1380b467-2203-5352-8d60-58c23285c129|supervisor:fef44ef8-3a56-5f86-85a1-c0f95b7cc477', update: [ [ 'messages', [ HumanMessage { lc_serializable: true, lc_kwargs: [Object], lc_namespace: [Array], content: "what's 1 + 1 ? don't answer yourself, delegate to specialised agents", name: undefined, additional_kwargs: {}, response_metadata: {}, id: '842602ac-8088-4213-8343-5e24be8e6353' }, AIMessage { lc_serializable: true, lc_kwargs: [Object], lc_namespace: [Array], content: '', name: 'supervisor', additional_kwargs: [Object], response_metadata: [Object], id: 'chatcmpl-B5H7EHupGq7MCtK6cFXbel20mdfMV', tool_calls: [Array], invalid_tool_calls: [], usage_metadata: [Object] }, ToolMessage { lc_serializable: true, lc_kwargs: [Object], lc_namespace: [Array], content: 'Successfully transferred to math_agent', name: 'transfer_to_math_agent', additional_kwargs: {}, response_metadata: {}, id: 'fd254135-2293-4f0b-92ec-c91be981198b', lc_direct_tool_output: true, status: undefined, tool_call_id: 'call_puCy3lRU4lmAVWnGUjQLP1cA', artifact: undefined } ] ], [ 'structuredResponse', null ], [ 'messages', [ HumanMessage { lc_serializable: true, lc_kwargs: [Object], lc_namespace: [Array], content: "what's 1 + 1 ? don't answer yourself, delegate to specialised agents", name: undefined, additional_kwargs: {}, response_metadata: {}, id: '842602ac-8088-4213-8343-5e24be8e6353' }, AIMessage { lc_serializable: true, lc_kwargs: [Object], lc_namespace: [Array], content: '', name: 'supervisor', additional_kwargs: [Object], response_metadata: [Object], id: 'chatcmpl-B5H7EHupGq7MCtK6cFXbel20mdfMV', tool_calls: [Array], invalid_tool_calls: [], usage_metadata: [Object] }, ToolMessage { lc_serializable: true, lc_kwargs: [Object], lc_namespace: [Array], content: 'Successfully transferred to math_agent', name: 'transfer_to_math_agent', additional_kwargs: {}, response_metadata: {}, id: 'fd254135-2293-4f0b-92ec-c91be981198b', lc_direct_tool_output: true, status: undefined, tool_call_id: 'call_puCy3lRU4lmAVWnGUjQLP1cA', artifact: undefined } ] ], [ 'structuredResponse', null ], [ 'messages', [ HumanMessage { lc_serializable: true, lc_kwargs: [Object], lc_namespace: [Array], content: "what's 1 + 1 ? don't answer yourself, delegate to specialised agents", name: undefined, additional_kwargs: {}, response_metadata: {}, id: '842602ac-8088-4213-8343-5e24be8e6353' }, AIMessage { lc_serializable: true, lc_kwargs: [Object], lc_namespace: [Array], content: '', name: 'supervisor', additional_kwargs: [Object], response_metadata: [Object], id: 'chatcmpl-B5H7EHupGq7MCtK6cFXbel20mdfMV', tool_calls: [Array], invalid_tool_calls: [], usage_metadata: [Object] }, ToolMessage { lc_serializable: true, lc_kwargs: [Object], lc_namespace: [Array], content: 'Successfully transferred to math_agent', name: 'transfer_to_math_agent', additional_kwargs: {}, response_metadata: {}, id: 'fd254135-2293-4f0b-92ec-c91be981198b', lc_direct_tool_output: true, status: undefined, tool_call_id: 'call_puCy3lRU4lmAVWnGUjQLP1cA', artifact: undefined }, AIMessage { lc_serializable: true, lc_kwargs: [Object], lc_namespace: [Array], content: '', name: 'supervisor', additional_kwargs: [Object], response_metadata: [Object], id: 'chatcmpl-B5H7FcMGK3hDAH4DdwdYD0uBsO5gb', tool_calls: [Array], invalid_tool_calls: [], usage_metadata: [Object] } ] ], [ 'structuredResponse', null ], [ 'messages', [ ToolMessage { lc_serializable: true, lc_kwargs: [Object], lc_namespace: [Array], content: 'Successfully transferred to math_add_expert', name: 'transfer_to_math_add_expert', additional_kwargs: {}, response_metadata: {}, id: undefined, lc_direct_tool_output: true, status: undefined, tool_call_id: 'call_ZvD8brjwFkA7tk72Yi4dHmqi', artifact: undefined } ] ] ], resume: undefined, goto: [ 'math_add_expert' ] }, pregelTaskId: '1380b467-2203-5352-8d60-58c23285c129' } Node.js v22.9.0 ``` --- Proof of concept 2 (extending multi supervisor example): ```typescript 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"; const model = new ChatOpenAI({ modelName: "gpt-4o", temperature: 0 }); // Create specialized agents const add = tool( async (args) => args.a + args.b, { name: "add", description: "Add two numbers.", schema: z.object({ a: z.number(), b: z.number() }), verbose: true } ); const multiply = tool( async (args) => args.a * args.b, { name: "multiply", description: "Multiply two numbers.", schema: z.object({ a: z.number(), b: z.number() }), verbose: true } ); const webSearch = tool( async (args) => { return ( "Here are the headcounts for each of the 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 the web for information.", schema: z.object({ query: z.string() }), verbose: true } ); const writingTool = tool( async (args) => { return `Drafted article about: ${args.topic}`; }, { name: "write_article", description: "Write an article on a given topic.", schema: z.object({ topic: z.string() }), verbose: true } ); const publishingTool = tool( async (args) => { return `Published article: ${args.article}`; }, { name: "publish_article", description: "Publish a written article.", schema: z.object({ article: z.string() }), verbose: true } ); const mathAgent = createReactAgent({ llm: model, tools: [add, multiply], name: "math_expert", prompt: "You are a math expert. Always use one tool at a time." }); const researchAgent = createReactAgent({ llm: model, tools: [webSearch], name: "research_expert", prompt: "You are a world class researcher with access to web search. Do not do any math." }); const writingAgent = createReactAgent({ llm: model, tools: [writingTool], name: "writing_expert", prompt: "You are an expert writer. Focus on creating engaging content." }); const publishingAgent = createReactAgent({ llm: model, tools: [publishingTool], name: "publishing_expert", prompt: "You are a publishing expert. Handle the publication process professionally." }); // Create team supervisors const researchTeam = createSupervisor({ agents: [researchAgent, mathAgent], llm: model, prompt: "You are a research team supervisor. Coordinate research and math tasks effectively." }).compile({ name: "research_team" }); const writingTeam = createSupervisor({ agents: [writingAgent, publishingAgent], llm: model, prompt: "You are a writing team supervisor. Coordinate content creation and publication." }).compile({ name: "writing_team" }); // Create top-level supervisor const topLevelSupervisor = createSupervisor({ agents: [researchTeam, writingTeam], llm: model, prompt: "You are an executive supervisor coordinating research and writing teams. Delegate tasks appropriately." }).compile({ name: "top_level_supervisor" }); // Run the workflow const result = await topLevelSupervisor.invoke({ messages: [ { role: "user", content: "what's 1+1 ? Don't guess the answer rely on research_team" } ] }); console.log(result.messages[result.messages.length - 1].content); ``` Error: ```bash ile:///Users/mwamedacen/Desktop/projects/blockanalytica/node_modules/.pnpm/@langchain+langgraph@0.2.49_@langchain+core@0.3.40_openai@4.85.4_ws@8.18.0_zod@3.24.2__/node_modules/@langchain/langgraph/dist/graph/state.js:595 throw new ParentCommand(command); ^ ParentCommand at RunnableCallable._controlBranch [as func] (file:///Users/mwamedacen/Desktop/projects/blockanalytica/node_modules/.pnpm/@langchain+langgraph@0.2.49_@langchain+core@0.3.40_openai@4.85.4_ws@8.18.0_zod@3.24.2__/node_modules/@langchain/langgraph/dist/graph/state.js:595:19) at file:///Users/mwamedacen/Desktop/projects/blockanalytica/node_modules/.pnpm/@langchain+langgraph@0.2.49_@langchain+core@0.3.40_openai@4.85.4_ws@8.18.0_zod@3.24.2__/node_modules/@langchain/langgraph/dist/utils.js:79:113 at AsyncLocalStorage.run (node:internal/async_local_storage/async_hooks:91:14) at AsyncLocalStorageProvider.runWithConfig (file:///Users/mwamedacen/Desktop/projects/blockanalytica/node_modules/.pnpm/@langchain+core@0.3.40_openai@4.85.4_ws@8.18.0_zod@3.24.2_/node_modules/@langchain/core/dist/singletons/async_local_storage/index.js:53:24) at RunnableCallable.invoke (file:///Users/mwamedacen/Desktop/projects/blockanalytica/node_modules/.pnpm/@langchain+langgraph@0.2.49_@langchain+core@0.3.40_openai@4.85.4_ws@8.18.0_zod@3.24.2__/node_modules/@langchain/langgraph/dist/utils.js:79:68) at Branch._route (file:///Users/mwamedacen/Desktop/projects/blockanalytica/node_modules/.pnpm/@langchain+langgraph@0.2.49_@langchain+core@0.3.40_openai@4.85.4_ws@8.18.0_zod@3.24.2__/node_modules/@langchain/langgraph/dist/graph/graph.js:65:43) at RunnableCallable.func (file:///Users/mwamedacen/Desktop/projects/blockanalytica/node_modules/.pnpm/@langchain+langgraph@0.2.49_@langchain+core@0.3.40_openai@4.85.4_ws@8.18.0_zod@3.24.2__/node_modules/@langchain/langgraph/dist/graph/graph.js:48:39) at file:///Users/mwamedacen/Desktop/projects/blockanalytica/node_modules/.pnpm/@langchain+langgraph@0.2.49_@langchain+core@0.3.40_openai@4.85.4_ws@8.18.0_zod@3.24.2__/node_modules/@langchain/langgraph/dist/utils.js:79:113 at AsyncLocalStorage.run (node:internal/async_local_storage/async_hooks:91:14) at AsyncLocalStorageProvider.runWithConfig (file:///Users/mwamedacen/Desktop/projects/blockanalytica/node_modules/.pnpm/@langchain+core@0.3.40_openai@4.85.4_ws@8.18.0_zod@3.24.2_/node_modules/@langchain/core/dist/singletons/async_local_storage/index.js:53:24) { lc_error_code: undefined, command: Command { lg_name: 'Command', lc_direct_tool_output: true, graph: 'research_team:52740bcf-fc8b-5987-ac8c-3465b699ea33|supervisor:f3105a4a-526a-5673-a6d0-e3ea3b8f1789', update: [ [ 'messages', [ HumanMessage { lc_serializable: true, lc_kwargs: [Object], lc_namespace: [Array], content: "what's 1+1 ? Don't guess the answer rely on research_team", name: undefined, additional_kwargs: {}, response_metadata: {}, id: '085adb01-f101-4898-ac01-2c4b2636146a' }, AIMessage { lc_serializable: true, lc_kwargs: [Object], lc_namespace: [Array], content: '', name: 'supervisor', additional_kwargs: [Object], response_metadata: [Object], id: 'chatcmpl-B5HQVFWX9ZJtKJEQ06IybWIl2RW6V', tool_calls: [Array], invalid_tool_calls: [], usage_metadata: [Object] }, ToolMessage { lc_serializable: true, lc_kwargs: [Object], lc_namespace: [Array], content: 'Successfully transferred to research_team', name: 'transfer_to_research_team', additional_kwargs: {}, response_metadata: {}, id: '25e86432-0bbd-4c21-97ca-d4dee518eb1d', lc_direct_tool_output: true, status: undefined, tool_call_id: 'call_x9pJIb7Xt3zocInFXRcrr0gc', artifact: undefined } ] ], [ 'structuredResponse', null ], [ 'messages', [ HumanMessage { lc_serializable: true, lc_kwargs: [Object], lc_namespace: [Array], content: "what's 1+1 ? Don't guess the answer rely on research_team", name: undefined, additional_kwargs: {}, response_metadata: {}, id: '085adb01-f101-4898-ac01-2c4b2636146a' }, AIMessage { lc_serializable: true, lc_kwargs: [Object], lc_namespace: [Array], content: '', name: 'supervisor', additional_kwargs: [Object], response_metadata: [Object], id: 'chatcmpl-B5HQVFWX9ZJtKJEQ06IybWIl2RW6V', tool_calls: [Array], invalid_tool_calls: [], usage_metadata: [Object] }, ToolMessage { lc_serializable: true, lc_kwargs: [Object], lc_namespace: [Array], content: 'Successfully transferred to research_team', name: 'transfer_to_research_team', additional_kwargs: {}, response_metadata: {}, id: '25e86432-0bbd-4c21-97ca-d4dee518eb1d', lc_direct_tool_output: true, status: undefined, tool_call_id: 'call_x9pJIb7Xt3zocInFXRcrr0gc', artifact: undefined } ] ], [ 'structuredResponse', null ], [ 'messages', [ HumanMessage { lc_serializable: true, lc_kwargs: [Object], lc_namespace: [Array], content: "what's 1+1 ? Don't guess the answer rely on research_team", name: undefined, additional_kwargs: {}, response_metadata: {}, id: '085adb01-f101-4898-ac01-2c4b2636146a' }, AIMessage { lc_serializable: true, lc_kwargs: [Object], lc_namespace: [Array], content: '', name: 'supervisor', additional_kwargs: [Object], response_metadata: [Object], id: 'chatcmpl-B5HQVFWX9ZJtKJEQ06IybWIl2RW6V', tool_calls: [Array], invalid_tool_calls: [], usage_metadata: [Object] }, ToolMessage { lc_serializable: true, lc_kwargs: [Object], lc_namespace: [Array], content: 'Successfully transferred to research_team', name: 'transfer_to_research_team', additional_kwargs: {}, response_metadata: {}, id: '25e86432-0bbd-4c21-97ca-d4dee518eb1d', lc_direct_tool_output: true, status: undefined, tool_call_id: 'call_x9pJIb7Xt3zocInFXRcrr0gc', artifact: undefined }, AIMessage { lc_serializable: true, lc_kwargs: [Object], lc_namespace: [Array], content: '', name: 'supervisor', additional_kwargs: [Object], response_metadata: [Object], id: 'chatcmpl-B5HQW4u28Gkc9j1vHnp4R2Vjdbkk2', tool_calls: [Array], invalid_tool_calls: [], usage_metadata: [Object] } ] ], [ 'structuredResponse', null ], [ 'messages', [ ToolMessage { lc_serializable: true, lc_kwargs: [Object], lc_namespace: [Array], content: 'Successfully transferred to math_expert', name: 'transfer_to_math_expert', additional_kwargs: {}, response_metadata: {}, id: undefined, lc_direct_tool_output: true, status: undefined, tool_call_id: 'call_RWt6E0DzJWUhQcZfzfUBuSRN', artifact: undefined } ] ] ], resume: undefined, goto: [ 'math_expert' ] }, pregelTaskId: '52740bcf-fc8b-5987-ac8c-3465b699ea33' } Node.js v22.9.0 ```
yindo closed this issue 2026-02-15 17:16:51 -05:00
Author
Owner

@sallespro commented on GitHub (Feb 26, 2025):

so i am unsing node v20.18.0 and this

        "@langchain/core": "^0.3.0",
        "@langchain/langgraph": "^0.2.0",
        "@langchain/langgraph-supervisor": "^0.0.2",

without problems.

@sallespro commented on GitHub (Feb 26, 2025): so i am unsing node v20.18.0 and this ```json "@langchain/core": "^0.3.0", "@langchain/langgraph": "^0.2.0", "@langchain/langgraph-supervisor": "^0.0.2", ``` without problems.
Author
Owner

@benjamincburns commented on GitHub (Feb 27, 2025):

We're moving this code into the main langgraphjs monorepo. I'll transfer this issue there now as of langchain-ai/langgraphjs#907. I'll transfer this issue there now so it doesn't get lost in the shuffle.

@benjamincburns commented on GitHub (Feb 27, 2025): We're moving this code into the main langgraphjs monorepo. I'll transfer this issue there now as of langchain-ai/langgraphjs#907. I'll transfer this issue there now so it doesn't get lost in the shuffle.
Author
Owner

@benjamincburns commented on GitHub (Feb 28, 2025):

@mwamedacen what version of langgraphjs are you using?

@benjamincburns commented on GitHub (Feb 28, 2025): @mwamedacen what version of langgraphjs are you using?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraphjs#178