A Message is a piece of information that is sent from the user to an assistant and vice versa. Coupled with life cycle hooks, they are the building blocks of a chat.

A message has a content, a timestamp and cannot be nested.

Example: Reply to a user message

Lets create a simple assistant that replies to a user message with a greeting.

import chainlit as cl

@cl.on_message
async def on_message(message: cl.Message):
    response = f"Hello, you just sent: {message.content}!"
    await cl.Message(response).send()

Message API

Learn more about the Message API.

Trick: Display a loader while waiting for a response

If a task is running and the latest message content is empty, Chainlit will display a loader while the content remains empty.

Steps support loading out of the box. If you need to display a loader in a Message, chances are you should be using a Step instead!

import chainlit as cl


@cl.on_message
async def on_message(message: cl.Message):
    msg = cl.Message(content="")
    await msg.send()

    # do some work
    await cl.sleep(2)

    msg.content = f"Processed message {message.content}"

    await msg.update()

Loading Message