Programmatically manage dynamic Nodes and Edges? #133

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

Originally created by @warlockdn on GitHub (Nov 18, 2024).

Hello,

I am just trying out a basic logic where I want to dynamically manage adding or skipping of adding a node or edge and so on.


import fs from "node:fs";
import { StateGraph } from "@langchain/langgraph";
import { Annotation, START, END } from "@langchain/langgraph";
const stateAnnotation = Annotation.Root({
  messsages: ({
    reducer: (x, y) => x.concat(y)
  })
})

async function fn(state) {
  return;
}

try {

  const graph = new StateGraph(stateAnnotation)
    .addNode("agent", fn)

  graph.addNode("researcher", fn)

  graph.addEdge("agent", "researcher")

  graph.addEdge(START, "agent")

  graph.addEdge("researcher", END)

  const compiledGraph = graph.compile();

  const representation = compiledGraph.getGraph();
  const image = await representation.drawMermaidPng();
  const arrayBuffer = await image.arrayBuffer();

  // save the image to a file
  fs.writeFileSync("graph.png", Buffer.from(arrayBuffer));

} catch (error) {
  console.log(error)
}

Ofcourse, the logic would be dynamic in nature. But here I just wanted to see the possibility of doing it. I keep running into errors

26:17 - Argument of type '"agent"' is not assignable to parameter of type '"__start__" | "__start__"[]'.
30:24 - Argument of type '"agent"' is not assignable to parameter of type '"__start__" | "__end__"'.
34:17 - Argument of type '"researcher"' is not assignable to parameter of type '"__start__" | "__start__"[]'.

I see the same functionality is available in Langgraph Python version.

image

What am I missing here?

Originally created by @warlockdn on GitHub (Nov 18, 2024). Hello, I am just trying out a basic logic where I want to dynamically manage adding or skipping of adding a node or edge and so on. ``` import fs from "node:fs"; import { StateGraph } from "@langchain/langgraph"; import { Annotation, START, END } from "@langchain/langgraph"; const stateAnnotation = Annotation.Root({ messsages: ({ reducer: (x, y) => x.concat(y) }) }) async function fn(state) { return; } try { const graph = new StateGraph(stateAnnotation) .addNode("agent", fn) graph.addNode("researcher", fn) graph.addEdge("agent", "researcher") graph.addEdge(START, "agent") graph.addEdge("researcher", END) const compiledGraph = graph.compile(); const representation = compiledGraph.getGraph(); const image = await representation.drawMermaidPng(); const arrayBuffer = await image.arrayBuffer(); // save the image to a file fs.writeFileSync("graph.png", Buffer.from(arrayBuffer)); } catch (error) { console.log(error) } ``` Ofcourse, the logic would be dynamic in nature. But here I just wanted to see the possibility of doing it. I keep running into errors ``` 26:17 - Argument of type '"agent"' is not assignable to parameter of type '"__start__" | "__start__"[]'. 30:24 - Argument of type '"agent"' is not assignable to parameter of type '"__start__" | "__end__"'. 34:17 - Argument of type '"researcher"' is not assignable to parameter of type '"__start__" | "__start__"[]'. ``` I see the same functionality is available in Langgraph Python version. ![image](https://github.com/user-attachments/assets/c5cc13ad-ff53-483b-8133-6d4795df3e1c) What am I missing here?
yindo closed this issue 2026-02-15 17:16:06 -05:00
Author
Owner

@warlockdn commented on GitHub (Nov 19, 2024):

Oh well, it works but just not in a notebook. This is weird. why would this be?

@warlockdn commented on GitHub (Nov 19, 2024): Oh well, it works but just not in a notebook. This is weird. why would this be?
Author
Owner

@jacoblee93 commented on GitHub (Nov 22, 2024):

For TypeScript, we update the types as the graph is built to provide guidance on what you can node/edge names you can use. So the final syntax is a bit different from Python in that you need to chain it in order to avoid TypeErrors:

  const graph = new StateGraph(stateAnnotation)
    .addNode("agent", fn)
    .addNode("researcher", fn)
    .addEdge("agent", "researcher")
    .addEdge(START, "agent")
    .addEdge("researcher", END)

  const compiledGraph = graph.compile();

It will still work if you don't chain it, it just messes up the types.

@jacoblee93 commented on GitHub (Nov 22, 2024): For TypeScript, we update the types as the graph is built to provide guidance on what you can node/edge names you can use. So the final syntax is a bit different from Python in that you need to chain it in order to avoid TypeErrors: ```ts const graph = new StateGraph(stateAnnotation) .addNode("agent", fn) .addNode("researcher", fn) .addEdge("agent", "researcher") .addEdge(START, "agent") .addEdge("researcher", END) const compiledGraph = graph.compile(); ``` It will still work if you don't chain it, it just messes up the types.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraphjs#133