Chainlit / chainlit

Build Conversational AI in minutes ⚡️
https://docs.chainlit.io
Apache License 2.0
7.06k stars 926 forks source link

Tasklist not supported when mounting Chainlit as a fastAPI subapp #1400

Open tioans opened 3 weeks ago

tioans commented 3 weeks ago

Describe the bug When the Chainlit app (v1.2.0) is mounted as a FastAPI sub-app using mount_chainlit, a TaskList element fails to load, displaying "An error occurred" in the top-right corner where the task list should be. However, the Chainlit app works correctly when run directly without mounting to FastAPI.

To Reproduce Steps to reproduce the behavior:

  1. Create a Chainlit app with the Tasklist example provided in the Chainlit documentation:
import chainlit as cl

@cl.on_chat_start
async def main():
    # Create the TaskList
    task_list = cl.TaskList()
    task_list.status = "Running..."

    # Create a task and put it in the running state
    task1 = cl.Task(title="Processing data", status=cl.TaskStatus.RUNNING)
    await task_list.add_task(task1)
    # Create another task that is in the ready state
    task2 = cl.Task(title="Performing calculations")
    await task_list.add_task(task2)

    # Optional: link a message to each task to allow task navigation in the chat history
    message = await cl.Message(content="Started processing data").send()
    task1.forId = message.id

    # Update the task list in the interface
    await task_list.send()

    # Perform some action on your end
    await cl.sleep(1)

    # Update the task statuses
    task1.status = cl.TaskStatus.DONE
    task2.status = cl.TaskStatus.FAILED
    task_list.status = "Failed"
    await task_list.send()
  1. Mount the Chainlit app to a FastAPI app using mount_chainlit:
from fastapi import FastAPI
from chainlit.utils import mount_chainlit

app = FastAPI()

@app.get("/app")
def read_main():
    return {"message": "Hello World from main app"}

mount_chainlit(app=app, target="test_tasklist.py", path="/chainlit")
  1. Run the FastAPI app with Uvicorn.
uvicorn test_mount:app --host 0.0.0.0 --port 80

Expected behavior The TaskList element should load and display the tasks as expected, as it does when the Chainlit app is run directly without mounting to FastAPI.

Screenshots image

Desktop (please complete the following information):

dosubot[bot] commented 3 weeks ago

Hey there, @tioans! I'm here to help you with any bugs, questions, or contributions you have. Let's tackle this issue together!

The issue you're encountering with the TaskList element not loading correctly when the Chainlit app is mounted as a FastAPI sub-app might be related to how the Chainlit app handles routing and requests in this setup. When Chainlit is mounted as a sub-app, certain configurations or environment variables might not be set correctly, affecting the functionality of specific elements like TaskList.

Here are a few things you can check or try:

  1. Environment Variables: Ensure that the necessary environment variables are set correctly. The mount_chainlit function sets CHAINLIT_ROOT_PATH and CHAINLIT_SUBMOUNT environment variables, which are crucial for the app to function properly as a sub-app [1].

  2. Debugging: Enable debugging by setting the CHAINLIT_DEBUG environment variable to True. This might provide more detailed error messages that can help identify the issue.

  3. Check for Errors in Console: Look for any error messages in the console where you are running the FastAPI app. These messages might give more insight into what is going wrong.

  4. Update Chainlit: Ensure you are using the latest version of Chainlit, as updates might include bug fixes related to FastAPI integration.

  5. Review TaskList Implementation: The TaskList implementation involves asynchronous operations and JSON serialization. Ensure that these operations are not being disrupted by the FastAPI integration [2].

If these steps do not resolve the issue, it might be necessary to delve deeper into the integration specifics or consider reaching out to the maintainers for more detailed support.

To continue talking to Dosu, mention @dosu.

dokterbob commented 2 weeks ago

@tioans Thanks so much for pointing this out! @willydouhard has created a PR attempting to fix this.

We'd greatly appreciate if you're perhaps able to confirm whether indeed his patch resolves your issue. Just the little extra testing that helps us ensure quality. :)