Grit
  • Welcome
  • gritholdings SDK
    • API Reference
    • Collection of Useful Prompts
  • Evaluation
    • Evaluators
  • Concepts
  • LangChain
  • LangGraph
  • Agent Architectures
    • Planner-Worker-Solver
  • Prompt Management
  • Memory
  • Guardrails
  • Others
    • AWS
      • AWS CDK
    • Migrating a Service to Docker
    • React
    • Salesforce
      • Object Reference
      • Writing Apex
      • Experience Cloud
    • Grit API Reference
    • Style Guide for Documentation and User Interface Text
Powered by GitBook
On this page
  • Code Examples
  • MessagesState vs GraphState

LangGraph

PreviousLangChainNextAgent Architectures

Last updated 8 months ago

Code Examples

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)
LangGraph with Tools
LangGraph: Basic