langserve icon indicating copy to clipboard operation
langserve copied to clipboard

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

Open StreetLamb opened this issue 11 months ago • 5 comments

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.

StreetLamb avatar Mar 30 '24 04:03 StreetLamb

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

eyurtsev avatar Mar 30 '24 23:03 eyurtsev

Thanks for looking into it! I'm using 0.0.51

StreetLamb avatar Mar 31 '24 01:03 StreetLamb

@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 avatar Mar 31 '24 01:03 eyurtsev

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

StreetLamb avatar Apr 01 '24 16:04 StreetLamb

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 avatar May 14 '24 15:05 SwenRanj