St.feedback in a chat

The following script runs perfectly

import streamlit as st
nstars = st.feedback("stars") 
if nstars is not None:
    st.success(f"You selected {nstars+1} star(s). Thanks for rating!", icon="šŸ˜€")

However, in a chat with a if-statement, the success message is not printed.

if prompt := st.chat_input("Start your chat here!",key="question"):
    with st.chat_message("user"):
        st.markdown(prompt)
    st.session_state.messages.append({"role": "user", "content": prompt})
    nstars = st.feedback("stars") 
    if nstars is not None:
        st.success(f"You selected {nstars+1} star(s). Thanks for rating!", icon="šŸ˜€")

Does anyone know how to resolve this?

This is an anti-pattern. Itā€™s like having a button nested in a button. The st.feedback widget will only appear immediately after a user submits a chat message. If the user interacts with the feedback widget, the resulting rerun will have if prompt... evaluated to False, so there will be no st.feeback to evaluate.

Thereā€™s a variety of ways to implement it, depending on how you want to save feedback and if you want users to be able to change their mind or vote on older replies. Hereā€™s just one example: