Sub topic 7
Logging a model with a signature and input example
This chapter assumes you can train a scikit-learn model, run a notebook on Databricks, and log parameters and metrics inside an MLflow run. Here you will log the trained model itself, with metadata that describes what it accepts and returns.
Why log the model, not just metrics
A metric tells you a run was good. It does not give you something to deploy. Logging a model stores the serialized model as an artifact of the run, along with an MLmodel file, environment files, and optional metadata. Later steps, such as registering the model in Unity Catalog or loading it for batch scoring, start from this logged copy.
Autologging can log a model for you. Calling mlflow.sklearn.log_model yourself gives you control over the signature and the input example.
What a signature and an input example add
A signature is a schema for the model's inputs and outputs: column names and types, or tensor shapes. An input example is a few real rows of input data saved with the model.
| Item | What it records | What it helps with |
|---|---|---|
| Signature | Input and output schema | Rejecting badly shaped requests at prediction time, and documenting the contract |
| Input example | A small sample of valid input | Showing users how to call the model, and testing the model when it is logged |
If you pass an input example without a signature, MLflow can infer a signature from it. Passing both makes the intent explicit.
Logging the model
This code assumes model, X_test, and the train/test split from the previous chapters are still defined in your notebook.
import mlflow
from mlflow.models import infer_signature
preds = model.predict(X_test)
signature = infer_signature(X_test, preds)
with mlflow.start_run(run_name="explicit-model-log") as run:
mlflow.sklearn.log_model(
model,
artifact_path="model",
signature=signature,
input_example=X_test[:5],
)
print(run.info.run_id)infer_signature looks at example inputs and the model's predictions, then builds the schema from them. Here X_test is a NumPy array, so the signature describes a tensor of floating-point numbers with four features per row. If you train on a pandas DataFrame, the signature records column names and types instead.
The artifact_path argument is the folder name inside the run's artifacts. You will use it again when registering the model, in the form runs:/<run_id>/model.
Inspecting what was stored
Open the run in the experiment UI and look at the Artifacts tab. Under model/ you will see the files above. The MLmodel file lists the flavors (for example sklearn and python_function) and the signature. The python_function flavor lets you load any logged model through one generic interface, mlflow.pyfunc.load_model.
Common errors
- Signature built from the wrong data. If you infer the signature from training data that has different columns than what you serve, the schema will reject valid requests. Use data in the same form that callers will send.
- Mismatched types at prediction time. A signature of integers will complain when you send floats that cannot be converted safely. Cast inputs to the logged types, or log the signature from correctly typed data.
- Predicting on the wrong object.
infer_signatureneeds model outputs, not true labels. Passmodel.predict(X_test), noty_test. - Large input examples. Keep the example to a handful of rows. It is stored with the model, and it is only meant to illustrate the format.
- Logging outside a run. Without an active run,
log_modelstarts one for you, which can leave stray runs. Usewith mlflow.start_run()so the model lands in the run you intended.
Checking the result
After logging, you can load the model back from the run and test it:
loaded = mlflow.pyfunc.load_model(f"runs:/{run.info.run_id}/model")
print(loaded.metadata.signature)
print(loaded.predict(X_test[:3]))If the signature prints and the predictions match what model.predict returns, the logged model is ready for registration.
Exercise
You logged a model with infer_signature(X_test, y_test) instead of infer_signature(X_test, preds). Why is this a mistake, and what does the output part of the signature then describe?
Show answer
The output schema should describe what the model returns, which is predictions. Passing y_test makes the signature describe the true labels. These often have the same type, so the error may go unnoticed, but if the labels are encoded differently from the predictions, the signature will misdescribe the model's outputs. Use model.predict(X_test) as the second argument.