Skip to content

use command in tool cause ParentCommand error #1885

@miniboxhaha

Description

@miniboxhaha

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangGraph.js documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangGraph.js rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangGraph (or the specific integration package).

Example Code

//super graph
const checkpointer = new MemorySaver();
const superGraph = new StateGraph(root_graph_state)
    .addNode("supervisor", supervisor_graph, {
        ends: [
            "sub_A", "sub_B", "sub_C", "sub_D", END
        ]
    })
    .addNode("sub_A", sub_A)
    .addEdge("sub_A", "supervisor")
    //
    .addNode("sub_B", sub_B)
    .addEdge("sub_B", "supervisor")
    .addEdge(START, "supervisor");

const compiledSuperGraph = superGraph.compile({ checkpointer });

// sub graph A

const shape_management_graph = new StateGraph(graph_state)
    .addNode("router", router, { ends: ["check"] })
    .addEdge(START, "router")
    //
    .addNode("extra_msg", extra_msg_when_needed, { ends: ["check"] })
    //
    .addNode("check", create_ckeck, { ends: ["extra_msg", "wirte_code"] })
    //
    .addNode("wirte_code", wirte_code, { ends: ["output"] })
    //
    .addNode("output", output)
    .compile({ checkpointer: true })

//wirte_code is a react graph 
const wirte_code_to_create_or_edit_shape = async (state, config) => {
    const code_runner = tool(
        async ({ code }, config) => {
           
                    return new Command({
                        update: {
                            _self_messages: new ToolMessage({
                                content: r.result,
                                tool_call_id: config.toolCall?.id!
                            }),
                            messages: new ToolMessage({
                                content: r.result,
                                tool_call_id: config.toolCall?.id!
                            }),
                            _work_result: {
                                "foo": "foo",
                                "bar": “bar”
                            }
                        },
                        goto: "output",
                        graph: Command.PARENT,
                    });
                
        },
        {
            name: "code_runner",
            description: "运行大模型生成的代码",
            schema: z.object({
                code: z.string().describe("要被执行的代码"),
            }),
        }
    )
    try {
            const agent = createAgent({
                model: llm,
                tools: [code_runner],
                systemPrompt: readFileSync("electron/main/agent/nodes/shape_manage/propmt.md", 'utf8')
            });
    
            const result = await agent.invoke(
                {
                    messages: state.current_work.content
                },
                {
                    recursionLimit: 10
                }
            );
        } catch (error) {
            //when use try catch throw ParentCommand error
            //when no try cathc , graph route to output as expect
            console.log("..................");
            console.log(error);
        }
}

Error Message and Stack Trace (if applicable)

ParentCommand
at RunnableCallable._controlBranch [as func] (file:///home/an/my_projects/electron+vue+ai/electron-vite-vue/node_modules/@langchain/langgraph/dist/graph/state.js:479:47)
at file:///home/an/my_projects/electron+vue+ai/electron-vite-vue/node_modules/@langchain/langgraph/dist/utils.js:39:108
at AsyncLocalStorage.run (node:internal/async_local_storage/async_hooks:91:14)
at AsyncLocalStorageProvider.runWithConfig (file:///home/an/my_projects/electron+vue+ai/electron-vite-vue/node_modules/@langchain/core/dist/singletons/async_local_storage/index.js:47:18)
at RunnableCallable.invoke (file:///home/an/my_projects/electron+vue+ai/electron-vite-vue/node_modules/@langchain/langgraph/dist/utils.js:39:63)
at Branch._route (file:///home/an/my_projects/electron+vue+ai/electron-vite-vue/node_modules/@langchain/langgraph/dist/graph/graph.js:41:32)
at RunnableCallable.func (file:///home/an/my_projects/electron+vue+ai/electron-vite-vue/node_modules/@langchain/langgraph/dist/graph/graph.js:32:24)
at file:///home/an/my_projects/electron+vue+ai/electron-vite-vue/node_modules/@langchain/langgraph/dist/utils.js:39:108
at AsyncLocalStorage.run (node:internal/async_local_storage/async_hooks:91:14)
at AsyncLocalStorageProvider.runWithConfig (file:///home/an/my_projects/electron+vue+ai/electron-vite-vue/node_modules/@langchain/core/dist/singletons/async_local_storage/index.js:47:18) {
lc_error_code: undefined,
command: Command {
lg_name: 'Command',
lc_direct_tool_output: true,
graph: '形状管理|代码沙盒:8ec9c5cc-1d51-5859-8940-da8ba150461d',
update: {
_self_messages: ToolMessage {
"content": "asdasdasddasd",
"additional_kwargs": {},
"response_metadata": {},
"tool_call_id": "call_5c7c3eb368ac44d1bd6a3111"
},
messages: ToolMessage {
"content": "asdasdsadad",
"additional_kwargs": {},
"response_metadata": {},
"tool_call_id": "call_5c7c3eb368ac44d1bd6a3111"
},
_work_result: [Object]
},
resume: undefined,
goto: [ '输出' ],
[Symbol(langgraph.command)]: {
lg_name: 'Command',
lc_direct_tool_output: true,
graph: '形状管理|代码沙盒:8ec9c5cc-1d51-5859-8940-da8ba150461d',
update: [Object],
resume: undefined,
goto: [Array],
[Symbol(langgraph.command)]: [Object]
}
},
pregelTaskId: '0493c810-d0f1-5147-bb6e-ebbded75b88b'
}

Description

as code part says
when use try catch around createAgent throw ParentCommand error
//when no try catch around createAgent , graph route to output as expect**

System Info

electron-vue-vite@28.1.0 /home/an/my_projects/electron+vue+ai/electron-vite-vue
├── @langchain/anthropic@1.1.3
├── @langchain/core@1.1.8
├── @langchain/langgraph@1.0.7
├── @langchain/openai@1.1.3
├── @langfuse/langchain@4.4.2
├── @langfuse/otel@4.4.2
├── @langfuse/tracing@4.4.2
├── @opentelemetry/sdk-node@0.208.0
├── @turf/turf@7.3.1
├── @types/cesium@1.67.14
├── @types/node@24.5.2
├── @vitejs/plugin-vue@5.2.4
├── @zilliz/milvus2-sdk-node@2.5.10
├── axios@1.10.0
├── b-spline@2.0.2
├── cesium@1.136.0
├── dotenv@17.2.3
├── electron-builder@24.13.3
├── electron@37.4.0
├── element-plus@2.10.2
├── geotiff@2.1.4-beta.0
├── h3-js@4.4.0
├── ioredis@5.8.0
├── langchain@1.2.3
├── md5@2.3.0
├── mermaid@11.7.0
├── ol@10.6.1
├── tslab@1.0.22
├── typescript@5.9.2
├── vite-plugin-electron-renderer@0.14.6
├── vite-plugin-electron@0.28.8
├── vite@5.4.19
├── vue-element-plus-x@1.3.7
├── vue-tsc@2.2.12
├── vue@3.5.20
├── yaml@2.8.2
└── zod@4.1.13

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions