Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate that endpoints are passed as a sequence #297

Merged
merged 3 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ Enable: The code below will only enable `invoke`, `batch` and the corresponding


```python
add_routes(app, chain, enabled_endpoints=("invoke", "batch", "config_hashes"))
add_routes(app, chain, enabled_endpoints=["invoke", "batch", "config_hashes"])
```

Disable: The code below will disable the playground for the chain
Expand Down
12 changes: 12 additions & 0 deletions langserve/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,18 @@ def __init__(
f"{disabled_endpoints}."
)

if enabled_endpoints and not isinstance(enabled_endpoints, Sequence):
raise ValueError(
f"Expected enabled_endpoints to be a sequence (e.g., list or tuple), "
f"got {type(enabled_endpoints)}"
)

if disabled_endpoints and not isinstance(disabled_endpoints, Sequence):
raise ValueError(
f"Expected disabled_endpoints to be a sequence (e.g., list or tuple), "
f"got {type(disabled_endpoints)}"
)

if enabled_endpoints is None:
if disabled_endpoints is None:
is_invoke_enabled = True
Expand Down
33 changes: 31 additions & 2 deletions tests/unit_tests/test_server_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1781,8 +1781,8 @@ async def check_types(inputs: VariousTypes) -> int:
)


async def test_all_endpoints_off() -> None:
"""Test toggling endpoints."""
async def test_endpoint_configurations() -> None:
"""Test enabling/disabling endpoints."""
app = FastAPI()

# All endpoints disabled
Expand Down Expand Up @@ -1866,3 +1866,32 @@ async def test_all_endpoints_off() -> None:
method, "/config_off" + endpoint, json=payload
)
assert response.status_code != 404, f"endpoint {endpoint} should be on"

with pytest.raises(ValueError):
# Passing "invoke" instead of ["invoke"]
add_routes(
app,
RunnableLambda(lambda foo: "hello"),
disabled_endpoints="invoke", # type: ignore
enable_feedback_endpoint=True,
path="/config_off",
)
with pytest.raises(ValueError):
# meow is not an endpoint.
add_routes(
app,
RunnableLambda(lambda foo: "hello"),
disabled_endpoints=["meow"], # type: ignore
enable_feedback_endpoint=True,
path="/config_off",
)

with pytest.raises(ValueError):
# meow is not an endpoint.
add_routes(
app,
RunnableLambda(lambda foo: "hello"),
enabled_endpoints=["meow"], # type: ignore
enable_feedback_endpoint=True,
path="/config_off",
)