Sub topic 4
Logging a Model with a Signature
This chapter assumes that you know MLflow Tracking concepts such as experiments, runs, metrics, and artifacts. You also need basic Python, a virtual environment, and a working MLflow tracking server at http://127.0.0.1:5000.
A logged model is a model artifact saved inside an MLflow run. Logging preserves the model and its files, but it does not yet make the model a named, versioned asset in the Model Registry. The registry step comes later.
Why a model signature matters
A model signature describes the inputs, outputs, and optional parameters that a model expects. For example, an Iris classifier might expect four numeric columns and return a numeric class prediction.
Without a signature, a model can be difficult to use safely. A serving application might send columns in the wrong order, use strings instead of numbers, or omit a required feature. A signature documents the model's interface and can help MLflow validate requests.
The signature is metadata attached to the logged model. It does not replace business validation or guarantee that every bad input will be rejected.
| Model part | Example | Purpose |
|---|---|---|
| Input schema | Four floating-point features | Describes data accepted by the model |
| Output schema | Integer predictions | Describes values returned by the model |
| Model artifact | Serialized scikit-learn model | Stores the trained model for later loading |
Infer and log the signature
The safest common pattern is to infer the signature from representative input data and the model's actual output. The following example trains a classifier, logs it to a run, and registers it at the same time.
import mlflow
from mlflow.models import infer_signature
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
mlflow.set_tracking_uri("http://127.0.0.1:5000")
mlflow.set_experiment("iris-classifier")
X, y = load_iris(return_X_y=True)
model = LogisticRegression(max_iter=200).fit(X, y)
predictions = model.predict(X)
signature = infer_signature(X, predictions)
with mlflow.start_run() as run:
mlflow.sklearn.log_model(
model,
artifact_path="model",
signature=signature,
registered_model_name="iris-classifier",
)
print(f"Run ID: {run.info.run_id}")X contains representative input rows. predictions contains the output produced by the fitted model. infer_signature uses both values to create the input and output schemas.
The registered_model_name argument connects the logged artifact to a registered model named iris-classifier. If that registered model does not exist, MLflow creates it. Each later logging operation with the same name creates another model version.
Understand the relationship
The run remains the source of the logged artifact. The registry stores a reference to that artifact and adds model-level metadata, such as a version number, description, tags, and aliases.
A useful signature should represent the data your production code will send. Training data is often suitable, but a small representative sample can be better when the training dataset is large. Include all expected columns and preserve their types.
Check the logged model
Open the MLflow user interface at http://127.0.0.1:5000. Select the iris-classifier experiment and inspect the run. The run should contain a model artifact. The registered model should contain a new version whose source points to that run's artifact.
A signature describes an interface, not model quality. Record evaluation metrics separately, then use those results when deciding whether a version is ready for promotion.
Exercise
Why should the signature be inferred from both X and the model's predictions?
Show answer
X describes the input schema, while the predictions describe the model's output schema. Using both records the complete model interface.