Chat Message Assistant Component getting pushed into User Message

Hi

Iā€™ve created a chat app using the the streamlit chat elements. I insert sources into the assistantā€™s response as follows:

    # Generate a response from the LLM
    response = QA_CHAIN(inputs)
    msg = response["text"]
    st.session_state.messages.append({"role": "assistant", "content": msg})
    with st.chat_message("assistant", avatar="./data/bot_avatar.jpg"):
        st.write(msg)
        with st.expander("Sources"):
            for i, doc in enumerate(docs):
                st.write(f"**Source {i+1}:** {doc.metadata['SourceFile']}")
                st.write(f"**Date:** {doc.metadata['ModDate']}")
                st.write(f"**Content:** {doc.page_content}")
                st.write("---")

This is what it looks like


When the user asks a subsequent question, the expander component (sources) end up in the userā€™s dialogue box, instead of staying the assistantā€™s dialogue box:

Can I please get some help with this?

1 Like

Had someone managed to resolve this or create a workaround? Got same issue with st.expander and st.popover.

streamlit==1.32.0

The code in the OP works as expected for me, but it doesnā€™t include the part where ā€œthe user asks a subsequent questionā€. So there may lie the problem.