LangGraph

Code Examples

LangGraph with Tools

LangGraph: Basic

MessagesState vs GraphState

Since having a list of messages in your state is a common requirement, a prebuilt state called MessagesState is available to simplify message handling. MessagesState is defined with a single messages key, which is a list of AnyMessageobjects, and it uses the add_messages reducer for state updates.

Using MessagesState:

from langgraph.graph import StateGraph, START, END, MessagesState


graph_builder = StateGraph(MessagesState)

The StateGraph equivalent of MessagesState is as follows:

from typing import Annotated
from typing_extensions import TypedDict

from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages


class State(TypedDict):
    messages: Annotated[list, add_messages]

graph_builder = StateGraph(State)

Last updated