chainlit icon indicating copy to clipboard operation
chainlit copied to clipboard

FastAPI endpoints return 404 Not Found error when defined after mount_chainlit function

Open Galsor opened this issue 1 year ago • 1 comments
trafficstars

Describe the bug When using the mount_chainlit function, any FastAPI endpoints defined after this function call do not behave as expected. Although these endpoints appear to be correctly registered and accessible, they return a 404 Not Found error when accessed.

To Reproduce Steps to reproduce the behavior:

1.	Set up a FastAPI application.
2.	Import and use mount_chainlit to mount a Chainlit application.
3.	Define any FastAPI route after the mount_chainlit function call.

Expected behavior Endpoints defined after the mount_chainlit function should be accessible and return the correct response as defined in the route handler.

Desktop (please complete the following information):

•	Python version: 3.12.2
•	FastAPI version: 0.110.3
•	Chainlit version: 1.1.306
•	Operating System: MacOS 14.5

Additional context Code to reproduce: ✅ Working

from chainlit.utils import mount_chainlit
from fastapi import FastAPI

app = FastAPI()

@app.get("/health")
async def health_check():
    return "ok"

mount_chainlit(app, target="<YOUR_APP_FILE>.py", path="/chainlit")

❌ Fail

from chainlit.utils import mount_chainlit
from fastapi import FastAPI

app = FastAPI()

mount_chainlit(app, target="<YOUR_APP_FILE>.py", path="/chainlit")

@app.get("/health")
async def health_check():
    return "ok"

Galsor avatar Jul 26 '24 08:07 Galsor

The same issue occurs while mounting StaticFiles to the client: ✅ Works

from chainlit.utils import mount_chainlit
from fastapi import FastAPI

app = FastAPI()

app.mount("static", StaticFiles(directory="static", html=True))

mount_chainlit(app, target="<YOUR_APP_FILE>.py", path="/chainlit")

❌ Fail

from chainlit.utils import mount_chainlit
from fastapi import FastAPI

app = FastAPI()

mount_chainlit(app, target="<YOUR_APP_FILE>.py", path="/chainlit")

app.mount("static", StaticFiles(directory="static", html=True))

Galsor avatar Jul 26 '24 08:07 Galsor

Thanks for the feedback!

Given the current implementation, this is expected behaviour; mount_chainlit() will 'grab' all of the URL space once it's executed.

Hence it should be called after any other endpoints are set up.

We acknowledge that this is not ideal behaviour and are looking to improve it. Please see #1223 for progress on an improved/alternative approach.

Feel free to help us get towards solving this issue and/or sharing your ideas/suggestions/feedback related to this problem on the relevant issue, I'm closing this for now (but have referred here for reference).

dokterbob avatar Aug 16 '24 11:08 dokterbob