Chat Message Assistant Component getting pushed into User Message


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"):
        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}")

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.


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.