KeyError: 'st.session_state has no key "_symbol". Did you forget to initialize it?

Streamlit : 1.37
pycharm: 3.1.x

I am developing a multi-pages app, it is going to share different widgets values across pages by preserving those values in session states. Here is part of my code, I want to save the user input from multi-selectbox() in session state ‘symbol ‘ via def save_value(key), which shared across pages, when switch back to this page, the saved value in session state ‘symbol’ write back to session state ‘_symbol’ via def get_value(key) and put it back as default value to multi-selectbox(). As a result, user do not need to input again.

Apparently, the script works fine but when I clear the cache and rerun, it trigger an error “KeyError: 'st.session_state has no key “_symbol”. Did you forget to initialize it?“

As seen in script, I have initialized the session_state of ‘_symbol’ at very beginning, why still got uninitialized error message?

Very appreciated if anyone have idea, thanks!

(Script)

import streamlit as st

if ‘_symbol’ not in st.session_state:
st.session_state[‘_symbol’] = ‘C’

def save_value(key):
st.session_state[key] = st.session_state[“_” + key]

def get_value(key):
st.session_state[“_” + key] = st.session_state[key]

if ‘symbol’ in st.session_state:
get_value(‘symbol’)

selected_symbol = st.multiselect(“Stock Symbol”, [‘A’,‘B’,‘C’,‘D’,‘E’], key=‘_symbol’,
on_change=save_value, args=(‘symbol’,))

st.write(selected_symbol)
st.write(st.session_state) # for debugging

(Replicate the problem)

  1. Run the script, select something in multi-selectbox (works fine)
  2. Click upper right menu → click clear cache → click re-run → error triggered as following

(Error message)
KeyError: ‘st.session_state has no key “_symbol”. Did you forget to initialize it? More info: Add statefulness to apps - Streamlit Docs
Traceback:
File “C:\Users\pc\AppData\Roaming\Python\Python310\site-packages\streamlit\runtime\scriptrunner\exec_code.py”, line 75, in exec_func_with_error_handling
result = func()
File “C:\Users\pc\AppData\Roaming\Python\Python310\site-packages\streamlit\runtime\scriptrunner\script_runner.py”, line 543, in code_to_exec
self._session_state.on_script_will_rerun(
File “C:\Users\pc\AppData\Roaming\Python\Python310\site-packages\streamlit\runtime\state\safe_session_state.py”, line 66, in on_script_will_rerun
self._state.on_script_will_rerun(latest_widget_states)
File “C:\Users\pc\AppData\Roaming\Python\Python310\site-packages\streamlit\runtime\state\session_state.py”, line 515, in on_script_will_rerun
self._call_callbacks()
File “C:\Users\pc\AppData\Roaming\Python\Python310\site-packages\streamlit\runtime\state\session_state.py”, line 528, in _call_callbacks
self.new_widget_state.call_callback(wid)
File “C:\Users\pc\AppData\Roaming\Python\Python310\site-packages\streamlit\runtime\state\session_state.py”, line 272, in call_callback
callback(*args, **kwargs)
File “C:\Users\pc\PycharmProjects\klee\2_test.py”, line 8, in save_value
st.session_state[key] = st.session_state["
" + key]
File “C:\Users\pc\AppData\Roaming\Python\Python310\site-packages\streamlit\runtime\state\session_state_proxy.py”, line 98, in getitem
return get_session_state()[key]
File “C:\Users\pc\AppData\Roaming\Python\Python310\site-packages\streamlit\runtime\state\safe_session_state.py”, line 94, in getitem
return self._state[key]
File “C:\Users\pc\AppData\Roaming\Python\Python310\site-packages\streamlit\runtime\state\session_state.py”, line 411, in getitem
raise KeyError(_missing_key_error_message(key))

Apparently this is intended behavior:

Session State can also be cleared by going to Settings → Clear Cache, followed by Rerunning the app.

Thanks for your reply!I have read the mentioned doc.

I still quite not understand why trigger uninitialized error when rerun. After clear cache, I know all the session states are deleted, when click rerun, i suppose the whole script run from top down again, and the 1st line of code must hit,

if ‘_symbol’ not in st.session_state:

st.session_state[‘_symbol’] = ‘C’

therefore the key '_symbol ’ must be assigned a session state value from very beginning and suppose not trigger KeyError: 'st.session_state has no key “_symbol”.

Am I misunderstand any key &session state concept?

As I am still developing the app and always click clear cache & rerun for debugging, and this error pops up every time after clear + rerun, that’s why I think there may be something wrong in my script, hope can remove this error.

Yes, I was chasing the wrong issue. It looks like, when you clear the cache and then rerun, the callback is called before the script starts. I wouldn’t expect that and I don’t know why it happens.

As a workaround you can rerun twice.