There seems to be a bug in a certain specific situation that causes the same text to be rendered twice at the same time, once normally and once as a gradually fading away text. This happened in my chat app DocDocGo and it took me a while to narrow down the issue.
Here is a simple demo app I have created to illustrate the issue:
I haven’t dug deep but my guess is that the issue may have something to do with the accounting of React keys to keep track of the identity of elements.
I have figured out a hacky fix for the issue, which is included in the code as a comment.
My code (link to full repo in OP) only st.writes each message once. Inserting st.empty() before the st.writes fixes the issue. Obviously it’s a hacky fix, but I’m hoping it can give the Streamlit devs a clue about the source of the issue.
I use the st.stream_write() and by placing an st.empty() after the st.stream_write() worked for me.
# Get the assistant's response
with st.chat_message("assistant"):
msg = st.write_stream(get_response(prompt, current_chat_hist))
st.empty() # fixes the ghosting bug...
for i, msg in enumerate(st.session_state.messages):
if msg["role"] == "assistant":
with col1:
with st.chat_message("assistant", avatar="image/bot.png"):
st.markdown(msg["content"])
st.empty()
I used st.empty() but it didn’t worked , streamlit version=1.38.0,
@sum Did you try to use st.empty() in the other way I suggested? Depending on your case, you might need more than one st.empty() if you’re writing it outside/after your message. When you follow with st.empty(), you’ll need as many empties as you had stale elements, basically. The alternate way I proposed is a little more robust.
yup I tried this way too but it failed for my scenario…
for i, msg in enumerate(st.session_state.messages):
if msg["role"] == "assistant":
with col1:
with st.chat_message("assistant", avatar="image/bot.png"),st.empty():
st.markdown(msg["content"])
Can you share more information about what exactly is happening in your case? You might want to create a new thread, share a link to your code, and include a screenshot or video link showing how stale elements are appearing for you.
Thanks @mathcatsand now its working I tried some changes in my code by adding placeholder = st.chat_message("assistant", avatar="image/bot.png") message_placeholder = placeholder.empty()
I have a similar problem. Just switched from the version 1.32 to 1.39 and it started doing it - please Streamlit team fix this as soon as possible, this wasn’t an issue before.
My code is something like this:
with st.chat_message('user'):
st.markdown(prompt)
with st.chat_message('assistant'):
with st.spinner('Asking GenAI...'):
... # generating response from the LLM
with st.spinner('Processing response...'):
st.code({code_from_llm})
... # producing plots/tables based on the generated code from LLM using
# st.plotly_chart, st.data_frame, st.markdown, etc.
Would anyone please suggest how to make a temporary fix before Streamlit team fixes this issue?
Using st.empty() inside the spinners doesn’t really make any difference.
But as you suggested using it like this:
with st.chat_message('user'):
st.markdown(prompt)
with st.chat_message('assistant'), st.empty():
with st.spinner('Asking GenAI...'):
... # generating response from the LLM
with st.spinner('Processing response...'):
st.code({code_from_llm})
... # producing plots/tables based on the generated code from LLM using
# st.plotly_chart, st.data_frame, st.markdown, etc.
the ghosting stops. However, everytime I send a message, I don’t see the assistant response after the spinners stop spinning. I guess it’s because I am generating those UI elements (such as st.code, st.dataframe, st.ploty_chart) inside the second spinner “with st.spinner(‘Processing response…’):”?
Hi, did anyone from Streamlit development team read this thread? Let me please know if this is a known bug and if it’s being worked on. Also, what is actually the reason this suddenly started occuring in the recent versions of Streamlit? Thank you for the support!
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.