AI / LangGraph LangChain Interview questions
What is the difference between MessageGraph and StateGraph?
MessageGraph is a specialised version of StateGraph where the entire state is a single list of messages (using the add_messages reducer). Nodes receive the message list and return new messages to append. StateGraph is the general-purpose graph where you define any TypedDict as the state, with full control over all fields and their reducers.
| Feature | MessageGraph | StateGraph |
|---|---|---|
| State structure | Always a list of BaseMessage objects | Any TypedDict with any fields |
| Node input | List of messages | Full state dict |
| Node output | One or more messages to append | Partial dict of any fields to update |
| Custom fields | Not supported | Any fields: scores, iteration counts, flags, etc. |
| Status | Simpler but less flexible | Recommended for all but trivial chatbots |
from langgraph.graph import MessageGraph # MessageGraph - state is just the messages list graph = MessageGraph() graph.add_node("model", lambda msgs: llm.invoke(msgs)) graph.set_entry_point("model") graph.set_finish_point("model")
MessageGraph was the original LangGraph API and is still useful for pure chatbot flows with no additional state. For anything more complex, StateGraph with Annotated[list, add_messages] for the messages field is preferred because it lets you add other state fields alongside the conversation history.
More Related questions...