Unable to open playground page when using add_routes() with FastAPI's APIRouter #152

Open
opened 2026-02-16 00:19:00 -05:00 by yindo · 5 comments
Owner

Originally created by @StreetLamb on GitHub (Mar 30, 2024).

Originally assigned to: @eyurtsev on GitHub.

I'm using APIRouter to separate my routes like so:

# app/main.py
app.include_router(api_router, prefix='/api/v1')

# app/api/main.py
from app.api.routes import chat

api_router = APIRouter()
api_router.include_router(chat.router, prefix="/chat", tags=["chats"])

# app/api/routes/chat.py
router = APIRouter()
add_routes(
    router,
    ChatOpenAI(),
)

I am able to access http://localhost /api/v1/chat/invoke properly. However http://localhost/api/v1/chat/playground brings me to a blank page. I get the error: Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.

On inspecting network, issue seems to be that the page is incorrectly fetching the playground assets from the base URL http://localhost/playground/assets/index-dbc96538.js instead of http://localhost/api/v1/chat/playground/assets/index-dbc96538.js.

My workaround for now is to move all the prefixes into add_routes instead:

# app/main.py
app.include_router(api_router)

# app/api/main.py
from app.api.routes import chat

api_router = APIRouter()
api_router.include_router(chat.router, tags=["chats"])

# app/api/routes/chat.py
router = APIRouter()
add_routes(
    router,
    ChatOpenAI(),
    path="/api/v1/chat"
)

Ideally, it should be able to infer the correct path when working with prefixes.

Originally created by @StreetLamb on GitHub (Mar 30, 2024). Originally assigned to: @eyurtsev on GitHub. I'm using APIRouter to separate my routes like so: ```python # app/main.py app.include_router(api_router, prefix='/api/v1') # app/api/main.py from app.api.routes import chat api_router = APIRouter() api_router.include_router(chat.router, prefix="/chat", tags=["chats"]) # app/api/routes/chat.py router = APIRouter() add_routes( router, ChatOpenAI(), ) ``` I am able to access `http://localhost /api/v1/chat/invoke` properly. However `http://localhost/api/v1/chat/playground` brings me to a blank page. I get the error: `Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.` On inspecting network, issue seems to be that the page is incorrectly fetching the playground assets from the base URL `http://localhost/playground/assets/index-dbc96538.js` instead of `http://localhost/api/v1/chat/playground/assets/index-dbc96538.js`. My workaround for now is to move all the prefixes into add_routes instead: ```python # app/main.py app.include_router(api_router) # app/api/main.py from app.api.routes import chat api_router = APIRouter() api_router.include_router(chat.router, tags=["chats"]) # app/api/routes/chat.py router = APIRouter() add_routes( router, ChatOpenAI(), path="/api/v1/chat" ) ``` Ideally, it should be able to infer the correct path when working with prefixes.
Author
Owner

@eyurtsev commented on GitHub (Mar 30, 2024):

Thanks! Will take a look on Monday. Which version of langserve are you on?

@eyurtsev commented on GitHub (Mar 30, 2024): Thanks! Will take a look on Monday. Which version of langserve are you on?
Author
Owner

@StreetLamb commented on GitHub (Mar 30, 2024):

Thanks for looking into it! I'm using 0.0.51

@StreetLamb commented on GitHub (Mar 30, 2024): Thanks for looking into it! I'm using 0.0.51
Author
Owner

@eyurtsev commented on GitHub (Mar 30, 2024):

@StreetLamb I am unable to reproduce, would you be kind enough to help me create an MRE with the unit tests?

https://github.com/langchain-ai/langserve/pull/579/files#diff-0796d94e9487fc7c014344ad4a1150ba02c980991a7433ad811f3fef23d843dfR63

@eyurtsev commented on GitHub (Mar 30, 2024): @StreetLamb I am unable to reproduce, would you be kind enough to help me create an MRE with the unit tests? https://github.com/langchain-ai/langserve/pull/579/files#diff-0796d94e9487fc7c014344ad4a1150ba02c980991a7433ad811f3fef23d843dfR63
Author
Owner

@StreetLamb commented on GitHub (Apr 1, 2024):

Hi @eyurtsev, raised a PR to update the playground unit tests as requested.

@StreetLamb commented on GitHub (Apr 1, 2024): Hi @eyurtsev, raised a PR to update the playground unit tests as requested.
Author
Owner

@SwenRanj commented on GitHub (May 14, 2024):

Hi, I am having the same issue on langserve version 0.1.1.
I noticed that the playground works when you supply a path to the add_routes function, but not when you use the prefix in include_router (like the sample that @StreetLamb provided.

Any news on this issue?

@SwenRanj commented on GitHub (May 14, 2024): Hi, I am having the same issue on langserve version 0.1.1. I noticed that the playground works when you supply a `path` to the `add_routes` function, but not when you use the `prefix` in `include_router` (like the sample that @StreetLamb provided. Any news on this issue?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langserve#152