How to beginner · 3 min read

How to add nodes to LangGraph

Quick answer
To add nodes to LangGraph in LangChain, instantiate a LangGraph object and use its add_node() method with node data or objects. Nodes represent documents or data points and can be added individually or in batches to build your graph structure.

PREREQUISITES

  • Python 3.8+
  • pip install langchain>=0.2
  • OpenAI API key (free tier works)
  • pip install openai>=1.0

Setup

Install the langchain package and set your OpenAI API key in the environment variables.

  • Install LangChain and OpenAI SDK:
bash
pip install langchain openai

Step by step

Here is a complete example showing how to create a LangGraph instance and add nodes representing text documents. Each node can be a dictionary or a LangChain document object.

python
import os
from langchain_community.langgraph import LangGraph

# Initialize LangGraph
graph = LangGraph()

# Add a single node with text data
node1 = {"id": "node1", "text": "This is the first node."}
graph.add_node(node1)

# Add multiple nodes
nodes = [
    {"id": "node2", "text": "Second node content."},
    {"id": "node3", "text": "Third node content."}
]
for node in nodes:
    graph.add_node(node)

# Print all nodes in the graph
print("Nodes in LangGraph:")
for node_id, node_data in graph.nodes.items():
    print(f"{node_id}: {node_data}")
output
Nodes in LangGraph:
node1: {'id': 'node1', 'text': 'This is the first node.'}
node2: {'id': 'node2', 'text': 'Second node content.'}
node3: {'id': 'node3', 'text': 'Third node content.'}

Common variations

You can add nodes asynchronously if your environment supports async, or use different node formats such as LangChain Document objects. Also, you can integrate LangGraph with vector stores for semantic search.

python
import asyncio
from langchain_community.langgraph import LangGraph
from langchain.schema import Document

async def add_nodes_async():
    graph = LangGraph()
    doc_node = Document(page_content="Async node content.", metadata={"id": "async_node"})
    await graph.add_node(doc_node)  # If add_node supports async
    print(graph.nodes)

# Run async example
# asyncio.run(add_nodes_async())  # Uncomment if add_node is async

Troubleshooting

  • If add_node() raises an error, verify the node format matches expected schema (usually a dict or Document).
  • Ensure LangGraph is properly imported from langchain_community.langgraph.
  • Check your environment variables for the OpenAI API key if you integrate with LLMs.

Key Takeaways

  • Use add_node() method to add nodes to a LangGraph instance.
  • Nodes can be dictionaries or LangChain Document objects representing data points.
  • You can add nodes individually or in batches by looping over your data.
  • Async addition of nodes is possible if supported by your environment and LangGraph version.
  • Verify node formats and environment setup to avoid common errors.
Verified 2026-04
Verify ↗