hsml.default_predictor #
DefaultPredict #
Predictor for deployments whose requests are described by a deployment schema.
Validates each request against the schema, looks up and transforms the feature vector through the feature view, runs the model when the deployment has one, and logs the request when the feature view has logging enabled. Without a model it returns the transformed vectors. Subclass it and override load_model or model_predict for models the default loader cannot handle.
Example
# predictor.py, deployed with model.deploy(script_file="predictor.py",
# default_predictor=True) so the schema is still inferred
from hsml.default_predictor import DefaultPredict
class Predict(DefaultPredict):
def load_model(self, model_files_path):
return my_framework.load(model_files_path)
def model_predict(self, feature_vectors):
return self.model.predict_proba(
feature_vectors[self.model_input_columns]
)
check_model_input_contract #
check_model_input_contract() -> None
Fail unless the model's input schema is served by the transformed feature view schema.
Columnar inputs must all exist among the transformed columns with a compatible type family; a tensor input must be a single tensor whose last dimension is the number of transformed columns. Nothing is guessed: a missing or incompatible input schema raises.
| RAISES | DESCRIPTION |
|---|---|
hopsworks.client.exceptions.ModelServingException | On any mismatch, naming the columns or shapes involved. |
close #
fetch_feature_vectors #
Look up and transform the feature vectors for rows.
| PARAMETER | DESCRIPTION |
|---|---|
rows | Validated rows as returned by |
| RETURNS | DESCRIPTION |
|---|---|
Any | A pandas DataFrame in the transformed schema order, one row per input row. |
| RAISES | DESCRIPTION |
|---|---|
fastapi.HTTPException | 404 when an entity is missing, 422 when a transformation fails, 503 when the feature store is unreachable. |
load_model #
Load the single pickle or joblib file under model_files_path.
| PARAMETER | DESCRIPTION |
|---|---|
model_files_path | Directory holding the model files ( TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
Any | The loaded model object. |
| RAISES | DESCRIPTION |
|---|---|
hopsworks.client.exceptions.ModelServingException | If there is not exactly one candidate file; override this method or pass your own script then. |
log #
log(
rows: list[dict[str, Any]],
feature_vectors: Any,
predictions: Any,
request_id: str | None = None,
) -> None
Queue the request for logging through the feature view when logging is enabled.
The logging frame is built and handed to the wrapper's async logger on a background thread, so this returns as soon as the request is queued. The backlog holds at most FEATURE_LOGGER_QUEUE_SIZE rows, or one request's batch when that alone is larger; beyond that the request's rows are dropped and counted, and a failure on the worker is counted and logged and never fails a prediction.
| PARAMETER | DESCRIPTION |
|---|---|
rows | Validated rows as returned by |
feature_vectors | The DataFrame returned by TYPE: |
predictions | The model output, ignored without a model. TYPE: |
request_id | Correlation id stored with every logged row. TYPE: |
model_predict #
Run the model on the transformed feature vectors, or return them unchanged when there is no model.
| PARAMETER | DESCRIPTION |
|---|---|
feature_vectors | The DataFrame returned by TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
Any | The model's predictions as a list, or |
| RAISES | DESCRIPTION |
|---|---|
fastapi.HTTPException | 500 when the model raises. |
predict #
Serve one request: validate, look up, predict or return the vectors, log.
| PARAMETER | DESCRIPTION |
|---|---|
inputs | The request rows, as objects keyed by field name or arrays in schema order. TYPE: |
request_id | Correlation id from the TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
Any | The predictions list with a model, else |
prepare_rows #
Validate the request rows against the schema.
| PARAMETER | DESCRIPTION |
|---|---|
inputs | The request rows, as objects keyed by field name or arrays in schema order. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
list[dict[str, Any]] | The rows as dicts keyed by field name. |
| RAISES | DESCRIPTION |
|---|---|
fastapi.HTTPException | 400 with the structured errors on a mismatch, 413 on an oversize batch. |
PredictionError #
Bases: ModelServingException
A request could not be served; carries the HTTP status and the structured error detail.
run_kserve_wrapper #
run_kserve_wrapper() -> None
Replace this process with the KServe wrapper that serves the Predict class of SCRIPT_PATH.
Deployments without a model artifact are started as python <script>; calling this from the script's __main__ block gives them the same server, payload handling, and feature logger as model deployments.