diff --git a/langserve/server.py b/langserve/server.py index 29d146ca..05f8bcb8 100644 --- a/langserve/server.py +++ b/langserve/server.py @@ -327,7 +327,10 @@ def add_routes( "Use `pip install sse_starlette` to install." ) - _register_path_for_app(app, path) + if isinstance(app, FastAPI): # type: ignore + # Cannot do this checking logic for a router since + # API routers are not hashable + _register_path_for_app(app, path) well_known_lc_serializer = WellKnownLCSerializer() if hasattr(app, "openapi_tags") and app not in _APP_SEEN: diff --git a/tests/unit_tests/test_server_client.py b/tests/unit_tests/test_server_client.py index 1272d044..6d23f5d7 100644 --- a/tests/unit_tests/test_server_client.py +++ b/tests/unit_tests/test_server_client.py @@ -8,7 +8,7 @@ import httpx import pytest import pytest_asyncio -from fastapi import FastAPI +from fastapi import APIRouter, FastAPI from fastapi.testclient import TestClient from httpx import AsyncClient from langchain.callbacks.tracers.log_stream import RunLogPatch @@ -1221,3 +1221,21 @@ def func2(baz) -> int: app, path="/foo", raise_app_exceptions=False ) as runnable: assert await runnable.ainvoke({"bar": 1}) == 1 + + +@pytest.mark.asyncio +async def test_using_router() -> None: + """Test using a router.""" + app = FastAPI() + + # Make sure that we can add routers + # to an API router + router = APIRouter() + + add_routes( + router, + RunnableLambda(lambda foo: "hello"), + path="/chat", + ) + + app.include_router(router)