I’m running into the KeyError: 'st.session_state has no key “whateverkey” problem and they are definitely initialized.
My app is run locally. Versions:
Python 3.10.4
streamlit 1.32.2
Error:
Exception has occurred: KeyError
‘st.session_state has no key “generated”. Did you forget to initialize it? More info: Add statefulness to apps - Streamlit Docs’
KeyError:
During handling of the above exception, another exception occurred:
File “C:\Users\bradc\Dropbox\Projects\chat-std\main.py”, line 17, in
pprint(st.session_state[‘generated’])
KeyError: ‘st.session_state has no key “generated”. Did you forget to initialize it? More info: Add statefulness to apps - Streamlit Docs’
Code:
import openai
import streamlit as st
from streamlit_chat import message
from pprint import pprint
# Setting page title and header
st.set_page_config(page_title="AVA", page_icon=":robot_face:")
st.markdown("<h1 style='text-align: center;'>AVA - a totally harmless chatbot 😬</h1>", unsafe_allow_html=True)
# Set org ID and API key
openai.organization = ""
openai.api_key = ""
# Initialise session state variables
if 'generated' not in st.session_state:
st.session_state['generated'] = []
pprint(st.session_state['generated'])
if 'past' not in st.session_state:
st.session_state['past'] = []
pprint(st.session_state['past'])
if 'messages' not in st.session_state:
st.session_state['messages'] = [
{"role": "system", "content": "You are a helpful assistant."}
]
pprint(st.session_state['messages'])
if 'model_name' not in st.session_state:
st.session_state['model_name'] = []
if 'cost' not in st.session_state:
st.session_state['cost'] = []
if 'total_tokens' not in st.session_state:
st.session_state['total_tokens'] = []
print(st.session_state['total_tokens'])
if 'total_cost' not in st.session_state:
st.session_state.total_cost = 0.0
print(st.session_state['total_cost'])
Aside from taking out from streamlit_chat import message since I don’t currently have it installed, your script runs without error for me. Is there any chance you had a typo or misspelling when you last ran the code? Is your local file correctly resaved to reflect the current state shown in your post?
There is a bug I’ve seen from time to time, where Streamlit does not correctly catch that a file has been updated. In this case, you have to stop the file server, make sure all the files are saved, the streamlit run your_app.py to guarantee it’s seeing the current file. I’m not thinking that’s the case here, but mentioning it for posterity.
Start from the smallest bit of code and work up to see what is going wrong. Can you execute this correctly:
import streamlit as st
if 'generated' not in st.session_state:
st.session_state['generated'] = []
st.write(st.session_state['generated'])
If yes, slowly add in and change pieces until you get the error. If not, I will have to appeal to higher authorities…
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.