chainlit icon indicating copy to clipboard operation
chainlit copied to clipboard

Duplicated steps after message edition and display issue

Open ghost opened this issue 1 year ago • 4 comments

Describe the bug

I use cl.instrument_openai() to display calls to my llm. When I edit a message and then come back to the thread with te history, it displays the step of the message before and after edition. If I edit the message again, three are displayed and so on ...

To Reproduce

  1. Chainlit app with auth, persistence (official data layer) and cl.instrument_openai() enabled.
  2. Send one message.
  3. Edit the message.
  4. New conversation, and come back to the thread with the chat history (or f5, just reload the thread).
  5. The step of the message before edition is still displayed, and renders badly on the UI.

Expected behavior

I expect that the step of the message before edition to not be displayed, or, if it not a bug but a feature, it should at least display well in the UI.

Screenshots

Image

Image

Desktop (please complete the following information):

  • Chainlit: 2.2.1
  • OS: Windows 11
  • Browser: Chrome Version 133.0.6943.142

ghost avatar Feb 27 '25 17:02 ghost

Closed as it doesn't bother me anymore 🙃

ghost avatar Apr 03 '25 16:04 ghost

I encountered the same problem. @tygourm How did you resolve this bro?

w3-3w avatar May 15 '25 01:05 w3-3w

The duplicated steps and bad rendering of the output are actually two different issues.

For the bad rendering, I used with cl.Step instead of cl.instrument_openai (https://docs.chainlit.io/api-reference/step-class), with the messages sent to the LLM as input and no output, since the output is just the message itself.

As for the duplicated steps, I haven’t found a fix yet. It really bothered me when I first noticed it, but I don’t edit my messages very often, so it hasn’t been a major issue for me.

I was using an older version of Chainlit (2.2.1), so hopefully this has been improved in more recent versions. 🙂

ghost avatar May 15 '25 05:05 ghost

I'm using 2.4.0 and the issue persists.

I end up with dirty fix: delete steps without messages on chat resume.

@cl.on_chat_resume
async def on_chat_resume(thread: ThreadDict | None):
    if thread is None:
        return
    indexes_of_system_steps_without_message: set[int] = set()
    steps = thread["steps"]
    for i, step in enumerate(steps):
        if step["type"] == "run":
            j = i + 1
            has_message = False
            while j < len(steps) and steps[j]["parentId"] == step["id"]:
                if "message" in steps[j]["type"]:
                    has_message = True
                    break
                j += 1
            if not has_message:
                indexes_of_system_steps_without_message.update(range(i, j))
    await asyncio.gather(
        *[data_layer.delete_step(steps[idx]["id"]) for idx in indexes_of_system_steps_without_message]
    )
    for i in sorted(indexes_of_system_steps_without_message, reverse=True):
        del steps[i]

w3-3w avatar May 15 '25 11:05 w3-3w