Skip to main content
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.
The MessageGraph class is a specialized version of StateGraph where the entire state is a single, append-only list of messages. Defined in: langgraph/graph/message.py:251

Overview

MessageGraph is a subclass of StateGraph whose entire state is a single list of messages. Each node in a MessageGraph takes a list of messages as input and returns zero or more messages as output. The add_messages function is used to merge the output messages from each node into the existing list of messages in the graph’s state.

Constructor

Create a new MessageGraph instance. The state schema is automatically set to Annotated[list[AnyMessage], add_messages].

Usage Example

Migration Guide

Instead of using MessageGraph, use StateGraph with MessagesState or a custom state that includes a messages field:

Before (MessageGraph)

After (StateGraph with MessagesState)

Alternative: Custom State

Methods

MessageGraph inherits all methods from StateGraph. See the StateGraph reference for detailed documentation of available methods:
  • add_node() - Add a node to the graph
  • add_edge() - Add an edge between nodes
  • add_conditional_edges() - Add conditional routing
  • set_entry_point() - Set the starting node
  • set_finish_point() - Set the ending node
  • compile() - Compile into an executable graph

Examples

Basic Message Workflow

With Conditional Edges

See Also