AI Hints
Home›LangGraph Tutorial›Chatbot Graphs
LangGraph · Lesson 28 of 120

MessagesState

Ready-made state.

⏱ 2 min read▶ Video at 29:45🎓 Free
Watch this lesson in the LangGraph courseWatch on YouTube ↗Full LangGraph course

So many graphs need a messages key with add messages that LangGraph provides it ready made. It's called messages state. You can use it as it is, or extend it with your own keys.

MessagesState
From the video: MessagesState
  • Very common
  • Ready-made
  • Extend it

Extending MessagesState

Our state extends messages state, so it already has messages with add messages. We add one key of our own, the customer's name. The node uses both. Input messages can be plain dictionaries now. Run it. The model greets the customer by name.

messages_state.pypython
from langchain.chat_models import init_chat_model
from langgraph.graph import MessagesState, StateGraph, START

model = init_chat_model("google_genai:gemini-flash-lite-latest", temperature=0)

class State(MessagesState):
    customer: str

def chat(s):
    prompt = [("system", f"Greet {s['customer']} by name. One sentence.")] + s["messages"]
    return {"messages": [model.invoke(prompt)]}

b = StateGraph(State); b.add_node(chat); b.add_edge(START, "chat")
out = b.compile().invoke({"customer": "Priya", "messages": [{"role": "user", "content": "Hi!"}]})
print(out["messages"][-1].text)
Output
Hello Priya, it's great to hear from you today!

MessagesState

Messages state is just a typed dictionary, with one key, messages, using add messages. Subclass it to add keys, like a customer name, a cart, or a counter. It's the usual starting point for chatbots and agents.

keyvalue
One keymessages
Reduceradd_messages
Subclassadd your keys
Use forchatbots, agents

Summary

Messages state gives you a messages key with add messages, ready to use or extend. Next, a chatbot graph with several turns.

Key takeaways
  • MessagesState
  • Subclass it
  • Dict messages work
Next up: A Chatbot Graph — one model node