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 
Community Cloud:  https://deadpool.streamlit.app  
There are no error messages to share 
Repo and Actual File  
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 
             
            
               
               
              1 Like 
            
            
           
          
            
            
              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:
  
  
    
  
  
    
    
      
        opened 12:33PM - 14 Nov 23 UTC 
      
      
     
    
        
          duplicate
         
    
   
 
  
    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… 
             
            
               
               
              2 Likes 
            
            
           
          
            
            
              Hello @brian-dka ,
Here’s how you can manage a chat application in Streamlit using session state:
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 !
➤ Website: https://sahirmaharaj.com  
➤ Email: sahir@sahirmaharaj.com  
➤ Want me to build your solution? Lets chat about how I can assist!  
➤ Join my Medium community of 30k readers! Sharing my knowledge about data science and AI  
➤ 100+ FREE Power BI Themes: Download Now 
             
            
               
               
              1 Like 
            
            
           
          
            
            
              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.
             
            
               
               
              1 Like 
            
            
           
          
            
            
              It’s my pleasure @brian-dka . Feel free to let me know if you might have any further questions!
             
            
               
               
              1 Like 
            
            
           
          
            
              
                system  
                
                  Closed 
               
              
                  
                    August 4, 2024,  4:40pm
                   
                   
              6 
               
             
            
              This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.