liveBook · Manning

Chat_message

As explained in Build Python Web Apps with Streamlit

st.chat_message is a Streamlit display widget that renders a conversational message container styled for a specified speaker role.

A chatbot needs to make it clear who said what. `st.chat_message` creates a Streamlit message container styled for a speaker. Pass `"human"` or `"ai"` to show the corresponding avatar and distinguish the messages visually. section 10.2.3

Show a prompt and reply

After `st.chat_input` returns a prompt, display it with `st.chat_message("human").markdown(prompt)`. Generate a reply, then display it with `st.chat_message("ai").markdown(reply)`. You can also write `with st.chat_message("human"):` and put `st.markdown(prompt)` inside the container. section 10.2.3

Displaying a reply is not the same as remembering a conversation. A first-pass chatbot can answer individual prompts yet fail to show earlier exchanges. To show the full conversation, retrieve its message history and render each entry in its own container, using `message.type` for the role and `message.content` for the text. section 10.2.3 section 10.3.2

...
bot = st.session_state.bot

for message in bot.get_history():
    st.chat_message(message.type).markdown(message.content)

if human_message_text := st.chat_input("Chat with Nibby!"):
    ...

This short frontend loop renders saved messages before accepting the next prompt. section 10.3.2

How Build Python Web Apps with Streamlit covers it

How deep it goes
Shows direct rendering, container syntax, and a history loop; leaves conversation persistence to the chatbot’s state handling.
Examples
Uses a single-prompt chatbot and a loop that displays each saved human or AI message.
What it assumes
Assumes basic Python and a Streamlit app; demonstrates role arguments and message containers through chatbot code.
What you can do after
Apply it via Streamlit to display a new exchange or render a retrieved conversation history.
How it connects
Pairs with `st.chat_input`, Markdown rendering, conversation history, and LangGraph message types.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Python Web Apps with Streamlit. The section links point to the parts of the book it draws on.