sk_serve package

Submodules

sk_serve.api module

class sk_serve.api.SimpleAPI(validation_model: type[BaseModel] | None = None)[source]

Bases: object

Simple API class that defines the HTTP endpoints for deploying a fitted scikit-learn pipeline. It can also take a pydantic validation model as input in order to validate the input every time inference is requested.

Registered endpoints:
  • GET / — welcome message with the list of available endpoints.

  • GET /health — liveness probe (200 when a pipeline is loaded, 503 otherwise).

  • GET /metadata — model type, supported methods, feature names, load time.

  • POST /inference — single-record prediction (accepts a JSON object).

  • POST /batch-inference — batch prediction (accepts a JSON list of records).

  • POST /predict-proba — class probabilities, if the pipeline supports it.

  • POST /decision-function — decision scores, if the pipeline supports it.

async batch_inference(request: Request) dict[str, Any][source]

Batch inference endpoint. The body must be a JSON list of records.

Returns:

dict: {"predictions": [...]} with one entry per input record.

async decision_function(request: Request) dict[str, Any][source]

Decision-score endpoint. Returns 501 if the pipeline does not expose decision_function. Body is a JSON list of records, like /batch-inference.

static health(request: Request) dict[str, str][source]

Liveness probe. Returns 200 when the pipeline is loaded, 503 otherwise.

static home() dict[str, str][source]

Message returned when a GET request hits the / endpoint.

async inference(request: Request) dict[str, Any][source]

Single-record inference endpoint. The body must be a JSON object whose keys correspond to the pipeline’s expected features. The deployed pipeline must have a predict method.

Args:

request (Request): FastAPI request carrying the JSON payload.

Returns:

dict: {"prediction": <scalar>}.

static metadata(request: Request) dict[str, Any][source]

Return basic information about the loaded pipeline.

async predict_proba(request: Request) dict[str, Any][source]

Class-probability endpoint. Returns 501 if the pipeline does not expose predict_proba. Body is a JSON list of records, like /batch-inference.

sk_serve.api.check_model_methods(model: Any, method: str) None[source]

Validate that model exposes a callable attribute named method.

Args:

model: A fitted object (typically a scikit-learn estimator or pipeline). method (str): The attribute name to check.

Raises:

AttributeError: If the attribute does not exist. TypeError: If the attribute exists but is not callable.

sk_serve.serve module

sk_serve.serve.serve(simple_api: SimpleAPI, pipeline: Any | None = None) FastAPI[source]

Function that constructs the model API.

Args:

simple_api (SimpleAPI): The SimpleAPI object needed for deployment. pipeline: Optional fitted pipeline to inject directly. When provided, the

MODEL_PATH environment variable is ignored and no file is read. Useful for unit tests and for callers that already have the pipeline in memory.

Returns:

app (FastAPI): The FastAPI application.