Skip to content

Swagger UI does not expose request parameters for /predict, causing 500 error when invoked #667

@mageshkrishna

Description

@mageshkrishna

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

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.

{
  "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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions