Skip to content

Sanitize history to prevent chat errors#284

Merged
brichet merged 6 commits intojupyterlite:mainfrom
Yahiewi:history
Mar 13, 2026
Merged

Sanitize history to prevent chat errors#284
brichet merged 6 commits intojupyterlite:mainfrom
Yahiewi:history

Conversation

@Yahiewi
Copy link
Copy Markdown
Contributor

@Yahiewi Yahiewi commented Mar 4, 2026

This PR fixes the second issue mentioned in #271 where clicking on Stop Streaming while a command is being executed would block the chat with Error generating response: No output generated. Check the stream for errors.
The solution is to use a _sanitizeHistory function that drops assistant messages whose tool call ids don't have matching results.
This function uses two new functions _getToolCallIds and _matchesAllToolCalls which are used to get tool call ids for assistant messages, and check if these ids have a result.

Screencast.from.2026-03-06.09-44-38.mp4

As you can see in this screencast, the chat no longer throws an error. However it does immediately retry running the command I asked it to in the first prompt and this is because of how history works and because our agent isn't aware of what it started to generate in its last interrupted response.
I think this is something that we could improve in our history, I plan to open an issue and PR for this.

@Yahiewi
Copy link
Copy Markdown
Contributor Author

Yahiewi commented Mar 6, 2026

After this other PR is merged, we can improve this by making it so when a user "Stops streaming" during a command, the UI responds automatically and the Stop Streaming button is reset to Send Message button before having to wait for the tool to finish running.
This needs the busy state from the other PR to block the user from sending a message while the agent is busy.

@Yahiewi Yahiewi marked this pull request as ready for review March 6, 2026 10:56
@brichet brichet added the bug Something isn't working label Mar 10, 2026
newHistory.push(msg);
} else {
// Message has unmatched tool calls drop it and everything after it
break;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Yahiewi for working on this.

I think that we can be more specific here, to avoid removing the whole history.
After some tests, I found out 2 main reasons that corrupts the history:

  • a tool call initiated but without result. This seems to happen when stopping the streaming while there is pending approval.
  • a tool response just after a user message. This is probably hard to reproduce, but probably when a user send a message during a tool call.

@brichet
Copy link
Copy Markdown
Collaborator

brichet commented Mar 13, 2026

I linted the PR.

It is already a good fix, and avoid blocking the model in some situation.
We can probably merge it and be more specific in a follow up PR.

@brichet brichet merged commit 565b3a0 into jupyterlite:main Mar 13, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants