Lightning-AI / Lightning-AI/LitServe
Swagger UI does not expose request parameters for /predict, causing 500 error when invoked
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 3.9k
- Forks
- 304
- Avg merge
- 3d 13h
- Merged PRs (30d)
- 6
Description
The `/predict` endpoint in the example servers does not define a request body schema. Consequently, Swagger UI provides no input form and sends an empty request body by default when "Execute" is clicked. This results in a 500 Internal Server Error as the server fails to parse the empty JSON payload.
## To Reproduce
### 1. Start the server
```bash
python tests/simple_server_diff_port.py
```
### 2. Access Swagger UI
Navigate to:
```
http://localhost:8000/docs
```
### 3. Trigger the Error
- Expand the `/predict` endpoint
- Click **Execute**
**Note:** No input field is visible to the user.
## Actual Results
The server crashes with a JSONDecodeError because Swagger sends an empty body:
```
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
...
File "src/litserve/server.py", line X, in predict
return await request.json()
```
## Expected Behavior
Swagger UI should display a request body form (e.g., a JSON blob like below). This allows users to provide input values directly in the interface, ensuring the `request.json()` call has valid data to parse.
```json
{
"input": 5
}
```
## Suggested Fix: Implement Pydantic Schemas
To resolve this, define a Pydantic model for the request payload. This allows the underlying FastAPI engine to generate the proper OpenAPI documentation for Swagger.
### Proposed change
```python
from pydantic import BaseModel
from typing import Any
# Define the expected schema
class PredictRequest(BaseModel):
input: Any
# Update the endpoint logic to reference this schema
# so Swagger generates the following interactive form:
# {
# "input": "value"
# }
```
## Environment
- OS: Linux
- Python: 3.12
- Installation: pip
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with tests/simple_server_diff_port.py and inspect how the /predict endpoint is declared and how its request body is parsed. Run the server, open /docs, and verify that Swagger UI exposes a JSON input form for /predict. Done means a request such as {"input": 5} can be submitted without the empty-body JSONDecodeError or a 500 response.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- fastapi, python
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 70/100