MLflow

raw JSON →
3.10.1 verified Tue May 12 auth: no python install: stale quickstart: stale

MLflow is an open-source platform designed to manage the entire machine learning lifecycle, encompassing experiment tracking, reproducible projects, model management, and deployment. The current stable version is 3.10.1, with frequent updates including patch, minor, and major releases that introduce new features and breaking changes. [9, 16]

pip install mlflow
error ModuleNotFoundError: No module named 'mlflow'
cause The MLflow library is not installed in the current Python environment or the environment where the code is being executed. [3, 7, 17, 18, 25]
fix
Install MLflow using pip or conda in your active environment: pip install mlflow or conda install -c conda-forge mlflow
error AttributeError: module 'mlflow' has no attribute 'sklearn'
cause You are attempting to access an MLflow integration (e.g., 'sklearn', 'keras', 'pytorch') as a direct attribute of the top-level 'mlflow' module, but these integrations are typically imported as submodules or are not directly exposed this way. [14, 18, 30]
fix
Explicitly import the specific flavor module, for example: import mlflow.sklearn or from mlflow import sklearn. Ensure the underlying machine learning library (e.g., scikit-learn) is also installed.
error mlflow.exceptions.MlflowException: Invalid parameter
cause An incorrect parameter name, an unsupported value, or a value of the wrong data type was provided to an MLflow logging or API function. [5]
fix
Refer to the MLflow documentation for the specific function being used (e.g., mlflow.log_param, mlflow.log_metric) to verify the correct parameter names, expected data types, and valid value ranges.
error mlflow.exceptions.MlflowException: When an mlflow-artifacts URI was supplied, the tracking URI must be a valid http or https URI, but it was currently set to file:///...
cause This error occurs when trying to serve an MLflow model, often with `mlflow models serve --enable-mlserver`, but the MLflow tracking URI is configured to a local `file://` path. Remote model serving requires artifacts to be accessible via an HTTP(S) MLflow Tracking Server. [15, 20]
fix
Start a remote MLflow Tracking Server (e.g., mlflow server --backend-store-uri sqlite:///mlflow.db --default-artifact-root ./mlruns) and configure your client to use its HTTP(S) address by setting the MLFLOW_TRACKING_URI environment variable or calling mlflow.set_tracking_uri('http://localhost:5000').
error OSError: No such file or directory: '/path/to/mlruns/...'
cause MLflow cannot find a required file or directory for its tracking store (typically `mlruns`) or artifact storage, likely due to an incorrect path, insufficient permissions, or the directory not existing. [1, 9, 28, 35, 37, 38]
fix
Verify that the specified path is correct, exists, and that the user running the MLflow process has appropriate read/write permissions. If using mlflow server, ensure --backend-store-uri and --default-artifact-root (or --artifacts-destination) are correctly configured and accessible.
breaking MLflow 3.x introduced significant breaking changes, including the complete removal of MLflow Recipes. Many model flavors (fastai, mleap, diviner) are no longer supported directly. The 'routes' and 'route_type' config keys for AI Gateway were removed. The deployment server and `start-server` CLI command have been removed, replaced by `mlflow models serve` or containerized deployments. [1, 3]
fix Review the MLflow 3 Migration Guide. For Recipes, migrate to standard MLflow tracking/model registry or MLflow Projects. For unsupported flavors, use `mlflow.pyfunc` with a custom wrapper or `mlflow.onnx`/`mlflow.pytorch`. Use the new AI Gateway configuration format and `mlflow models serve` for deployments. [1]
breaking In MLflow 3.x, the `run_uuid` attribute on `RunInfo` objects has been removed and replaced by `run_id`. Additionally, several Git-related run tags (`mlflow.gitBranchName`, `mlflow.gitRepoURL`) were removed. [1]
fix Update code to use `run_id` instead of `run_uuid`. Remove reliance on the deprecated Git tags or implement custom tagging for similar information. [1]
breaking The Artifacts tab in the MLflow UI for runs no longer displays model artifacts in MLflow 3.x. Model artifacts are now accessed through a dedicated 'Logged Models' page. [1]
fix Navigate to the 'Logged Models' page in the MLflow UI to view model-specific information and artifacts. Update any automation or user guides that rely on the old UI structure. [1]
deprecated Parameters like `artifact_path` are deprecated; use `name` instead. Additionally, parameters such as `example_no_conversion` and `code_path` have been removed from model logging/saving APIs. `requirements_file` for PyTorch flavor is removed, and `inference_config` from Transformers flavor is also removed. [1]
fix For deprecated `artifact_path`, use `name`. For `code_path`, use the default code directory structure. For PyTorch `requirements_file`, use `pip_requirements` or `extra_pip_requirements`. For Transformers `inference_config`, set the configuration before logging the model. [1]
gotcha When upgrading a self-hosted MLflow server, it is crucial to stop the server, upgrade the package, run database migrations using `mlflow db upgrade <backend-store-url>`, and then restart the server. MLflow does not natively support live upgrades, and schema migrations can be slow and non-transactional. Always back up your database before migration. [16]
fix Follow the official upgrade procedure carefully. Plan for downtime or implement a rolling upgrade strategy with a load balancer for high availability. Back up your database before any migration. [16]
gotcha MLflow clients and servers work best when they are on the same version. While a newer server is generally backward compatible with older clients for basic logging, using newer client features (e.g., MLflow Tracing) with an older server might lead to missing endpoints or unexpected behavior. [16]
fix Strive to keep your MLflow client SDK and server versions aligned. If using new client features, ensure your server is also updated to a compatible version. [16]
gotcha Building native Python packages (e.g., scikit-learn, numpy, pandas, cryptography) in minimal environments like Alpine Linux often fails due to missing C/C++ compilers and development headers. These packages require tools like `gcc` and `g++` to compile their C/Fortran/C++ extensions during installation.
fix Install necessary build tools and compilers (e.g., `build-base`, `gcc`, `g++`, `python3-dev`) in your Dockerfile or environment before attempting to install such packages. For Alpine, typically `apk add build-base gcc python3-dev` is required.
python os / libc status wheel install import disk
3.10 alpine (musl) build_error - - - -
3.10 alpine (musl) - - - -
3.10 slim (glibc) wheel 38.4s 4.63s 734M
3.10 slim (glibc) - - 3.93s 732M
3.11 alpine (musl) build_error - - - -
3.11 alpine (musl) - - - -
3.11 slim (glibc) wheel 37.1s 5.71s 794M
3.11 slim (glibc) - - 5.36s 791M
3.12 alpine (musl) build_error - - - -
3.12 alpine (musl) - - - -
3.12 slim (glibc) wheel 34.5s 6.12s 768M
3.12 slim (glibc) - - 6.65s 766M
3.13 alpine (musl) build_error - - - -
3.13 alpine (musl) - - - -
3.13 slim (glibc) wheel 37.4s 5.78s 765M
3.13 slim (glibc) - - 5.88s 763M
3.9 alpine (musl) build_error - - - -
3.9 alpine (musl) - - - -
3.9 slim (glibc) wheel 37.6s 3.87s 767M
3.9 slim (glibc) - - 3.27s 766M

This quickstart demonstrates how to log parameters, metrics, and a scikit-learn model using the MLflow fluent API. It sets up an experiment, trains a logistic regression model on the Iris dataset, logs its hyperparameters and accuracy, infers the model signature, and registers the model in the MLflow Model Registry. To view the results, start the MLflow UI by running `mlflow ui` in your terminal and navigating to `http://localhost:5000` (or `http://127.0.0.1:5000`). [2, 5, 22]

import mlflow
import pandas as pd
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from mlflow.models import infer_signature

# Set MLflow tracking URI (optional, defaults to local ./mlruns)
# For a local server, run 'mlflow ui' in your terminal and point to http://127.0.0.1:5000
# os.environ['MLFLOW_TRACKING_URI'] = os.environ.get('MLFLOW_TRACKING_URI', 'http://127.0.0.1:5000')

mlflow.set_experiment("MLflow_Quickstart_Experiment")

# Load the Iris dataset
X, y = datasets.load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Define model hyperparameters
params = {"solver": "lbfgs", "max_iter": 1000, "multi_class": "auto", "random_state": 8888}

with mlflow.start_run():
    # Log hyperparameters
    mlflow.log_params(params)

    # Train the model
    lr = LogisticRegression(**params)
    lr.fit(X_train, y_train)

    # Make predictions and calculate metrics
    y_pred = lr.predict(X_test)
    accuracy = accuracy_score(y_test, y_pred)
    mlflow.log_metric("accuracy", accuracy)

    # Infer model signature
    predictions = lr.predict(X_train) # Use training data for signature inference
    signature = infer_signature(X_train, predictions)

    # Log the model
    mlflow.sklearn.log_model(
        sk_model=lr,
        artifact_path="logistic_regression_model",
        signature=signature,
        registered_model_name="IrisLogisticRegression"
    )

    print(f"Logged model with accuracy: {accuracy}")
    print(f"View runs in MLflow UI: run 'mlflow ui' in your terminal and navigate to http://127.0.0.1:5000")