fix: preserve line breaks in received messages #2372
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
So after careful investigation on #2367 and #2264, I tested as many scenarios as I could and concluded the following:
Line breaks in received messages (both user and AI) are not preserved properly, and this PR fixes that
There will still be cases of "improper" or "unexpected" rendering - for example, multiple consecutive new lines like this:
which will be rendered into this:
or code like this:
will be rendered into this:

This happens because Chainlit treats both user and AI messages as Markdown, and changing this behavior would not only be a breaking change, but also potentially undesirable, as LLMs will interpret such text as Markdown too and we can''t distinguish cases where it should be treated as such and where it shouldn't. Any future issues caused by rendering user messages as Markdown should be closed with the resolution
won't fix
, and this behavior should be preserved at least until (if) #2357 is implemented.If users desire to preserve formatting, they may use the ``` block:
will be rendered as
