In MLflow, what does a logged model's MLmodel file declare, and how does it let one scikit-learn model load through both mlflow.sklearn and mlflow.pyfunc?
answer
- a directory, not one file
- YAML manifest at the root
- one entry per flavor
- python_function names a loader_module
- native object against generic predict
basics
~20 sMLmodel is the YAML manifest at the root of a logged MLflow model directory, listing its flavors, signature, input example and environment files. The sklearn flavor loads the native Pipeline; python_function names mlflow.sklearn as its loader_module, so pyfunc gets one generic predict.
solid answer
~40 sA logged MLflow model is a directory, not a single file: the serialized model, `conda.yaml`, `python_env.yaml`, `requirements.txt`, an optional `input_example.json` and `serving_input_example.json`, and the `MLmodel` YAML at the root. `MLmodel` lists every **flavor** the model can be read as, plus `signature`, `saved_input_example_info`, `mlflow_version`, `run_id` and `utc_time_created`. `mlflow.sklearn.log_model` writes two flavors that point at the same model file. The `sklearn` flavor records the file, its `serialization_format` and the scikit-learn version, so `mlflow.sklearn.load_model` hands back the real `Pipeline` with its steps and `predict_proba`. The `python_function` flavor records `loader_module: mlflow.sklearn` and the environment files, so `mlflow.pyfunc.load_model` returns a `PyFuncModel` with one `predict` — the interface MLflow's own tools rely on, and the path on which the signature is enforced.
code
yaml · 21 lines# MLmodel at the root of the logged ticket-router directory (abridged)
flavors:
python_function:
env:
conda: conda.yaml
virtualenv: python_env.yaml
loader_module: mlflow.sklearn
model_path: model.skops
predict_fn: predict
sklearn:
pickled_model: model.skops
serialization_format: skops
mlflow_version: 3.16.1
saved_input_example_info:
artifact_path: input_example.json
pandas_orient: split
type: dataframe
signature:
inputs: '[{"type": "string", "name": "subject", "required": true}, {"type": "string", "name": "body", "required": true}]'
outputs: '[{"type": "string", "required": true}]'
params: nullgo deeper
Recall the directory layout: MLmodel at the root, the serialized model, three environment files, an optional input example. Know that one model carries several flavors.
Explain what each flavor entry records, why python_function names a loader_module, and what changes when you load natively instead of through pyfunc.
Show you know which loader production uses and what that implies: signature enforcement only on pyfunc, one predict method chosen at log time, no dependency installation on load.
Frame the flavor system as a contract between producers and generic consumers, and decide which loader a platform standardises on so that checks are not bypassed by teams loading natively.
## What a logged model is When a training job calls a flavor's `log_model` — say `mlflow.sklearn.log_model` for the support-ticket router's text pipeline — MLflow does not store one opaque file. It writes a **model directory** with a fixed layout: - the serialized model itself (`model.skops` or `model.pkl` for scikit-learn, depending on `serialization_format`); - `MLmodel`, a YAML file at the directory root that describes everything else; - `conda.yaml`, `python_env.yaml` and `requirements.txt`, which describe the Python environment the model needs; - `input_example.json` and `serving_input_example.json`, written only when an `input_example` was passed and validated; - `environment_variables.txt`, written only when MLflow saw allow-listed environment variables (names only, never values) read during inference. The platform team that receives the router does not need the data scientist's notebook. Everything a loader needs is either in that directory or named by `MLmodel`. ## The MLmodel file, field by field | Field | What it records | |---|---| | `flavors` | one entry per way the model can be loaded, each with its own keys | | `signature` | the input schema, output schema and optional params schema, as JSON strings | | `saved_input_example_info` | where the input example file is and how it is encoded | | `mlflow_version` | the MLflow release that logged the model | | `run_id`, `utc_time_created` | which run created it and when | The file is metadata only. It never holds weights; it points at the files that do. ## Flavors: one model, several ways to read it A **flavor** is a convention a loader understands. A model can declare several, and each flavor entry carries only what its own loader needs. A scikit-learn model logged by `mlflow.sklearn` gets two: 1. `sklearn` — keys such as `pickled_model` (the file name, kept under that historical key even when the file is skops), `serialization_format` and `sklearn_version`. `mlflow.sklearn.load_model` reads this entry and returns the native estimator or `Pipeline`. 2. `python_function` — keys such as `loader_module: mlflow.sklearn`, `model_path`, `predict_fn`, `python_version` and `env` (pointing at `conda.yaml` and `python_env.yaml`). `mlflow.pyfunc.load_model` imports the named loader module, asks it to rebuild the model, and wraps the result in a `PyFuncModel`. Both entries point at the same serialized file; nothing is stored twice. MLflow's built-in flavors also write `python_function` — scikit-learn skips it only when the estimator lacks the chosen predict method — which is why the docs can say any MLflow Python model is loadable as pyfunc. ## Native flavor against python_function | Question | `mlflow.sklearn.load_model` | `mlflow.pyfunc.load_model` | |---|---|---| | What comes back | the `Pipeline` object itself | a `PyFuncModel` wrapper | | Methods available | everything scikit-learn offers | `predict` (plus `predict_stream` where supported) | | Signature enforced | no | yes, on every `predict` | | Who uses it | code that knows it holds scikit-learn | MLflow's scoring server, `spark_udf`, batch jobs that treat models generically | Neither loader installs dependencies. `mlflow.pyfunc.load_model` assumes the packages are present and only warns when the installed versions differ from the ones the model recorded. ## Inspecting a logged model without loading it You rarely need to open the YAML by hand. `mlflow.models.get_model_info(model_uri)` downloads only the metadata and returns a `ModelInfo` whose `flavors` dictionary is the `flavors` section of `MLmodel` and whose `signature` is the parsed signature. Two quick checks it answers before anyone writes serving code: - which loaders will work — if `python_function` is missing from `flavors`, generic tools cannot load the model at all; - what the model expects — the signature's input names and types are the contract a caller must meet. The same `flavors` entry also shows any load-time `model_config` a pyfunc model accepts, which `mlflow.pyfunc.load_model(model_uri, model_config=...)` can override. ## Why this matters at handoff The data scientist tested `router.predict_proba(df)` on the native object. The platform team loads the same URI with `mlflow.pyfunc.load_model` and calls `predict`, which goes through the method named by `predict_fn` — `predict` unless the model was logged with `pyfunc_predict_fn="predict_proba"`. Two consequences follow: - a team that needs probabilities through pyfunc must say so at log time, because the generic interface has one prediction method; - schema checks run only on the pyfunc path, so a notebook test on the native object proves nothing about how a malformed request will be treated in production. ## Common traps - Treating the directory as "a pickle file". The environment files and `MLmodel` are what make it portable. - Editing `MLmodel` by hand to change a flavor. Re-log the model instead; the flavor entries are written by the flavor's own `save_model`. - Expecting `mlflow.pyfunc.load_model` to return the `Pipeline`. It returns a wrapper; the native loader returns the object.
- The platform team needs class probabilities from the router, but pyfunc predict returns labels. What do you change?Re-log with `mlflow.sklearn.log_model(..., pyfunc_predict_fn="predict_proba")`. The flavor records it as `predict_fn` in the `python_function` entry, and `PyFuncModel.predict` then calls `predict_proba`. The supported names are `predict`, `predict_proba`, `predict_log_proba`, `predict_joint_log_proba` and `score`.
- Why does mlflow.pyfunc.load_model print a warning about package versions, and does it fix them?It does not install anything. It compares the requirements recorded with the model against the current environment and warns on mismatches. `mlflow.pyfunc.get_model_dependencies(model_uri)` returns the path of the model's `requirements.txt` (or `conda.yaml`) so you can install them yourself.
A logged model is like a document saved with both a native file and a universal print view: open it in the native editor and every feature is there; open it in a generic viewer and you get one uniform page that any tool can display.
- A logged MLflow model is a single pickle file you copy around.
- mlflow.pyfunc.load_model returns the original scikit-learn Pipeline object.
- Loading with the sklearn flavor validates inputs against the logged signature.
- The MLmodel file stores the model weights.
- mlflow.pyfunc.load_model installs the model's dependencies before loading it.