Hey everyone! I could be missing something simple but I am following the documentation on how to create a chat bot but everytime I try to initialize chat history I get this error:
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
If applicable, please provide the steps we should take to reproduce the error or specified behavior.
Expected behavior:
web pg app to load
Actual behavior:
receiving error: AttributeError: st.session_state has no attribute “messages”. Did you forget to initialize it? More info: Add statefulness to apps - Streamlit Docs
Debug info
Streamlit version: (1.27.2)
Python version: (3.10.1)
VS code
OS version: Sonoma Ver 14
Browser version:
Requirements file
only streamlit is required, using nothing else atm
Not sure what a requirements file is? Check out this doc and add a requirements file to your app.
Links
no repo yet, just playing around.
Additional information
If needed, add any other context about the problem here.
import streamlit as st
st.title("Echo Bot")
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Display chat messages from history on app rerun
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# React to user input
if prompt := st.chat_input("What is up?"):
# Display user message in chat message container
st.chat_message("user").markdown(prompt)
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
response = f"Echo: {prompt}"
# Display assistant response in chat message container
with st.chat_message("assistant"):
st.markdown(response)
# Add assistant response to chat history
st.session_state.messages.append({"role": "assistant", "content": response})
That is so bizarre. Does it happen if you switch to a different Streamlit version too? I’m wondering if I can reproduce if I update my computer to Sonoma
Did you forget to read the full error message, maybe? In my case, I accidentally tried to run the streamline app with python app.py, but you need to use streamline run app.py.
You should be promoted about this, if you accidentally did the same. In my case it said:
2023-10-24 09:00:23.842 Session state does not function when running a script without `streamlit run`
(...)
AttributeError: st.session_state has no attribute "messages". Did you forget to initialize it? More info: https://docs.streamlit.io/library/advanced-features/session-state#initialization
The confusion was that the streamlin run “warning” came at the very top of the log, whereas the final error came much later, making a bit challenging to debug.
@dilln - Could you please try run with streamlit cli. For example - **streamlit run main.py**. I got the same error while I was trying to run app with python main.py command.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.