chainlit icon indicating copy to clipboard operation
chainlit copied to clipboard

Messages sent during on_chat_resume disappear after on_chat_resume

Open lupingsse opened this issue 3 weeks ago • 4 comments
trafficstars

Describe the bug The chat resume feature seems to have an issue with resuming the thread correctly, most notably messages sent during chat resume disappear immediately afterwards.

When a function decorated using @chainlit.on_chat_resume is called, the following happens:

  1. The welcome view is shown (as if the user started a new chat)
  2. (optional) 2.a. A message is sent using chainlit.Message().send() 2.b. The the chat view (displaying previous messages) is loaded 2.c. The new message appears in chat view 2.d. After on_chat_resume returns, the new message disappears immediately
  3. The chat view is (re)loaded displaying previous messages (not including a message sent during step 2)

We are guessing the issue is, that context.emitter.resume_thread is called after on_chat_resume instead of before, and will post a merge request fixing this issue shortly.

To Reproduce Steps to reproduce the behavior:

  1. Enable authentication and data persistence (we used the example header_auth_callback and the official data layer as shown in chainlit docs).
  2. Run an application that sends a message msg during on_chat_resume (example code down below).
  3. Start a new chat & send a message prev_msg.
  4. Start another new chat.
  5. Switch back to the first chat, triggering on_chat_resume. The welcome view is shown. When msg is sent, the chat view is loaded and previous messages (including prev_msg) and msg are displayed. When on_chat_resume returns, msg disappears.

Example code for reproduction:

import chainlit as cl
from chainlit.types import ThreadDict
from starlette.datastructures import Headers

@cl.on_message
async def on_message(message: cl.Message) -> None:
    await cl.Message("in cl_on_message").send()

@cl.on_chat_resume
async def on_chat_resume(thread: ThreadDict) -> None:
    # on_chat_resume, 'welcome view' is shown (as if user started a new chat) instead of 'chat view' with previous messages
    await cl.sleep(2)
    # sending a new message triggers loading the 'chat view'
    # the new message itself appears in chat immediately
    await cl.Message("in on_chat_resume").send()
    # showcase the new message indeed appears in chat
    await cl.sleep(2)
    # new message disappears
    return

@cl.on_chat_start
async def on_chat_start():
    app_user = cl.user_session.get("user")
    await cl.Message(f"Hello {app_user.identifier}").send()

@cl.header_auth_callback
async def header_auth_callback(headers: Headers) -> cl.User:
    return cl.User(
        identifier="admin", metadata={"role": "admin", "provider": "header"}
    )

if __name__ == "__main__":
    cl.run_chainlit(__file__)

Expected behavior

  • 'chat view' is loaded immediately when a chat is resumed.
  • a message sent during on_chat_resume ('msg' in the reproduction example) does not disappear after on_chat_resume returned.

Desktop (please complete the following information):

  • OS: Ubuntu running inside WSL on Windows
  • Browser: Firefox running on Windows
  • Version: 144.0

lupingsse avatar Oct 29 '25 13:10 lupingsse