Skip to content

fix: preserve line breaks in received messages #2372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 6, 2025

Conversation

asvishnyakov
Copy link
Contributor

@asvishnyakov asvishnyakov commented Aug 6, 2025

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:

    print("Line 1")
    
    print("Line 3")
    
    
    print("Line 6")
    

    which will be rendered into this:

    print("Line 1")
    
    print("Line 3")
    
    print("Line 6")
    

    or code like this:

    import os
    
    import streamlit as st
    
    from app_env import configure_logger
    
    configure_logger(os.path.basename(__file__))
    
    # This file contains the menu for the app. It is used to navigate between pages and to show the documentation link.
    
    # The default page to show when logged in
    DEFAULT_PAGE = "pages/example.py"
    
    
    def authenticated_menu():
        # Show a navigation menu for authenticated users
        with st.sidebar:
            st.sidebar.page_link("pages/example.py", label="Example Link", icon="🚀")

    will be rendered into this:
    image

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:

```python
import os

import streamlit as st

from app_env import configure_logger

configure_logger(os.path.basename(__file__))

# This file contains the menu for the app. It is used to navigate between pages and to show the documentation link.

# The default page to show when logged in
DEFAULT_PAGE = "pages/example.py"


def authenticated_menu():
    # Show a navigation menu for authenticated users
    with st.sidebar:
        st.sidebar.page_link("pages/example.py", label="Example Link", icon="🚀")
```

will be rendered as
image

@asvishnyakov asvishnyakov requested a review from hayescode August 6, 2025 22:55
@dosubot dosubot bot added size:XS This PR changes 0-9 lines, ignoring generated files. frontend Pertains to the frontend. labels Aug 6, 2025
@asvishnyakov asvishnyakov added this pull request to the merge queue Aug 6, 2025
Merged via the queue into main with commit 63a0b20 Aug 6, 2025
9 checks passed
@asvishnyakov asvishnyakov deleted the fix/line-breaks-in-received-messages branch August 6, 2025 23:19
asvishnyakov added a commit that referenced this pull request Aug 8, 2025
github-merge-queue bot pushed a commit that referenced this pull request Aug 8, 2025
This reverts commit 63a0b20.

This PR reverts a part of solution for #2367 and closes #2375.

Ignoring of some line breaks and removal of leading whitespaces [are
part of Markdown](https://github.com/orgs/remarkjs/discussions/1194).

User messages rendered as Markdown by both Chainlit and all popular LLM
chats.

All future issues about that should be closed with resolution `won't
fix`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
frontend Pertains to the frontend. size:XS This PR changes 0-9 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants