Has somebody solved the session_state.messages error?

Hello everyone,
I have been trying to run this code for a while:

if "messages" not in st.session_state:
    st.session_state.messages = [] 

But I always get the same error: AttributeError: st.session_state has no attribute “messages”. Did you forget to initialize it?

I am running it VSCode using a .py file, I have seen that many people has this problem. Has someone solved it?

Many thanks in advance

Hi @brio

Your code itself will not cause an error. Please check later if you have:

  1. Either misspelt “messages” as … say… “message” OR
  2. Trying to assign / access st.session_state.messages before you issue the following statement
if "messages" not in st.session_state:
    st.session_state.messages = []


  1. You have used del st.session_state.messages somewhere and are later trying to access / assign st.session_state.messages

Going through the rest of your code will help you pinpoint the error.


1 Like

Hello many thanks.
My bad, I forgot to run it from the command line. It works, many thanks again.

I’m having a similar error and trying to make sure I understand. I’ve attached my code block below - do I need to have “st.session_state.messages=” prior to assigning the roles in the initialize chat history section?

import time
import streamlit as st
from utils import load_chain

# Custom image for the app icon and the assistant's avatar
company_logo = '/Users/brockbriggs/Documents/Github/notion-chat/gWorks_Logo.jpg'

# Configure Streamlit page
    page_title="Your Notion Chatbot",

# Initialize LLM chain in session_state
if 'chain' not in st.session_state:
    st.session_state['chain']= load_chain()

# Initialize chat history
if 'messages' not in st.session_state:
    # Start with first message from assistant
    st.session_state['messages'] = [{"role": "assistant", 
                                  "content": "Hi human! I am gWorks smart AI. How can I help you today?"}]

# Display chat messages from history on app rerun
# Custom avatar for the assistant, default avatar for user
for message in st.session_state.messages:
    if message["role"] == 'assistant':
        with st.chat_message(message["role"], avatar=company_logo):
        with st.chat_message(message["role"]):

# Chat logic
if query := st.chat_input("Ask me anything"):
    # Add user message to chat history
    st.session_state.messages.append({"role": "user", "content": query})
    # Display user message in chat message container
    with st.chat_message("user"):

    with st.chat_message("assistant", avatar=company_logo):
        message_placeholder = st.empty()
        # Send user's question to our chain
        result = st.session_state['chain']({"question": query})
        response = result['answer']
        full_response = ""

        # Simulate stream of response with milliseconds delay
        for chunk in response.split():
            full_response += chunk + " "
            # Add a blinking cursor to simulate typing
            message_placeholder.markdown(full_response + "▌")

    # Add assistant message to chat history
    st.session_state.messages.append({"role": "assistant", "content": response})

Run streamlit directly from the command line to solve it