Managing conversation history in Langgraph across an entire session

I’m working with Langgraph and need help storing conversation history in a single variable that persists during the whole chat session.

My current setup:

from langchain_openai import ChatOpenAI
from langgraph.graph import StateGraph, START, END
from typing import TypedDict, Annotated
from langchain_core.messages import AIMessage
from langgraph.graph.message import add_messages

model = ChatOpenAI(temperature=0.8)

class ChatState(TypedDict):
    conversation: Annotated[list, add_messages]
    
workflow = StateGraph(ChatState)

def ai_handler(state: ChatState):
    latest_input = state["conversation"][-1].content
    bot_reply = model.invoke(latest_input)
    return {"conversation": [AIMessage(content=bot_reply)]}

def history_tracker(state: ChatState):
    print("Full conversation:")
    all_messages = state['conversation']
    message_texts = [message.content for message in all_messages]
    print("Complete History:", ", ".join(message_texts))
    return state

workflow.add_node("ai_handler", ai_handler)
workflow.add_edge(START, "ai_handler")
workflow.add_node("history_tracker", history_tracker)
workflow.add_edge("ai_handler", "history_tracker")
workflow.add_edge("history_tracker", END)

compiled_graph = workflow.compile()

while True:
    query = input("You: ")
    if query.lower() in ['exit', 'stop']:
        print("Session ended")
        break
    
    for step in compiled_graph.stream({"conversation": ("user", query)}):
        for result in step.values():
            msgs = result["conversation"]
            if isinstance(msgs, list):
                recent_msg = msgs[-1]
            else:
                recent_msg = msgs
            print("Bot:", recent_msg.content)

What I want to achieve:

I need the conversation history to accumulate like this:

  • First exchange: “Hello” → “Hi there” → History shows both
  • Second exchange: “Tell me about Python” → “Python is a programming language” → History shows all four messages

Current problem:
I’m getting validation errors and the history doesn’t persist between interactions. How can I properly maintain and access the complete conversation history throughout the session?

You’re creating a new state for each interaction instead of keeping it persistent across the session. Initialize your state once, then keep updating it.

Here’s what worked for me with similar persistence issues:

# Initialize state once outside the loop
chat_state = {"conversation": []}

while True:
    query = input("You: ")
    if query.lower() in ['exit', 'stop']:
        print("Session ended")
        break
    
    # Update existing state instead of creating new one
    chat_state["conversation"].append(("user", query))
    
    for step in compiled_graph.stream(chat_state):
        for result in step.values():
            # Update the persistent state with new messages
            chat_state = result
            msgs = result["conversation"]
            recent_msg = msgs[-1] if isinstance(msgs, list) else msgs
            print("Bot:", recent_msg.content)

Keep chat_state outside the loop and pass the same state object each time. This lets your add_messages annotation accumulate messages instead of starting fresh every iteration.