Implementation:Mlflow Mlflow Log Artifact
| Knowledge Sources | |
|---|---|
| Domains | ML_Ops, Experiment_Tracking |
| Last Updated | 2026-02-13 20:00 GMT |
Overview
Concrete tool for persisting local files, directories, and trained models as artifacts within an MLflow run, provided by the MLflow library.
Description
mlflow.log_artifact uploads a local file or directory to the artifact store associated with the currently active run. It is the general-purpose function for persisting any output file -- plots, data exports, configuration snapshots, or custom serialization formats. For trained models specifically, mlflow.sklearn.log_model (and analogous functions in other flavor modules) provides a higher-level interface that serializes the model, records its dependencies and input/output signature, and optionally registers the model in the MLflow Model Registry.
Together, these two functions cover the full spectrum of artifact persistence needs: log_artifact for arbitrary files and log_model (per-flavor) for self-describing, deployment-ready model packages.
Usage
Use log_artifact for non-model files such as plots, CSV exports, configuration YAML files, or custom binary formats. Use mlflow.sklearn.log_model (or the equivalent for your framework) when logging a trained model that should be loadable via mlflow.sklearn.load_model or servable via mlflow models serve. Both functions require an active run context. Provide an artifact_path to organize artifacts into subdirectories within the run's artifact store.
Code Reference
Source Location
- Repository: mlflow
- File (log_artifact):
mlflow/tracking/fluent.py - Lines (log_artifact): L1500-1533
- File (sklearn.log_model):
mlflow/sklearn/__init__.py - Lines (sklearn.log_model): L379-501
Signature
# General artifact logging
def log_artifact(
local_path: str,
artifact_path: str | None = None,
run_id: str | None = None,
) -> None: ...
# scikit-learn model logging (flavor-specific wrapper)
def log_model(
sk_model,
artifact_path: str | None = None,
conda_env=None,
code_paths=None,
serialization_format=SERIALIZATION_FORMAT_CLOUDPICKLE,
registered_model_name=None,
signature: ModelSignature = None,
input_example: ModelInputExample = None,
await_registration_for=DEFAULT_AWAIT_MAX_SLEEP_SECONDS,
pip_requirements=None,
extra_pip_requirements=None,
pyfunc_predict_fn="predict",
metadata=None,
extra_files=None,
params: dict[str, Any] | None = None,
tags: dict[str, Any] | None = None,
model_type: str | None = None,
step: int = 0,
model_id: str | None = None,
name: str | None = None,
skops_trusted_types: list[str] | None = None,
**kwargs,
) -> ModelInfo: ...
Import
import mlflow
import mlflow.sklearn # for sklearn.log_model
I/O Contract
Inputs (log_artifact)
| Name | Type | Required | Description |
|---|---|---|---|
| local_path | str | Yes | Path to the local file or directory to upload as an artifact. |
| artifact_path | str or None | No | Subdirectory within the run's artifact URI where the artifact will be placed. If None, the artifact is placed at the root. |
| run_id | str or None | No | If specified, logs the artifact to the given run instead of the active run. |
Inputs (sklearn.log_model)
| Name | Type | Required | Description |
|---|---|---|---|
| sk_model | sklearn estimator | Yes | The fitted scikit-learn model object to serialize and log. |
| name | str or None | No | Name for the logged model within the artifact store. Replaces the deprecated artifact_path.
|
| signature | ModelSignature or None | No | Input/output schema describing the model's expected data types and shapes. |
| registered_model_name | str or None | No | If provided, registers the model under this name in the MLflow Model Registry, creating the registered model if it does not exist. |
| serialization_format | str | No | Serialization format: "cloudpickle" (default), "pickle", or "skops".
|
| input_example | ModelInputExample or None | No | Sample input data used for model signature inference and testing. |
| pip_requirements | list or None | No | Explicit pip requirements for the model environment. |
| model_type | str or None | No | Model type classification for registry purposes. |
Outputs
| Name | Type | Description |
|---|---|---|
| (log_artifact return) | None | The file is uploaded to the artifact store. No return value. |
| (sklearn.log_model return) | ModelInfo | Metadata about the logged model, including model_uri, model_id, and registered_model_version (if registered).
|
Usage Examples
Basic Usage
import tempfile
from pathlib import Path
import mlflow
# Log a text file as an artifact
with mlflow.start_run():
features = "rooms, zipcode, median_price, school_rating, transport"
with tempfile.TemporaryDirectory() as tmp_dir:
path = Path(tmp_dir, "features.txt")
path.write_text(features)
mlflow.log_artifact(path)
Logging a scikit-learn Model
import mlflow
import mlflow.sklearn
from mlflow.models import infer_signature
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
with mlflow.start_run():
iris = load_iris()
model = DecisionTreeClassifier()
model.fit(iris.data, iris.target)
signature = infer_signature(iris.data, model.predict(iris.data))
model_info = mlflow.sklearn.log_model(
model,
name="iris_classifier",
signature=signature,
)
print(f"Model URI: {model_info.model_uri}")
Logging with Model Registration
import mlflow
import mlflow.sklearn
from sklearn.ensemble import RandomForestClassifier
with mlflow.start_run():
model = RandomForestClassifier(n_estimators=100)
model.fit(X_train, y_train)
mlflow.sklearn.log_model(
model,
name="rf_model",
registered_model_name="production-classifier",
)