# Issue with s.chat\_message and Page not loading correctly (sometimes)

**URL:** <https://discuss.streamlit.io/t/issue-with-s-chat-message-and-page-not-loading-correctly-sometimes/61064>\
**Category:** Using Streamlit\
**Tags:** session-state, streamlit-cloud\
**Created:** [January 30, 2024, 3:35am UTC](https://discuss.streamlit.io/t/issue-with-s-chat-message-and-page-not-loading-correctly-sometimes/61064 "2024-01-30T03:35:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![brian-dka](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/brian-dka/32/37816_2.png) [@brian-dka](https://discuss.streamlit.io/u/brian-dka)\
**Post date:** [January 30, 2024, 3:35am UTC](https://discuss.streamlit.io/t/issue-with-s-chat-message-and-page-not-loading-correctly-sometimes/61064/1 "2024-01-30T03:35:59Z")

</div>

Greetings,

I’m developing a new page for my app that will use LangChain to interact with my Database via OpenAI - it’s working great for the most part!

However, during _some_ submissions of the `st.chat_input` field and the `st.chat_message`, the page is acting funny… as soon as I hit submit the message I just typed will overwrite the last message that was in the thread, and you don’t see a _RUNNING_ indicator for streamlit. If you wait a little bit and reload the page (by clicking the navigation, not refreshing) the question and answer will render property. It _almost_ seems that as the chat starts to get towards the bottom of the page and the input control it acts funny…

Anyone have any thoughts on where to look or what might be wrong?

[Video of Behavior](https://github.com/broepke/deadpool-app/blob/main/bug_recording.mov)

1. Community Cloud: [https://deadpool.streamlit.app](https://deadpool.streamlit.app)
2. There are no error messages to share
3. [Repo and Actual File](https://github.com/broepke/deadpool-app/blob/main/pages/2_Ask_the%20Arbiter.py)
4. Versions:  
Python 3.11.6  
Streamlit, version 1.30.0

_Note: Please don’t flag this as an advertisement - you didnt’ read this if you think that…_  
Edit to try to unblock 🙂

---

<div class="post-metadata">

**Author:** ![brian-dka](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/brian-dka/32/37816_2.png) [@brian-dka](https://discuss.streamlit.io/u/brian-dka)\
**Post date:** [January 30, 2024, 2:13pm UTC](https://discuss.streamlit.io/t/issue-with-s-chat-message-and-page-not-loading-correctly-sometimes/61064/2 "2024-01-30T14:13:20Z")

</div>

I found that this is an issue with the cookie manager used by `streamlit-authenticator` - i’ll keep an eye on that thread and issue:

> <https://github.com/mkhorasani/Streamlit-Authenticator/issues/99>
>
> I created a minimum wrapper for the openai chat completion api. It runs locally …and on the Streamlit cloud without issue. When I add authentication, the local version still works fine but if I deploy to the streamlit community cloud, the chat stops working after one or two user inputs. Below is the code that includes the authentication.
> 
> \`\`\`
> import openai
> import streamlit as st
> import streamlit\_authenticator as stauth
> 
> st.title("ChatGPT-like clone")
> 
> openai.api\_key = st.secrets\["OPENAI\_API\_KEY"\]
> 
> authenticator = stauth.Authenticate(
> dict(st.secrets\['credentials'\]),
> st.secrets\['cookie'\]\['name'\],
> st.secrets\['cookie'\]\['key'\],
> st.secrets\['cookie'\]\['expiry\_days'\],
> st.secrets\['preauthorized'\]
> )
> 
> name, authentication\_status, username = authenticator.login('Login', 'sidebar') # location is 'sidebar' or 'main'
> 
> if authentication\_status:
> 
> if "openai\_model" not in st.session\_state:
> st.session\_state\["openai\_model"\] = "gpt-3.5-turbo"
> 
> if "messages" not in st.session\_state:
> st.session\_state.messages = \[\]
> 
> for message in st.session\_state.messages:
> with st.chat\_message(message\["role"\]):
> st.markdown(message\["content"\])
> 
> if prompt := st.chat\_input("What is up?"):
> st.session\_state.messages.append({"role": "user", "content": prompt})
> with st.chat\_message("user"):
> st.markdown(prompt)
> 
> with st.chat\_message("assistant"):
> message\_placeholder = st.empty()
> full\_response = ""
> response = openai.chat.completions.create(
> model=st.session\_state\["openai\_model"\],
> messages=st.session\_state.messages,
> stream=True,
> )
> for part in response:
> full\_response += (part.choices\[0\].delta.content or "")
> message\_placeholder.markdown(full\_response + "▌")
> message\_placeholder.markdown(full\_response)
> st.session\_state.messages.append({"role": "assistant", "content": full\_response})
> 
> elif authentication\_status == False:
> st.error('Username/password is incorrect')
> elif authentication\_status == None:
> st.warning('Please enter your username and password')
> 
> \`\`\`
> If I comment out the authentication, I can deploy to the Streamlit cloud and it works fine. See below
> \`\`\`
> import openai
> import streamlit as st
> import streamlit\_authenticator as stauth
> 
> st.title("ChatGPT-like clone")
> 
> openai.api\_key = st.secrets\["OPENAI\_API\_KEY"\]
> 
> \# authenticator = stauth.Authenticate(
> \# dict(st.secrets\['credentials'\]),
> \# st.secrets\['cookie'\]\['name'\],
> \# st.secrets\['cookie'\]\['key'\],
> \# st.secrets\['cookie'\]\['expiry\_days'\],
> \# st.secrets\['preauthorized'\]
> \# )
> 
> \# name, authentication\_status, username = authenticator.login('Login', 'sidebar') # location is 'sidebar' or 'main'
> 
> \# if authentication\_status:
> 
> if "openai\_model" not in st.session\_state:
> st.session\_state\["openai\_model"\] = "gpt-3.5-turbo"
> 
> if "messages" not in st.session\_state:
> st.session\_state.messages = \[\]
> 
> for message in st.session\_state.messages:
> with st.chat\_message(message\["role"\]):
> st.markdown(message\["content"\])
> 
> if prompt := st.chat\_input("What is up?"):
> st.session\_state.messages.append({"role": "user", "content": prompt})
> with st.chat\_message("user"):
> st.markdown(prompt)
> 
> with st.chat\_message("assistant"):
> message\_placeholder = st.empty()
> full\_response = ""
> response = openai.chat.completions.create(
> model=st.session\_state\["openai\_model"\],
> messages=st.session\_state.messages,
> stream=True,
> )
> for part in response:
> full\_response += (part.choices\[0\].delta.content or "")
> message\_placeholder.markdown(full\_response + "▌")
> message\_placeholder.markdown(full\_response)
> st.session\_state.messages.append({"role": "assistant", "content": full\_response})
> 
> \# elif authentication\_status == False:
> \# st.error('Username/password is incorrect')
> \# elif authentication\_status == None:
> \# st.warning('Please enter your username and password')
> 
> \`\`\`
> 
> I have tried a few variations. As soon as the code 
> \`\`\`
> \# authenticator = stauth.Authenticate(
> \# dict(st.secrets\['credentials'\]),
> \# st.secrets\['cookie'\]\['name'\],
> \# st.secrets\['cookie'\]\['key'\],
> \# st.secrets\['cookie'\]\['expiry\_days'\],
> \# st.secrets\['preauthorized'\]
> \# )
> \`\`\`
> is uncommented (even if I don't check the authentication status), chat stops working after one or two user inputs

_Note: Please don’t flag this as an advertisement - you didnt’ read this if you think that…_

---

<div class="post-metadata">

**Author:** ![sahirmaharaj](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/sahirmaharaj/32/54744_2.png) [@sahirmaharaj](https://discuss.streamlit.io/u/sahirmaharaj)\
**Post date:** [February 3, 2024, 5:15pm UTC](https://discuss.streamlit.io/t/issue-with-s-chat-message-and-page-not-loading-correctly-sometimes/61064/3 "2024-02-03T17:15:15Z")

</div>

Hello @brian-dka,

Here’s how you can manage a chat application in Streamlit using session state:

```auto
import streamlit as st

if 'chat_history' not in st.session_state:
    st.session_state.chat_history = []

for message in st.session_state.chat_history:
    st.chat_message(message['role'], message['content'])

user_input = st.chat_input("Say something...")
if user_input:
    st.session_state.chat_history.append({'role': 'user', 'content': user_input})
    
    response = "This is a response to your message."
    
    st.session_state.chat_history.append({'role': 'bot', 'content': response})

```

Kind Regards,  
Sahir Maharaj  
Data Scientist | AI Engineer

P.S. Lets connect on [LinkedIn](https://www.linkedin.com/in/sahir-maharaj/)!

➤ Website: [https://sahirmaharaj.com](https://sahirmaharaj.com/)  
➤ Email: [sahir@sahirmaharaj.com](mailto:sahir@sahirmaharaj.com)  
➤ Want me to build your solution? [Lets chat about how I can assist!](https://topmate.io/sahirmaharaj)  
➤ Join my Medium community of 30k readers! [Sharing my knowledge about data science and AI](https://medium.com/@sahirmaharaj)  
➤ 100+ FREE Power BI Themes: [Download Now](https://www.powerbi.style/)

---

<div class="post-metadata">

**Author:** ![brian-dka](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/brian-dka/32/37816_2.png) [@brian-dka](https://discuss.streamlit.io/u/brian-dka)\
**Post date:** [February 6, 2024, 4:13pm UTC](https://discuss.streamlit.io/t/issue-with-s-chat-message-and-page-not-loading-correctly-sometimes/61064/4 "2024-02-06T16:13:31Z")

</div>

Thank you @sahirmaharaj ! This results in the same behaviour - looks like this is a common bug from the `streamlit-authentication`. The author is working on it.

---

<div class="post-metadata">

**Author:** ![sahirmaharaj](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/sahirmaharaj/32/54744_2.png) [@sahirmaharaj](https://discuss.streamlit.io/u/sahirmaharaj)\
**Post date:** [February 6, 2024, 4:39pm UTC](https://discuss.streamlit.io/t/issue-with-s-chat-message-and-page-not-loading-correctly-sometimes/61064/5 "2024-02-06T16:39:57Z")

</div>

It’s my pleasure @brian-dka. Feel free to let me know if you might have any further questions!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/streamlit/original/3X/c/f/cf4c2ee0d938358f82448c5fe4b9c3c91d7ce19c.svg) [@system](https://discuss.streamlit.io/u/system)\
**Post date:** [August 4, 2024, 4:40pm UTC](https://discuss.streamlit.io/t/issue-with-s-chat-message-and-page-not-loading-correctly-sometimes/61064/6 "2024-08-04T16:40:09Z")

</div>

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