Exception has occurred: KeyError?

Hello,

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'])
1 Like

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?

1 Like

It’s saved. The pprint()'s are newly added by me just to provide an access to the Key’s in question and show the exception.

It’s very strange.

1 Like

My first test was on Windows 10, the one were I posted the problem in this thread.

I just switched over to Ubuntu 22.04 and it throws an exception in the same spot. This makes me think that the example I’m using is doing this wrong.

1 Like

Two things come to mind.

  1. 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.
  2. 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…

1 Like

Ummm, never mind. I’m too stupid to live.

I wasn’t starting my app with streamlit.

I missed that part.

2 Likes

Glad you’ve figured it out. :smile:

1 Like

So, how does one run this in a debugger… say in VS Code?

1 Like

Check out this thread for some approaches people have used:

1 Like