Can't solve the KeyError: 'st.session_state has no key "d"

I’m trying to create an app using Gemini api but keep getting that error. forgive the mess.

def init_session_state():
    session_state = st.session_state
    if 'd' not in session_state:
        session_state.d = 0
        
init_session_state()  
      
def main():
    st.title("Travel Planner")

    global response
    response = ""
    current_day = 0
    
    d = 0
    session_state = st.session_state
    if 'd' not in session_state:
        session_state.d = d
        
    col1, col2, col3 = st.columns([1, 1, 2])

    with col1:
        st.sidebar.header("Trip Details")
        country = st.sidebar.text_input("Enter the country", "Russia")
        city = st.sidebar.text_input("Enter the city", "Moscow")
        days = st.sidebar.number_input("For how many days?",  min_value=1, value=3)
        members = st.sidebar.number_input("How big is your group?",  min_value=1, value=1)
        if st.sidebar.button("Generate Trip Plan"):
            model = Gemini(city, country, days, members)
            response = json.loads(model.get_response(markdown=False))
            
    with col2:
        st.title("Gemini Response")
               
        session_state = st.session_state
        if 'd' not in session_state:
            st.session_state.d = 0
            session_state = st.session_state

            
        current_day = f"Day {session_state.d + 1}"

        current_day_placeholder = st.empty()
        current_day_infos = st.empty()
        current_day_placeholder.write(f"**{current_day}**")
        current_day_infos.write(response[current_day].items())

        buton = st.button("Next Day")
        if buton and session_state.d != len(response)-1:

            session_state.d += 1
            if session_state.d < len(response):
                current_day = f"Day {session_state.d + 1}"
                current_day_placeholder.empty()
                current_day_placeholder.write(f"**{current_day}**")
                current_day_infos.write(response[current_day].items()) 

if __name__ == "__main__":
    init_session_state()
        
    main()

The error I get is

Traceback (most recent call last):
  File "c:\Users\Cihan\Desktop\web\gemini\app.py", line 104, in <module>
    main()
  File "c:\Users\Cihan\Desktop\web\gemini\app.py", line 63, in main
    current_day = f"Day {session_state.d + 1}"
                         ^^^^^^^^^^^^^^^
  File "C:\Users\Cihan\Desktop\web\ML\Lib\site-packages\streamlit\runtime\state\session_state_proxy.py", line 121, in __getattr__
    raise AttributeError(_missing_attr_error_message(key))
AttributeError: st.session_state has no attribute "d". Did you forget to initialize it? More info: https://docs.streamlit.io/library/advanced-features/session-state#initialization ```

For me your code raises TypeError in this line:

current_day_infos.write(response[current_day].items())

because both response and current_day are strings. After commenting out that line, it works without raising any errors.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.