-
Notifications
You must be signed in to change notification settings - Fork 117
feat: add health and readiness check endpoints to CLI serve API #1100
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -7,7 +7,7 @@ | |||||
| import sys | ||||||
| import time | ||||||
| import uuid | ||||||
| from typing import Any, Literal, cast | ||||||
| from typing import Any, cast | ||||||
|
|
||||||
| try: | ||||||
| import typer | ||||||
|
|
@@ -52,6 +52,18 @@ | |||||
| ) | ||||||
|
|
||||||
|
|
||||||
| @app.get("/health", status_code=200) | ||||||
| async def health_check(): | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing return type —
Suggested change
|
||||||
| """Basic liveness check endpoint. | ||||||
|
|
||||||
| Returns a 200 OK status to signal that the Python process is alive and responding. | ||||||
|
|
||||||
| Returns: | ||||||
| dict: A dictionary with status "pass". | ||||||
| """ | ||||||
| return {"status": "pass"} | ||||||
|
|
||||||
|
|
||||||
| @app.exception_handler(RequestValidationError) | ||||||
| async def validation_exception_handler( | ||||||
| request: Request, exc: RequestValidationError | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -10,7 +10,7 @@ | |||||||
| from fastapi.testclient import TestClient | ||||||||
| from pydantic import BaseModel, ValidationError | ||||||||
|
|
||||||||
| from cli.serve.app import make_chat_endpoint, validation_exception_handler | ||||||||
| from cli.serve.app import app, make_chat_endpoint, validation_exception_handler | ||||||||
| from cli.serve.models import ( | ||||||||
| ChatCompletion, | ||||||||
| ChatCompletionRequest, | ||||||||
|
|
@@ -45,6 +45,18 @@ def sample_request(): | |||||||
| ) | ||||||||
|
|
||||||||
|
|
||||||||
| class TestHealthCheckEndpoint: | ||||||||
| """Tests for the health check endpoint.""" | ||||||||
|
|
||||||||
| def test_health_check(self): | ||||||||
| """Test that /health endpoint returns 200 with correct JSON response.""" | ||||||||
| client = TestClient(app) | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: the other test classes here use fixtures or class-level setup rather than constructing the client inline, but this is totally fine for a single test. Just worth a comment so the next person adding tests knows this is intentional:
Suggested change
|
||||||||
| response = client.get("/health") | ||||||||
|
|
||||||||
| assert response.status_code == 200 | ||||||||
| assert response.json() == {"status": "pass"} | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: one edge case worth pinning while you're here — what does def test_health_check_rejects_post(self):
client = TestClient(app)
assert client.post("/health").status_code == 405 |
||||||||
|
|
||||||||
|
|
||||||||
| class TestChatEndpoint: | ||||||||
| """Tests for the chat completion endpoint.""" | ||||||||
|
|
||||||||
|
|
@@ -722,8 +734,6 @@ async def test_text_format_no_schema(self, mock_module): | |||||||
| @pytest.mark.asyncio | ||||||||
| async def test_json_schema_missing_schema_field(self, mock_module): | ||||||||
| """Test that json_schema without schema field raises ValidationError.""" | ||||||||
| from pydantic import ValidationError | ||||||||
|
|
||||||||
| # Should raise ValidationError when creating ResponseFormat | ||||||||
| with pytest.raises(ValidationError) as exc_info: | ||||||||
| ResponseFormat( | ||||||||
|
|
||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI:
status_code=200is FastAPI's default for GET so it's a no-op — the code works correctly either way. Just flagging in case you want to trim it: