My app RERUNS from the beginning every time I enter a message in an st.chat_input(). How can I prevent the rerun from happening?

I have a simple chat bot application with a simple chat interface.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import streamlit as st
# Function to handle when some text is entered into the chat_input() by the user
def handle_chat_input():
# setting session state variables
st.session_state.human_input = st.session_state.user_input
st.session_state.topic = st.session_state.user_input
st.session_state.generating = True
st.session_state.asking = False
# Function to render the st.chat_input()
def render_chat_message():
human_input = st.chat_input("Type your message here...", key="user_input", on_submit=handle_chat_input)
render_chat_message()
</code>
<code>import streamlit as st # Function to handle when some text is entered into the chat_input() by the user def handle_chat_input(): # setting session state variables st.session_state.human_input = st.session_state.user_input st.session_state.topic = st.session_state.user_input st.session_state.generating = True st.session_state.asking = False # Function to render the st.chat_input() def render_chat_message(): human_input = st.chat_input("Type your message here...", key="user_input", on_submit=handle_chat_input) render_chat_message() </code>
import streamlit as st

# Function to handle when some text is entered into the chat_input() by the user
def handle_chat_input():
    # setting session state variables
    st.session_state.human_input = st.session_state.user_input
    st.session_state.topic = st.session_state.user_input
    st.session_state.generating = True
    st.session_state.asking = False

# Function to render the st.chat_input()
def render_chat_message():
        human_input = st.chat_input("Type your message here...", key="user_input", on_submit=handle_chat_input)

render_chat_message()

Chatbot Code Logic:
Somewhere in the backend of my Chatbot, I have a code logic which waits for the
st.session_state.asking
to be set to False by the chat_input(). And then the code logic gets the human input from the st.session_state.human_input and prompts the LLM with it and proceeds with the execution. This is how the LLM get the input prompt from the user.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@tool
def ask_human_input_tool(question_for_human: str) -> str:
"""This tool is used for asking a human input. This tool takes a question as an input and ask the input.
The tool return a the answer received."""
st.session_state.asking = True
# Wait for st.session_state.asking == False
while st.session_state.asking
print(st.session_state)
time.sleep(7)
human_input = st.session_state.human_message
return human_input
</code>
<code>@tool def ask_human_input_tool(question_for_human: str) -> str: """This tool is used for asking a human input. This tool takes a question as an input and ask the input. The tool return a the answer received.""" st.session_state.asking = True # Wait for st.session_state.asking == False while st.session_state.asking print(st.session_state) time.sleep(7) human_input = st.session_state.human_message return human_input </code>
@tool
def ask_human_input_tool(question_for_human: str) -> str:
    """This tool is used for asking a human input. This tool takes a question as an input and ask the input.
        The tool return a the answer received."""
    st.session_state.asking = True
    
    # Wait for st.session_state.asking == False
    while st.session_state.asking
        print(st.session_state)
        time.sleep(7)

    human_input = st.session_state.human_message
    return human_input

But the PROBLEM is every time I hit enter or submit the text in the chat_input(), my whole application runs again from the beginning. Now I understand that any time something must be updated on the screen, Streamlit reruns your entire Python script from top to bottom.

My Question

Because of this my chatbot executes and runs from the beginning, which I want to prevent from happening. How do I prevent the app rerun every time I interact with the widget? Is there a a concrete way to just disable the rerun?

Note: Should I not be using a while loop to wait for the session state to update? is there a better way to do it?


This is what I have tried:

  1. I wrapped the the render_chat_input() function with a @st.experimental_fragment as shown below:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code># Function to render the st.chat_input()
@st.experimental_fragment
def render_chat_message():
human_input = st.chat_input("Type your message here...", key="user_input", on_submit=handle_chat_input)
</code>
<code># Function to render the st.chat_input() @st.experimental_fragment def render_chat_message(): human_input = st.chat_input("Type your message here...", key="user_input", on_submit=handle_chat_input) </code>
# Function to render the st.chat_input()
@st.experimental_fragment
def render_chat_message():
        human_input = st.chat_input("Type your message here...", key="user_input", on_submit=handle_chat_input)

Now with this fragment rendered. The issue is that any interaction I have with this widget only updates this particular fragment. What I mean specifically by this is that when the session state state is changed in this fragment, it is not visible outside of this fragment. This is preventing my code logic from seeing the updates/changes made to the session state in the chat_input().

  1. I have also tried putting a st.text_input() in an st.form() as an alternative but the human input tool doesn’t see it too.

Or maybe I am using it wrong Can I see some example code for how it is implemented in a st.form()?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật