Clear input text on submit not working for my chatbot

I am currently working on a chatbot and runnning a streamlit locally on my macos system.
I want to clear the input_text once submit is pressed by the user and found this code on the discussion.
here

after implemennting on my work:
my code:

def submit():
    st.session_state.something = st.session_state.widget
    st.session_state.widget = ''

if "messages" not in st.session_state:
    st.session_state.messages = [
        {"role": "assistant", "content": "How may I help you today?"}
    ]

if "current_response" not in st.session_state:
    st.session_state.current_response = ""

for message in st.session_state.messages:
    chat_message(message["content"], is_user=message["role"] == "user")

if user_prompt := st.text_input("Your message here",on_change=submit):
    st.session_state.messages.append(
        {"role": "user", "content": user_prompt}
    )
    chat_message(user_prompt, is_user=True)

    response = model(user_prompt, max_length, temp)

    st.session_state.messages.append(
        {"role": "assistant", "content": response}
    )
    chat_message(response)

here is the error:
st.session_state has no attribute โ€œwidgetโ€. Did you forget to initialize it? More info: Add statefulness to apps - Streamlit Docs

1 Like

Hey @Pooja_Harihar,

It looks like st.session_state.widget isnโ€™t being initialized until the submit() function is being called (which doesnโ€™t happen until someone enters something in the st.text_input widget).

Initializing st.session_state.widget outside of the submit() function should help you avoid this error. E.g.:

if "widget" not in st.session_state:
   st.session_state.widget = "default value" # add what you want the default value to be
1 Like

Hi @Caroline thank you for your help
so i tried this:

if "widget" not in st.session_state:
    st.session_state.widget = ''

def submit():
    # Check if Enter key was pressed
    if st.session_state.widget.endswith('\n'):
        st.session_state.widget = ''  # Reset text input after submission

if "messages" not in st.session_state:
    st.session_state.messages = [
        {"role": "assistant", "content": "How may I help you today?"}
    ]

but it doesnt do the task, and once i click enter it still shows the text and is not cleared.
is it becuase I am using keys instead of submit button?

1 Like

Hey @Pooja_Harihar,

Is that snippet the full code youโ€™re running?

1 Like

no @Caroline ,
the full streamlit snippet is as follows:

styl = f"""
<style>
    .stTextInput {{
        position: fixed;
        bottom: 3rem;
    }}
</style>
"""
st.markdown(styl, unsafe_allow_html=True)

# Initialize session state variables
if "widget" not in st.session_state:
    st.session_state.widget = ''

def submit():
    # Check if Enter key was pressed
    if st.session_state.widget.endswith('\n'):
        st.session_state.widget = ''  # Reset text input after submission

if "messages" not in st.session_state:
    st.session_state.messages = [
        {"role": "assistant", "content": "How may I help you today?"}
    ]

if "current_response" not in st.session_state:
    st.session_state.current_response = ""

for message in st.session_state.messages:
    chat_message(message["content"], is_user=message["role"] == "user")

# Take questions from the chat input to pass to the LLM
if user_prompt := st.text_input("Your message here", on_change=submit):

    st.session_state.messages.append(
        {"role": "user", "content": user_prompt}
    )

    chat_message(user_prompt, is_user=True)
    response = model(user_prompt, max_length, temp)

    st.session_state.messages.append(
        {"role": "assistant", "content": response}
    )
    chat_message(response)
1 Like

Hey @Pooja_Harihar,

Sorry for the delayed response. Can you share the link to the GitHub repo for your app so I can run the full app? That snippet unfortunately isnโ€™t runnable

1 Like

There is no reason why this should have any effect on the text input.

st.session_state.widget = ''  # Reset text input after submission
1 Like

@Goyo

Iโ€™m sorry just to make myself understand,
does that mean
it resets the value of st.session_state.widget not on the streamlit but on the variable only?

1 Like

@Caroline since its llm based and still under work , it is not uploaded in the github yet.

1 Like

Not sure what you mean. st.session_state.widget is not related to the text input at all. Why would you expect otherwise?

1 Like

@Goyo then is there anyway where i can change the text. (basically reset the textbox)
AIM
The user types their question in the textbox and I want the textbox cleared when user presses enter key through keyboard.

1 Like
st.session_state.text = ""
st.text_input("Your input here", key="text")

Or you can use chat_input instead.

1 Like