Cannot use subclasses, functions or conditionals to define the workflow #216

Closed
opened 2026-02-15 17:17:26 -05:00 by yindo · 1 comment
Owner

Originally created by @jigarzon on GitHub (Mar 23, 2025).

Because of the strong typing of the addNode function, its very hard to split the graph creation into different functions, or subclasses.

For example, this code doesn't compile

      let workflowGen = workflow.addNode('generator', (state) => this.generateSolution(state))
        .addNode('reviewer', (state) => this.reviewSolution(state));
      if (this.config.generateLoadingMessages) {
        workflowGen.addNode('loadingMessage', (state) => this.generateLoadingMessage(state))
      }
      workflowGen.addEdge('__start__', 'loadingMessage')

by saying Argument of type '"loadingMessage"' is not assignable to parameter of type '"__start__" | "generator" | "reviewer" | "__end__"'.ts(2345)

This is because the addNode function creates a complex type that stores all the node names. It seems to be a feature to avoid typos, but considering that the graph is validated in the compile() function, and also that the benefits are less than the cons (for example, you cannot add conditionals for the creation of the graph, or you cannot split the creation of the graph in multiple function / subclasses), I would say that this is a design bug. Or maybe I'm missing an alternative approach

Originally created by @jigarzon on GitHub (Mar 23, 2025). Because of the strong typing of the `addNode` function, its very hard to split the graph creation into different functions, or subclasses. For example, this code doesn't compile ``` let workflowGen = workflow.addNode('generator', (state) => this.generateSolution(state)) .addNode('reviewer', (state) => this.reviewSolution(state)); if (this.config.generateLoadingMessages) { workflowGen.addNode('loadingMessage', (state) => this.generateLoadingMessage(state)) } workflowGen.addEdge('__start__', 'loadingMessage') ``` by saying `Argument of type '"loadingMessage"' is not assignable to parameter of type '"__start__" | "generator" | "reviewer" | "__end__"'.ts(2345)` This is because the `addNode` function creates a complex type that stores all the node names. It seems to be a feature to avoid typos, but considering that the graph is validated in the `compile()` function, and also that the benefits are less than the cons (for example, you cannot add conditionals for the creation of the graph, or you cannot split the creation of the graph in multiple function / subclasses), I would say that this is a design bug. Or maybe I'm missing an alternative approach
yindo closed this issue 2026-02-15 17:17:26 -05:00
Author
Owner

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

I'm afraid this duplicates #763. See this comment for a workaround. It's a bit ugly, but another workaround that doesn't involve passing explicit type parameters is to cast the first argument of addNode, addEdge, and addConditionalEdge to any.

@benjamincburns commented on GitHub (Mar 25, 2025): I'm afraid this duplicates #763. See [this comment](https://github.com/langchain-ai/langgraphjs/issues/763#issuecomment-2680321351) for a workaround. It's a bit ugly, but another workaround that doesn't involve passing explicit type parameters is to cast the first argument of `addNode`, `addEdge`, and `addConditionalEdge` to `any`.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraphjs#216