Which environment files does MLflow write with a logged model, and how do you fix a missing dependency and prove it before deploying?
answer
- three files, one per tool
- inferred from imports, not a freeze
- extra_pip_requirements adds, pip_requirements replaces
- a fresh environment, virtualenv by default
- override first, then update the model
basics
~10 sMLflow writes requirements.txt, python_env.yaml and conda.yaml, inferred from what the model imports. Add a missed package with extra_pip_requirements, or test one with mlflow.models.predict's pip_requirements_override, which scores in a fresh environment built from those files.
solid answer
~50 sEvery logged model carries `requirements.txt` (pip packages, pinned), `python_env.yaml` (the Python version, pip/setuptools/wheel and `-r requirements.txt`, for the `virtualenv` and `uv` managers) and `conda.yaml` (for `conda`). The requirements are not a `pip freeze`: MLflow loads the saved model in a subprocess and records the packages it imports. In a uv project — `uv.lock` plus `pyproject.toml`, since MLflow 3.11 — it exports them from the lockfile instead. A package imported only on a rare path is missed. Add it at log time with `extra_pip_requirements`; `pip_requirements` replaces the inferred list, and only one of `conda_env`, `pip_requirements` and `extra_pip_requirements` may be passed. To prove the fix, `mlflow.models.predict(model_uri, input_data, env_manager=...)` builds a fresh environment from the model's files (`virtualenv` by default) and scores in it. `pip_requirements_override` tries a fix without re-logging, and `mlflow.models.update_model_requirements` writes it into the logged model.
code
python · 20 linesimport mlflow
from mlflow.models.utils import load_serving_example
uri = model_info.model_uri # returned by mlflow.pyfunc.log_model(...)
# 1. Score in a fresh environment built from the model's files,
# trying the missing package without re-logging.
mlflow.models.predict(
model_uri=uri,
input_data=load_serving_example(uri),
env_manager="uv",
pip_requirements_override=["langdetect"],
)
# 2. Once it scores, write the package into requirements.txt and conda.yaml.
mlflow.models.update_model_requirements(
model_uri=uri,
operation="add",
requirement_list=["langdetect"],
)go deeper
Recall the three environment files and which tool reads each, and that MLflow writes them on every log without being asked.
Explain how requirements are inferred, when inference silently falls back to defaults, and the difference between extra_pip_requirements, pip_requirements and conda_env.
Show the proof loop: score in a fresh environment, override to test a fix, persist it with update_model_requirements, and know why the local manager proves nothing.
Decide whether the platform requires locked dependencies or uv lockfiles at log time, and where environment proof belongs in the path from a data scientist's run to a deployable artifact.
## The three files Whenever a model is logged, MLflow writes three descriptions of the environment it needs, one per tool that may rebuild it: | File | Holds | Read by | |---|---|---| | `requirements.txt` | pip requirements, pinned to the versions installed at log time | everything below, directly or by reference | | `python_env.yaml` | the Python version, build tools (`pip`, `setuptools`, `wheel`) and `-r requirements.txt` | the `virtualenv` and `uv` environment managers | | `conda.yaml` | a conda environment with the Python version and the same pip list | the `conda` environment manager | The docs advise against editing these files by hand: they are regenerated whenever the model is saved again, and the supported changes go through the APIs below. ## How MLflow decides what goes in them The requirements are **inferred**, not frozen from the notebook: 1. If the working directory is a **uv project** (`uv.lock` and `pyproject.toml`, with uv installed), MLflow 3.11 and later export the locked requirements with `uv export`. Setting `MLFLOW_UV_AUTO_DETECT=false` turns this off. 2. Otherwise MLflow loads the saved model in a subprocess and records the packages it imports, then merges them with the flavor's defaults. Passing an `input_example` makes the capture more accurate, because MLflow can also run a prediction. 3. If inference fails or times out, MLflow logs a warning and falls back to the flavor's default requirements. Only `MLFLOW_REQUIREMENTS_INFERENCE_RAISE_ERRORS=true` turns that into an error. Point 3 is how a model gets logged with an incomplete `requirements.txt` and nobody notices until it is loaded somewhere clean. ## Declaring dependencies yourself - **`extra_pip_requirements=[...]`** adds packages to the inferred list — the usual fix for a package imported lazily, for example a language-detection library that the support-ticket router's pre-filter imports only for non-English tickets. - **`pip_requirements=[...]`** replaces the inferred list entirely. The docs warn that this is risky unless it matches what the model was trained with. - **`conda_env=`** supplies a whole conda environment. - These three are mutually exclusive: passing more than one raises `ValueError`. - **`code_paths=[...]`** is for local modules that are not pip packages. - **`MLFLOW_LOCK_MODEL_DEPENDENCIES=true`** (since MLflow 2.16, needs uv) pins transitive dependencies too. ## Proving the environment before deployment `mlflow.pyfunc.load_model` does not install anything; in the notebook it succeeds because the notebook already has every package. The check that means something builds a clean environment from the model's own files: - **`mlflow.models.predict(model_uri, input_data=..., env_manager=...)`** — `env_manager` is `"virtualenv"` by default (with pyenv for the Python version), or `"uv"`, `"conda"` or `"local"`. `"local"` reuses the current environment and so proves nothing about the files. Results go to stdout or `output_path`. - **`pip_requirements_override=[...]`** installs extra or different packages into that environment for this call only, so you can iterate without re-logging. - **`mlflow.models.utils.load_serving_example(model_uri)`** returns the saved serving example, a ready-made `input_data`. ## A worked diagnosis The support-ticket router scores in the notebook, but the platform team's batch job fails with `ModuleNotFoundError: No module named 'langdetect'`. The path from there: 1. Read the model's `requirements.txt` — `mlflow.pyfunc.get_model_dependencies(model_uri)` returns its local path — and confirm the package is absent. It is: the pre-filter imports it inside a branch that the logging-time capture never ran. 2. Reproduce away from the notebook with `mlflow.models.predict(..., env_manager="uv")` or the default virtualenv. The same error proves the files, not the job, are wrong. 3. Add the package with `pip_requirements_override` and score again until it passes. 4. Persist the fix, as below, so that every later consumer gets it. ## Writing the fix into the model Once a candidate fix scores, persist it. `mlflow.models.update_model_requirements(model_uri, operation="add", requirement_list=[...])` rewrites `requirements.txt` and `conda.yaml` in the logged model's artifacts. It refuses registered-model URIs such as `models:/ticket-router/3`, because registered artifacts are read-only; re-log with `extra_pip_requirements` and register a new version instead. ## What this does not cover The environment files pin Python packages. Operating-system libraries, the base image and the hardware are outside them, which is why a test in a virtualenv on a laptop can still differ from a container on a server.
- The model loads in the notebook but logs a warning about package versions in the platform's job. What is going on?`mlflow.pyfunc.load_model` loads into whatever environment it runs in and only compares versions against the model's recorded requirements, warning on mismatches. The job's environment drifted from the one the model was logged in. Install from the model's files, which `mlflow.pyfunc.get_model_dependencies(model_uri)` returns, or score through an environment manager.
- Why can mlflow.models.predict with env_manager='local' pass while deployment fails?`local` skips building an environment and runs in the caller's own interpreter, which has every package the notebook installed. It tests the input format and the code, not the environment files. Use `virtualenv` (the default), `uv` or `conda` so the files are what gets tested.
- requirements.txt is a pip freeze of the notebook environment.
- mlflow.pyfunc.load_model installs the model's dependencies before loading it.
- mlflow.models.predict with env_manager='local' proves the model's environment files.
- pip_requirements adds packages to the list MLflow inferred.
- If requirement inference fails, log_model stops and nothing is logged.
Assumes MLflow 3.16; exporting requirements from uv.lock during inference arrived in MLflow 3.11.