Chat History & MemoryLesson 8

Streamlit Chat Message History

After Introduction to Streamlit, store turns in st.session_state with StreamlitChatMessageHistory. RunnableWithMessageHistory stays the same — only get_session_history returns the Streamlit store instead of Redis or MongoDB.

Before you start

Complete Introduction to Streamlit first. Here you add LangChain history to the same HTML Questions page — session_id is demo-1.

How it works

Streamlit reruns the script on each submit. A plain Python list would reset; StreamlitChatMessageHistory reads and writes st.session_state so prior turns survive the rerun.

Plain Python variable

history = []

# Lost when Streamlit reruns
# the script on each click

StreamlitChatMessageHistory

msgs = StreamlitChatMessageHistory(
    key="langchain_messages",
)

# Backed by st.session_state —
# survives reruns in the tab
Streamlit reruns your script top-to-bottom on every interaction. StreamlitChatMessageHistory reads and writes st.session_state[key] so past turns are still there.

Install

Activate the venv from Project Setup, then install Streamlit and the community package. See Streamlit installation if you have not set it up yet.

pip install streamlit langchain-community

get_session_history

Create one StreamlitChatMessageHistory instance and return it from a lambda — messages live in st.session_state["langchain_messages"].

from langchain_community.chat_message_histories import StreamlitChatMessageHistory

msgs = StreamlitChatMessageHistory(key="langchain_messages")

st.session_state

{
"langchain_messages": [
HumanMessage("What does the HTML <a> tag do?"),
AIMessage("The <a> tag creates a hyperlink."),
],
}
Default key is langchain_messages. Pass a custom key= if you run multiple history stores in one app.

Wrap and invoke

Same wrapper and invoke call as MongoDB Chat Message History lambda session_id: msgs replaces get_session_history.

chain_with_history = RunnableWithMessageHistory(
    chain,
    lambda session_id: msgs,
    input_messages_key="input",
    history_messages_key="history",
)

config = {"configurable": {"session_id": "demo-1"}}

Each submit

On every rerun, render prior messages with st.chat_message. When st.chat_input has a new question, invoke the chain and write the reply.

for msg in msgs.messages:
    st.chat_message(msg.type).write(msg.content)

if question := st.chat_input("Type a question…"):
    st.chat_message("human").write(question)
    response = chain_with_history.invoke({"input": question}, config)
    st.chat_message("ai").write(response.content)
Streamlit HTML Questions at localhost:8501 — title and st.chat_input
Same layout as Introduction to Streamlit. After a submit, st.chat_message shows prior turns and invoke loads them into the chain on the next rerun.

The demo script

streamlit_chat_message_history_demo.py runs the same two HTML <a> questions with session_id="demo-1" in session state.

streamlit_chat_message_history_demo.py
"""streamlit_chat_message_history_demo.py"""
msgs = StreamlitChatMessageHistory(key="langchain_messages")
RunnableWithMessageHistory(chain, lambda session_id: msgs, …)
st.chat_input · st.chat_message

Download the code

streamlit_chat_message_history_demo.py

Two HTML questions — Streamlit session state

Download .py
Requires Streamlit and an OpenAI API key in .env. Venv from Project Setup.

Run it

Activate the venv from Project Setup, set OPENAI_API_KEY in .env, then from the folder that contains the demo file:

streamlit run streamlit_chat_message_history_demo.py
PowerShell — (.venv) active
(.venv) PS C:\projects\langchain-course> streamlit run streamlit_chat_message_history_demo.py
You can now view your Streamlit app in your browser.
Local URL: http://localhost:8501
Human: What does the HTML <a> tag do?
AI: The <a> tag creates a hyperlink to another page or resource.
Human: What attribute opens the link in a new tab?
AI: Use target="_blank" on the <a> tag.
The second answer works because prior turns stay in st.session_state across reruns.

Quick reference

What's Next

You have finished the Chat History & Memory module. Return to the course home for LCEL chains and later topics.