How to beginner · 3 min read

How to add nodes to LangGraph

Quick answer
To add nodes to LangGraph, use the add_node() method on a StateGraph instance, passing the node name and a function that takes and returns the graph state. Then set the entry point with set_entry_point() and define edges with add_edge(). Finally, compile and invoke the graph.

PREREQUISITES

  • Python 3.8+
  • pip install langgraph
  • Basic Python function knowledge

Setup

Install the langgraph package via pip and import the necessary classes. Define a TypedDict for your graph state to ensure type safety.

bash
pip install langgraph
output
Collecting langgraph
  Downloading langgraph-0.1.0-py3-none-any.whl (10 kB)
Installing collected packages: langgraph
Successfully installed langgraph-0.1.0

Step by step

Create a StateGraph with a typed state, add nodes with add_node() by defining functions that update the state, set the entry point, add edges, compile the graph, and invoke it with an initial state.

python
from langgraph.graph import StateGraph, END
from typing import TypedDict

# Define the state type
class State(TypedDict):
    messages: list[str]

# Define node functions

def start_node(state: State) -> State:
    state["messages"].append("Hello from start_node")
    return state

def second_node(state: State) -> State:
    state["messages"].append("Hello from second_node")
    return state

# Create the graph
graph = StateGraph(State)

# Add nodes
graph.add_node("start", start_node)
graph.add_node("second", second_node)

# Set entry point
graph.set_entry_point("start")

# Add edges
graph.add_edge("start", "second")
graph.add_edge("second", END)

# Compile the graph
app = graph.compile()

# Invoke the graph
result = app.invoke({"messages": []})
print(result["messages"])
output
['Hello from start_node', 'Hello from second_node']

Common variations

  • Use asynchronous node functions by defining async def and invoking with await app.invoke_async().
  • Manage more complex state by extending the TypedDict with additional fields.
  • Use conditional edges by adding logic inside node functions to decide next nodes dynamically.
python
import asyncio

async def async_start_node(state: State) -> State:
    state["messages"].append("Hello from async_start_node")
    return state

# Replace start node with async version
graph.add_node("start", async_start_node)

# Compile and invoke asynchronously
app = graph.compile()

async def main():
    result = await app.invoke_async({"messages": []})
    print(result["messages"])

asyncio.run(main())
output
['Hello from async_start_node', 'Hello from second_node']

Troubleshooting

  • If you see KeyError on state keys, ensure your initial state dictionary matches the TypedDict keys.
  • If nodes do not execute, verify that edges are correctly added and the entry point is set.
  • For type errors, confirm your node functions accept and return the correct state type.

Key Takeaways

  • Use add_node() with a function that takes and returns the graph state to add nodes.
  • Set the entry point with set_entry_point() before compiling the graph.
  • Define edges between nodes with add_edge() to control flow.
  • Compile the graph with compile() and invoke it with an initial state dictionary.
  • Async node functions require invoke_async() and async def syntax.
Verified 2026-04
Verify ↗