DS DevShelfHub Projects · AI tools
Tutorials / LangChain / Reference / Methods / .add_edge()
Method StateGraph

.add_edge(): Reference Guide

By DevShelfHub

Add direct edge between nodes.

What is .add_edge()?

.add_edge() is the simplest graph construction method in LangGraph — it draws an unconditional arc from one node to another. After from_node finishes executing and returns its state update, LangGraph always transitions to to_node, with no condition evaluation. This is the method to reach for when the execution flow between two steps is fixed and deterministic.

Edges defined with add_edge() must reference nodes that have already been added to the graph via add_node(). Adding an edge to a non-existent node raises a ValueError at graph compilation time (when you call .compile()), not at definition time — a common source of confusion. The graph is not validated until compile() is called, so you can define edges in any order as long as all referenced nodes are registered before compilation.

The START and END sentinels (from langgraph.graph) are special node names representing the entry point and terminal state. graph.add_edge(START, 'first_node') sets the entry node, equivalent to calling graph.set_entry_point('first_node'). Graphs with no path from any node to END will run indefinitely if they contain cycles — always verify at least one execution path terminates at END.

Use Cases

  • Define workflows
  • Node sequences
  • Sequential execution
  • Graph routing
  • Workflow paths
  • Execution flow

Key Features

  • Direct connections
  • Sequential flow
  • Simple routing
  • Graph structure
  • Path definition
  • Composable

When NOT to Use

For conditional routing—use add_conditional_edges().

Notes

Edges are validated at compile time, not definition time

Calling add_edge('a', 'b') before add_node('a', ...) will not immediately error. The ValueError fires only when you call graph.compile(). Add compile() calls in your tests to catch missing-node bugs before they reach production.

Use START instead of set_entry_point()

graph.add_edge(START, 'my_node') is the canonical way to set the entry point in LangGraph 0.2+. The older set_entry_point() method still works but is considered legacy and may be removed in a future version.

Fan-out with multiple edges from one source

Adding multiple edges from the same source node causes LangGraph to execute all target nodes concurrently: add_edge('a', 'b') + add_edge('a', 'c') runs b and c in parallel when execution reaches a. All parallel branches must converge at a subsequent node before the graph can continue.

No path to END means infinite loop

Graphs containing cycles (e.g., a -> b -> a) with no conditional exit to END will run indefinitely or until a recursion limit error. Always verify at least one execution path terminates at END before compiling.

Method Signature

python
graph.add_edge(from_node, to_node)

Parameters

Parameter Type Required Purpose
from_node str Yes Source node name

Return Value

Type:

StateGraph

Description:

Graph with edge added

Example Output:

graph

Code Examples

Basic sequential edge

python
graph.add_node('step1', func1)
graph.add_node('step2', func2)
graph.add_edge('step1', 'step2')  # step1 -> step2

Three-node sequential pipeline with START and END

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

class State(TypedDict):
    value: str

def step_a(state): return {"value": state["value"] + " A"}
def step_b(state): return {"value": state["value"] + " B"}
def step_c(state): return {"value": state["value"] + " C"}

graph = StateGraph(State)
graph.add_node('a', step_a)
graph.add_node('b', step_b)
graph.add_node('c', step_c)
graph.add_edge(START, 'a')
graph.add_edge('a', 'b')
graph.add_edge('b', 'c')
graph.add_edge('c', END)
compiled = graph.compile()
result = compiled.invoke({"value": "start"})
print(result["value"])  # start A B C

Fan-out to parallel nodes then merge

python
from langgraph.graph import StateGraph, START, END

# Fan-out: single node triggers two parallel nodes
graph.add_node('preprocessor', preprocess)
graph.add_node('llm_call', call_llm)
graph.add_node('db_lookup', query_db)
graph.add_node('aggregator', aggregate)
graph.add_edge(START, 'preprocessor')
graph.add_edge('preprocessor', 'llm_call')
graph.add_edge('preprocessor', 'db_lookup')
graph.add_edge('llm_call', 'aggregator')
graph.add_edge('db_lookup', 'aggregator')
graph.add_edge('aggregator', END)

Common Mistakes

❌ Add edge before adding nodes

✅ Add nodes first, then edges

Browse the full LangChain API reference index to explore more classes, methods, and decorators, or start with the LangChain introduction tutorial for end-to-end context on building with .add_edge() and the wider framework.

.add_edge() FAQ

What does .add_edge() do in LangChain?

Add direct edge between nodes. .add_edge() is the simplest graph construction method in LangGraph — it draws an unconditional arc from one node to another. After from_node finishes executing and returns its state update, LangGraph always transitions to to_node, with no condition evaluation. This is the method to reach for when the execution flow between two steps is fixed and deterministic. Edges defined with add_edge() must reference nodes that have already been added to the graph via add_node(). Adding …

Which LangChain classes support .add_edge()?

.add_edge() is available on StateGraph. Pin your installed LangChain version and verify the method exists in that release before deploying.

When should I use .add_edge()?

Use .add_edge() when your LangChain chains, agents, or pipelines need the behavior described in this guide.

What does .add_edge() return?

.add_edge() returns a StateGraph. Graph with edge added

Does .add_edge() have an async equivalent?

.add_edge() does not have a documented async variant. Avoid .add_edge() For conditional routing—use add_conditional_edges().

Where can I explore more LangChain API reference pages?

Open the LangChain API reference index on DevShelfHub to browse classes, methods, and decorators, each with runnable examples, parameters, common mistakes, and cross-links.