Duplicated steps after message edition and display issue
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
- Chainlit app with auth, persistence (official data layer) and
cl.instrument_openai()enabled. - Send one message.
- Edit the message.
- New conversation, and come back to the thread with the chat history (or f5, just reload the thread).
- 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
Desktop (please complete the following information):
- Chainlit: 2.2.1
- OS: Windows 11
- Browser: Chrome Version 133.0.6943.142
Closed as it doesn't bother me anymore 🙃
I encountered the same problem. @tygourm How did you resolve this bro?
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. 🙂
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]