Skip to content

UI elements may render out of order due to missing ORDER BY in SQL query #2742

@atulkhairnar

Description

@atulkhairnar

Describe the bug
Elements belonging to a thread may be rendered in the wrong order on the UI. This happens because the SQL query used to fetch elements in SQLAlchemyDataLayer.get_all_user_threads does not contain an ORDER BY clause, which may cause the database to return elements in an arbitrary order. This can cause elements to appear out of place or pages to render unexpectedly.

To Reproduce
Steps to reproduce the behavior:

  1. Create several elements attached to a Step.
  2. Open the thread in the UI.
  3. Expand the Step element if it is collapsed
  4. Observe that elements sometimes are displayed out of sequence.
    Expected behavior
    Elements should be rendered in a deterministic, page-based and/or creation order, matching the order they were added or their intended arrangement.

Additional context
Relevant code snippet (in backend/chainlit/data/sql_alchemy.py):

        elements_query = f"""
            SELECT ...
            FROM elements e
            WHERE e."threadId" IN {thread_ids}
        """

Suggested fix:
Add something like:

ORDER BY e."page" ASC NULLS LAST, e."id" ASC

to ensure deterministic ordering.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingdata layerPertains to data layers.needs-triagestaleIssue has not had recent activity or appears to be solved. Stale issues will be automatically closed

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions