Skip to content

Commit

Permalink
Fix bug for adding routes to APIRouter (#130)
Browse files Browse the repository at this point in the history
-- Disables the startup logs and the check for re-used paths for API
routers.
-- API Router is unhashable, so we'll disable some non-essential
features for API routers
  • Loading branch information
eyurtsev authored Oct 30, 2023
1 parent a94b666 commit cb3de13
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
5 changes: 4 additions & 1 deletion langserve/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
20 changes: 19 additions & 1 deletion tests/unit_tests/test_server_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

0 comments on commit cb3de13

Please sign in to comment.