Tip
Read the Build a basic LLM chat app tutorial to learn how to use st.chat_message
and st.chat_input
to build chat-based apps.
Insert a chat message container.
To add elements to the returned container, you can use with notation (preferred) or just call methods directly on the returned object. See the examples below.
Function signature[source] | |
---|---|
st.chat_message(name, *, avatar=None) | |
Parameters | |
name ("user", "assistant", "ai", "human", or str) | The name of the message author. Can be "human"/"user" or "ai"/"assistant" to enable preset styling and avatars. Currently, the name is not shown in the UI but is only set as an accessibility label. For accessibility reasons, you should not use an empty string. |
avatar (Anything supported by st.image, str, or None) | The avatar shown next to the message. If avatar is None (default), the icon will be determined from name as follows:
In addition to the types supported by st.image (like URLs or numpy arrays), the following strings are valid:
|
Returns | |
(Container) | A single container that can hold multiple elements. |
Examples
You can use with notation to insert any element into an expander
import streamlit as st import numpy as np with st.chat_message("user"): st.write("Hello π") st.line_chart(np.random.randn(30, 3))Or you can just call methods directly in the returned objects:
import streamlit as st import numpy as np message = st.chat_message("assistant") message.write("Hello human") message.bar_chart(np.random.randn(30, 3))
For an overview of the st.chat_message
and st.chat_input
API, check out this video tutorial by Chanin Nantasenamat (@dataprofessor), a Senior Developer Advocate at Streamlit.
Still have questions?
Our forums are full of helpful information and Streamlit experts.