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
Annotated[list[AnyMessage], add_messages].
Usage Example
Migration Guide
Instead of usingMessageGraph, 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 graphadd_edge()- Add an edge between nodesadd_conditional_edges()- Add conditional routingset_entry_point()- Set the starting nodeset_finish_point()- Set the ending nodecompile()- Compile into an executable graph
Examples
Basic Message Workflow
With Conditional Edges
See Also
- StateGraph - The recommended replacement for MessageGraph
- MessagesState - Pre-built state schema for message-based graphs
- add_messages - Message reducer function