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 (str, numpy.ndarray, or BytesIO)

The avatar shown next to the message. Can be one of:

  • A single emoji, e.g. "🧑‍💻", "🤖", "🦖". Shortcodes are not supported.
  • An image using one of the formats allowed for st.image: path of a local
    image file; URL to fetch the image from; an SVG image; array of shape (w,h) or (w,h,1) for a monochrome image, (w,h,3) for a color image, or (w,h,4) for an RGBA image.

If None (default), uses default icons if name is "user", "assistant", "ai", "human" or the first letter of the name value.

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.

Was this page helpful?

editEdit this page on GitHub