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?