Skip to main content

A simple framework for creating ML endpoints

Project description

JLServe

A simple Python framework for creating ML inference endpoints with minimal boilerplate.

Features

  • Simple API - Decorator-based pattern for defining apps and endpoints
  • Type-safe - Pydantic models for input/output validation
  • Developer-friendly - IDE autocomplete, typo detection, auto-generated docs
  • Fast startup - Server ready in under 2 seconds

Installation

pip install jlserve

Or with uv:

uv add jlserve

Quick Start

Create an app in app.py:

import jlserve
from pydantic import BaseModel


class Input(BaseModel):
    name: str


class Output(BaseModel):
    message: str


@jlserve.app()
class Greeter:
    def setup(self):
        self.prefix = "Hello"

    @jlserve.endpoint()
    def greet(self, input: Input) -> Output:
        return Output(message=f"{self.prefix}, {input.name}!")

Run the server:

jlserve dev app.py

Output:

Serving Greeter at http://localhost:8000
Docs at http://localhost:8000/docs

Endpoints: POST /greet

Test the endpoint:

curl -X POST http://localhost:8000/greet \
  -H "Content-Type: application/json" \
  -d '{"name": "World"}'

Response:

{"message": "Hello, World!"}

API Reference

@jlserve.app()

Decorator that marks a class as a JLServe app. Only one app per module/deployment.

Parameter Type Required Description
name str No Custom name for the app. Defaults to class name.
requirements list[str] No Python dependencies to auto-install (e.g., ["torch", "transformers"])

@jlserve.endpoint()

Decorator that marks a method as an endpoint within the app class.

Parameter Type Required Description
path str No Custom route path. Defaults to "/" + method name.

Class Methods

Method Required Description
setup(self) No Called once when server starts. Use for loading models, initializing resources.
<endpoint_method>(self, input) -> output Yes Endpoint methods decorated with @jlserve.endpoint(). Must have type hints for input and output.

Input/Output Requirements

  • Input must be a Pydantic BaseModel subclass
  • Output must be a Pydantic BaseModel subclass
  • Type hints are required on endpoint methods

CLI Reference

jlserve dev <file>

Runs the app locally for development.

Option Default Description
--port, -p 8000 Port to serve on

Example:

jlserve dev app.py --port 3000

Auto-Generated Features

Feature Description
OpenAPI docs Available at /docs
Request validation Invalid input returns 422 with details
JSON serialization Automatic based on Pydantic models

Error Handling

Error Behavior
No @jlserve.app() class Error at startup: "No app found. Did you decorate a class with @jlserve.app()?"
No endpoint methods Error at startup: "App has no endpoints. Add methods decorated with @jlserve.endpoint()."
Missing type hints on endpoint methods Error at startup: Validation error with details
Invalid input JSON 422 response with validation errors
Exception in endpoint method 500 response with error message
Exception in setup() Server fails to start with error message

Example: ML Inference

import jlserve
from pydantic import BaseModel


class SentimentInput(BaseModel):
    text: str


class SentimentOutput(BaseModel):
    label: str
    score: float


@jlserve.app(requirements=["transformers"])
class SentimentAnalyzer:
    def setup(self):
        from transformers import pipeline
        self.pipe = pipeline("sentiment-analysis")

    @jlserve.endpoint()
    def analyze(self, input: SentimentInput) -> SentimentOutput:
        result = self.pipe(input.text)[0]
        return SentimentOutput(
            label=result["label"],
            score=result["score"]
        )

Development

Run tests:

uv run pytest

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jlserve-0.1.0.tar.gz (54.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jlserve-0.1.0-py3-none-any.whl (22.3 kB view details)

Uploaded Python 3

File details

Details for the file jlserve-0.1.0.tar.gz.

File metadata

  • Download URL: jlserve-0.1.0.tar.gz
  • Upload date:
  • Size: 54.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.14

File hashes

Hashes for jlserve-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f202412449a696d26c884a9db2370f3e1551ac5d797718f7d3d5a124e4547e5c
MD5 3de0aa1dff41db2121a83b5572931000
BLAKE2b-256 f3b0fd8314718823854c26c6d75b8cf6e19f893ce70f32cc89ed1171cf76e348

See more details on using hashes here.

File details

Details for the file jlserve-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: jlserve-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 22.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.14

File hashes

Hashes for jlserve-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8f66e28c8bd0c942e924e3a3be2b86e878a79004f2ad77a5c300991c05ac3410
MD5 0645fc80182f639d5e10e33e27933360
BLAKE2b-256 d38e944d36c0fd0cce7752870aee4faa8a668a0b7bb831850f57dff12ecb3747

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page