What does MLflow's pyfunc predict check against a logged model signature, and what happens to malformed input when no signature was logged?
answer
- checked only on the pyfunc path
- input_example infers it at log time
- missing required column is loud
- extra columns dropped quietly
- integer column with NaN arrives float
basics
~20 sWith a signature, pyfunc predict rejects missing required columns and unsafe type conversions, drops extra columns and applies params defaults. Without one, MLflow passes the input through untouched, so a bad request fails deep inside the model or scores wrongly.
solid answer
~50 sA signature is the input schema (named, typed columns, each required or optional), the output schema and an optional `params` schema, stored in `MLmodel`. It comes from `infer_signature(model_input, model_output, params)`, from an `input_example` that MLflow runs through the model at log time, or from an explicit `ModelSignature`. On every pyfunc `predict` — and so in MLflow's scoring server — a missing required column raises `Model is missing inputs [...]`, extra columns are dropped with a warning, columns are reordered to the schema, and only safe conversions are allowed: a `float64` column cannot become `long`, which is how an integer column with one missing value gets rejected. Params outside the params schema are dropped with a warning. With no signature, nothing is checked: the frame reaches the model as sent, and a renamed column surfaces as the model's own error, or not at all.
code
python · 16 linesimport mlflow
from mlflow.models import infer_signature
# a realistic slice, including rows where priority is missing
sample = train_df[["subject", "body", "priority"]].sample(200, random_state=7)
signature = infer_signature(sample, router.predict(sample))
with mlflow.start_run():
info = mlflow.sklearn.log_model(
router,
name="ticket-router",
signature=signature,
input_example=sample.head(3),
)
print(info.signature) # priority inferred as an optional double because of the gapsgo deeper
Recall that a signature lists input columns with types, that input_example produces one automatically, and that it lives in the MLmodel file.
Walk through what pyfunc predict does with a missing, extra or wrongly typed column, and why integer columns with gaps are the classic rejection.
Separate the loud failures from the quiet ones, explain why the native loader bypasses all of it, and choose the sample you infer from so production batches are not refused.
Decide whether the platform refuses models without signatures at registration, and weigh strict schemas that page on drift against permissive ones that let bad input score silently.
## What a signature is An MLflow **model signature** is a declaration, stored in the `MLmodel` file, of what the model accepts and returns. It has three parts: - **inputs** — either column-based (a list of named columns, each with an MLflow type such as `string`, `long`, `double`, `boolean` or `datetime`, and a `required` flag) or tensor-based (dtype and shape); - **outputs** — the same kind of schema for predictions; - **params** — an optional schema for inference-time parameters, each with a type and a default. For the support-ticket router, a sensible input schema is `subject` (string, required), `body` (string, required) and `priority` (double, optional) — double rather than long, because pandas cannot hold a missing value in an integer column. ## Where a signature comes from 1. **`input_example=`** on `log_model`. MLflow saves the example as `input_example.json` and `serving_input_example.json`, runs it through the model and infers the signature from the input and the output. This is the cheapest route and the one the docs recommend. 2. **`mlflow.models.infer_signature(model_input, model_output, params)`** when you want to control the sample, or add a params schema. 3. **An explicit `ModelSignature`** built from `Schema` and `ColSpec`, for example `ColSpec("long", "priority", required=False)`. Passing `signature=False` to `mlflow.sklearn.log_model` turns that inference off. A model logged with neither a signature nor an example simply has none — and against a self-hosted tracking server, `log_model` does not warn you. ## What pyfunc predict enforces Enforcement happens in `PyFuncModel.predict`, which MLflow's own serving paths go through: a model loaded with `mlflow.pyfunc.load_model`, the scoring server, `spark_udf`. The native flavor loader skips it. | Input condition | With a signature | Without one | |---|---|---| | Required column missing | `MlflowException`: `Model is missing inputs ['body']` | model's own error, often a `KeyError` deep in the pipeline | | Optional column missing | allowed | — | | Extra column present | dropped, warning `These inputs will be ignored.` | passed to the model | | Column order differs | reordered to the schema | passed as sent | | `int32` where `long` declared | upcast | passed as sent | | `float64` where `long` declared | rejected: `Can not safely convert float64 to int64` | passed as sent | | `params` sent | type-checked, defaults filled, unknown keys dropped with a warning | dropped with a warning | The allowed conversions are the safe ones: object to string, int to long, float to double, small ints to double, datetime precision changes. Anything that could change values is refused, including `long` to `double` unless every value is 0 or 1. ## The integer-with-missing-values trap pandas cannot hold a missing value in an integer column, so it turns the column into `float64`. If the training sample had no gaps, `infer_signature` records `priority` as `long`. The first production batch with one blank priority arrives as `float64`, and pyfunc refuses it. The error message even carries a hint about missing values. The fixes are to infer from a realistic sample that contains gaps — `infer_signature` then records the column as an optional `double` — or to declare such columns `double` by hand. ## Loud and silent failures The signature makes some mistakes loud that would otherwise be silent, but not all: - **Loud:** a renamed or missing required column, an unsafe type, a tensor of the wrong shape. - **Quiet:** an extra column (dropped with a warning), a missing optional column, a parameter the params schema does not declare. - **Silent with no signature:** a numeric column sent as strings may reach a scikit-learn pipeline that coerces or fails on its own terms, and nothing in MLflow records that the input was wrong. Output schemas are recorded for documentation and some consumers, but pyfunc does not check predictions against them. ## Seeing what a model expects Before writing a client, read the contract rather than guessing it. `mlflow.models.get_model_info(model_uri).signature` returns the parsed signature, and `mlflow.models.utils.load_serving_example(model_uri)` returns the request body MLflow saved alongside it. If schema errors must not echo request data or column names back to callers, setting `MLFLOW_DISABLE_SCHEMA_DETAILS=true` replaces the detailed messages with generic ones — the check still runs, only the message changes. ## Fixing a model logged without one For a logged model, `mlflow.models.set_signature(model_uri, signature)` writes a signature onto it. Registered versions are immutable, so the route there is to re-log the model with the signature and register the new copy as a fresh version.
- A wrapped model takes a min_confidence parameter, but values sent to predict seem to be ignored. Why?The signature has no params schema. Pyfunc drops any `params` the schema does not declare and only logs a warning. Re-log with `infer_signature(sample, output, params={"min_confidence": 0.6})`; the default is then filled when a caller omits it and type-checked when one is sent.
- Can you add a signature to a registered model version that was logged without one?Not in place: registered version artifacts are immutable. Load the model, infer a signature, re-log it with `signature=`, and register the new copy as a new version. For a logged model that is not registered, `mlflow.models.set_signature(model_uri, signature)` writes it directly.
- Without a signature, MLflow infers column types from the first request and enforces them.
- Extra columns in a request make MLflow schema enforcement fail.
- An integer column with a missing value still matches a long column in the signature.
- Passing input_example only saves a file; it has no effect on the signature.
- Params sent to pyfunc predict always reach the model, with or without a params schema.