Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Principle:Langchain ai Langgraph Edge Configuration

From Leeroopedia
Metadata Value
Type Principle
Library langgraph
Source libs/langgraph/langgraph/graph/state.py
Workflow Building_a_Stateful_Graph

Overview

Edge configuration defines the connections between nodes in a LangGraph StateGraph. Edges determine the order in which nodes execute and can be either static (unconditional) or conditional (determined at runtime by a routing function). Two special sentinel constants -- START and END -- mark the entry and exit points of the graph.

Description

Directed Edges

A directed edge (A, B) means: "after node A finishes, execute node B." Edges are the arrows in the directed graph that define execution order. Without edges, nodes have no way to trigger one another.

Static Edges

A static edge is an unconditional connection added with add_edge(start_key, end_key). When the start node completes, execution proceeds to the end node. Static edges are sufficient for linear pipelines and fixed workflows.

Fan-in (join) edges: When start_key is a list of node names, the edge becomes a join -- the end node only executes after all of the listed start nodes have completed. This enables synchronization barriers in parallel execution paths.

Conditional Edges

A conditional edge is added with add_conditional_edges(source, path, path_map). Instead of a fixed destination, a routing function (path) is called with the current state after the source node completes. The function returns one or more node names (or END) that should execute next.

The optional path_map parameter maps the return values of the routing function to actual node names, providing a layer of indirection. If omitted, the routing function must return valid node names directly.

Conditional edges enable:

  • Branching -- Different nodes execute depending on state contents.
  • Looping -- A node can route back to itself or to an earlier node, creating cycles.
  • Early termination -- Returning END stops the graph.
  • Fan-out -- Returning multiple node names triggers parallel execution.

START and END Constants

  • START ("__start__") -- The virtual entry node. Adding an edge from START to a node marks that node as the first to execute when the graph is invoked.
  • END ("__end__") -- The virtual exit node. Adding an edge from a node to END marks that node as a terminal point. When execution reaches END, the graph returns its output.

Convenience Methods

  • set_entry_point(key) -- Equivalent to add_edge(START, key).
  • set_finish_point(key) -- Equivalent to add_edge(key, END).
  • set_conditional_entry_point(path, path_map) -- Equivalent to add_conditional_edges(START, path, path_map).

Visualization and Type Hints

For conditional edges, LangGraph inspects the routing function's return type annotation. If the return type is a Literal (e.g., -> Literal["node_a", "__end__"]), the graph visualization can accurately show which nodes are reachable. Without type hints or a path_map, the visualization assumes the edge could go to any node.

Usage

from typing import Literal
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, START, END

class State(TypedDict):
    x: int

def node_a(state: State) -> dict:
    return {"x": state["x"] + 1}

def node_b(state: State) -> dict:
    return {"x": state["x"] * 2}

def router(state: State) -> Literal["node_b", "__end__"]:
    if state["x"] > 10:
        return END
    return "node_b"

builder = StateGraph(State)
builder.add_node(node_a)
builder.add_node(node_b)

# Static edge: START -> node_a
builder.add_edge(START, "node_a")

# Conditional edge: node_a -> node_b or END
builder.add_conditional_edges("node_a", router)

# Static edge: node_b -> END
builder.add_edge("node_b", END)

graph = builder.compile()

Theoretical Basis

  • Directed graph theory -- A stateful graph is a directed graph G = (V, E) where V is the set of nodes and E is the set of edges. Static edges correspond to fixed arcs; conditional edges correspond to arcs chosen at runtime based on state predicates.
  • Control flow graphs -- The combination of static and conditional edges mirrors control flow in programming: static edges are sequential statements; conditional edges are if/else branches and loop back-edges.
  • Synchronization barriers -- Fan-in edges (multiple start keys to one end key) implement barrier synchronization from parallel computing, where a downstream task waits for all upstream tasks to complete before proceeding.

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment