Sub topic 2
Adding a signature and input example
This chapter assumes you can train a scikit-learn model and log it with mlflow.sklearn.log_model. You should also know that Unity Catalog registers models under three-level names. Here you add the two pieces of metadata that Unity Catalog and Model Serving depend on: the signature and the input example.
What a signature is
A model signature is a record of the model's input and output schema, stored with the logged model. It lists each column's name and data type, or a tensor's shape and type for array inputs.
A signature has two main uses:
- Registration. Unity Catalog refuses to register a model version that has no signature.
- Validation. MLflow checks incoming data against the signature when you call
predicton a loaded model. A serving endpoint applies the same checks to each request.
Inferring a signature
The simplest way to get a signature is infer_signature. Pass it a sample of the inputs and the model's outputs on those inputs.
import mlflow
from mlflow.models import infer_signature
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
X, y = load_iris(return_X_y=True, as_frame=True)
model = RandomForestClassifier(n_estimators=50, random_state=0).fit(X, y)
signature = infer_signature(X, model.predict(X))
print(signature)The printed signature lists four input columns, each of type double, and one output of type long. Pass a pandas DataFrame, not a NumPy array. With a DataFrame, the signature keeps the column names, and those names appear later in your REST payload.
Writing a signature by hand
Inference reads types from the sample you pass. If the sample is not representative, the signature is wrong. For example, an integer column that can contain missing values must be declared as double. In those cases, build the signature yourself.
from mlflow.models import ModelSignature
from mlflow.types.schema import Schema, ColSpec
input_schema = Schema([
ColSpec("double", "sepal length (cm)"),
ColSpec("double", "sepal width (cm)"),
ColSpec("double", "petal length (cm)"),
ColSpec("double", "petal width (cm)"),
])
output_schema = Schema([ColSpec("long")])
signature = ModelSignschema, outputs=output_schema)Adding an input example
An input example is a few real rows of input, saved next. It serves two purposes:
- It documents what a valid request looks like.
- MLflow runs the example through the model when you log it. A model that cannot score its own example fails at logging time instead of after deployment.
Pass both to log_model:
import mlflow
with mlflow.start_run():
mlflow.sklearn.log_model(
model,
artifact_path="model",
signature=signature,
input_example=X.head(5),
)You can also pass only input_example, and MLflow will infer a signature from it. Passing the signature explicitly is clearer, because the types are then a choice you made and not a side effect.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Registration to Unity Catalog fails because the model has no signature | `log_model` was called without `signature` or `input_example` | Add a signature and log the model again |
| `predict` raises a schema enforcement error about a missing column | Input column names differ from the signature | Rename the columns, or check how the signature was built |
| Error about an incompatible type, such as `long` versus `double` | Data has different types than the sample used for inference | Cast the data, or write the signature by hand |
| Signature shows an integer column, but production data has nulls | The training sample had no missing values | Declare the column as `double` |
Checking what was saved
You can read the signature back from a logged model without loading the model itself:
from mlflow.models import get_model_info
info = get_model_info("runs:/<run-id>/model")
print(info.signature)Replace <run-id> with the ID of your run. If this prints None, the model was logged without a signature, and registration will fail.
Exercise
A column of whole-number ages is stored as an integer in your training data, but in production some ages are missing. Why might an inferred signature cause trouble, and what do you do?
Show answer
The inferred type would be an integer type, which cannot hold missing values. Requests with nulls could fail schema validation. Build the signature by hand and declare the column as "double" in a ColSpec so missing values are allowed.