star

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:

  • If name is "user" or "human", the message will have a default user icon.
  • If name is "ai" or "assistant", the message will have a default bot icon.
  • For all other values of name, the message will show the first letter of the name.

In addition to the types supported by st.image (like URLs or numpy arrays), the following strings are valid:

  • A single-character emoji. For example, you can set avatar="๐Ÿง‘โ€๐Ÿ’ป" or avatar="๐Ÿฆ–". Emoji short codes are not supported.

  • An icon from the Material Symbols library (outlined style) in the format ":material/icon_name:" where "icon_name" is the name of the icon in snake case.

    For example, icon=":material/thumb_up:" will display the Thumb Up icon. Find additional icons in the Material Symbols font library.

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.

forum

Still have questions?

Our forums are full of helpful information and Streamlit experts.