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.
Function signature[source] | |
---|---|
st.chat_input(placeholder="Your message", *, key=None, max_chars=None, accept_file=False, file_type=None, disabled=False, on_submit=None, args=None, kwargs=None) | |
Parameters | |
placeholder (str) | A placeholder text shown when the chat input is empty. This defaults to "Your message". For accessibility reasons, you should not use an empty string. |
key (str or int) | An optional string or integer to use as the unique key for the widget. If this is omitted, a key will be generated for the widget based on its content. No two widgets may have the same key. |
max_chars (int or None) | The maximum number of characters that can be entered. If this is None (default), there will be no maximum. |
accept_file (bool or str) | Whether the chat input should accept files. This can be one of the following values:
When the widget is configured to accept files, the accepted file types can be configured with the file_type parameter. By default, uploaded files are limited to 200 MB each. You can configure this using the server.maxUploadSize config option. For more information on how to set config options, see config.toml. |
file_type (str, Sequence[str], or None) | The allowed file extension(s) for uploaded files. This can be one of the following types:
|
disabled (bool) | Whether the chat input should be disabled. This defaults to False. |
on_submit (callable) | An optional callback invoked when the chat input's value is submitted. |
args (tuple) | An optional tuple of args to pass to the callback. |
kwargs (dict) | An optional dict of kwargs to pass to the callback. |
Returns | |
(None, str, or dict-like) | The user's submission. This is one of the following types:
When the widget is configured to accept files and the user submits something in the last rerun, you can access the user's submission with key or attribute notation from the dict-like object. This is shown in Example 3 below. The text attribute holds a string, which is the user's message. This is an empty string if the user only submitted one or more files. The files attribute holds a list of UploadedFile objects. The list is empty if the user only submitted a message. Unlike st.file_uploader, this attribute always returns a list, even when the widget is configured to accept only one file at a time. The UploadedFile class is a subclass of BytesIO, and therefore is "file-like". This means you can pass an instance of it anywhere a file is expected. |
Examples
Example 1: Pin the the chat input widget to the bottom of your app
When st.chat_input is used in the main body of an app, it will be pinned to the bottom of the page.
Example 2: Use the chat input widget inline
The chat input can also be used inline by nesting it inside any layout container (container, columns, tabs, sidebar, etc) or fragment. Create chat interfaces embedded next to other content, or have multiple chatbots!
Example 3: Let users upload files
When you configure your chat input widget to allow file attachments, it will return a dict-like object when the user sends a submission. You can access the user's message through the text attribute of this dictionary. You can access a list of the user's submitted file(s) through the files attribute. Similar to st.session_state, you can use key or attribute notation.
For an overview of the st.chat_input
and st.chat_message
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.