langgraph.graph module provides the primary interfaces for building stateful, multi-actor applications with LangGraph. This module exports the core graph building classes and utilities.
Module Exports
The graph module exports the following classes and constants:- StateGraph - Main graph builder class for state-based graphs
- MessageGraph - Specialized graph for message-based workflows (deprecated)
- MessagesState - TypedDict for message-based state
- add_messages - Reducer function for merging message lists
- START - Constant for the graph entry point
- END - Constant for the graph exit point
langgraph/graph/__init__.py
Core Classes
StateGraph
A graph whose nodes communicate by reading and writing to a shared state.The signature of each node is
State -> Partial<State>. Each state key can optionally be annotated with a reducer function that will be used to aggregate the values of that key received from multiple nodes.Warning: StateGraph is a builder class and cannot be used directly for execution. You must first call .compile() to create an executable graph that supports methods like invoke(), stream(), astream(), and ainvoke().See the StateGraph reference for detailed documentation.Defined in: langgraph/graph/state.py:113MessageGraph
A StateGraph where every node receives a list of messages as input and returns one or more messages as output.Deprecated: MessageGraph is deprecated in LangGraph v1.0.0 and will be removed in v2.0.0. Please use
StateGraph with a messages key instead.MessageGraph is a subclass of StateGraph whose entire state is a single, append-only list of messages. Each node takes a list of messages as input and returns zero or more messages as output.See the MessageGraph reference for detailed documentation.Defined in: langgraph/graph/message.py:251MessagesState
A pre-built TypedDict for message-based state.Provides a convenient state schema with a single
messages key that uses the add_messages reducer.Defined in: langgraph/graph/message.py:307Attributes
List of messages with automatic merging via the
add_messages reducer.Usage Example
Functions
add_messages
Merges two lists of messages, updating existing messages by ID.By default, this ensures the state is “append-only”, unless the new message has the same ID as an existing message. When messages have matching IDs, the new message replaces the old one.Defined in:
langgraph/graph/message.py:61Parameters
The base list of messages.
The list of messages (or single message) to merge into the base list.
The format to return messages in. If
None, messages are returned as-is. If 'langchain-openai', messages are formatted to match OpenAI message format.Requires langchain-core>=0.3.11 to use this feature.Returns
A new list of messages with the messages from
right merged into left. If a message in right has the same ID as a message in left, the message from right replaces the message from left.Usage Example
Constants
START
The first (maybe virtual) node in graph-style Pregel.Use this constant to add edges from the graph entry point to your first node(s). Equivalent to the string
"__start__".Defined in: langgraph/constants.py:30Usage Example
END
The last (maybe virtual) node in graph-style Pregel.Use this constant to mark terminal nodes in your graph. When execution reaches a node with an edge to
END, the graph will cease execution. Equivalent to the string "__end__".Defined in: langgraph/constants.py:28