How to Log Artifacts in MLflow: Simple Guide with Examples
To log artifacts in MLflow, use
mlflow.log_artifact() to save a single file or mlflow.log_artifacts() to save all files in a directory. These functions store files like models, images, or logs in the current MLflow run's artifact storage.Syntax
The main functions to log artifacts in MLflow are:
mlflow.log_artifact(local_path, artifact_path=None): Logs a single file fromlocal_path. Optionally, you can specifyartifact_pathto organize artifacts in folders.mlflow.log_artifacts(local_dir, artifact_path=None): Logs all files from the directorylocal_dir. You can also specifyartifact_pathto place them inside a folder.
Both functions must be called inside an active MLflow run context.
python
import mlflow # Log a single file mlflow.log_artifact(local_path='path/to/file.txt', artifact_path='my_folder') # Log all files in a directory mlflow.log_artifacts(local_dir='path/to/folder', artifact_path='my_folder')
Example
This example shows how to create a text file and log it as an artifact in MLflow. It demonstrates starting a run, logging the artifact, and ending the run.
python
import mlflow # Create a sample file to log file_name = 'example.txt' with open(file_name, 'w') as f: f.write('Hello, MLflow artifacts!') # Start an MLflow run with mlflow.start_run(): # Log the file as an artifact mlflow.log_artifact(file_name, artifact_path='texts') print(f'Logged artifact: {file_name}')
Output
Logged artifact: example.txt
Common Pitfalls
- Not starting an MLflow run: You must call
mlflow.start_run()before logging artifacts, or you will get an error. - Incorrect file paths: Ensure the file or directory exists at the given path before logging.
- Confusing
log_artifactandlog_artifacts: Uselog_artifactfor single files andlog_artifactsfor directories.
python
import mlflow # Wrong: logging without a run try: mlflow.log_artifact('example.txt') except Exception as e: print(f'Error: {e}') # Right: logging inside a run with mlflow.start_run(): mlflow.log_artifact('example.txt') print('Artifact logged successfully')
Output
Error: No active run. mlflow.start_run() should be used to start a run.
Artifact logged successfully
Quick Reference
| Function | Purpose | Parameters |
|---|---|---|
| mlflow.log_artifact | Log a single file as an artifact | local_path: file path, artifact_path: optional folder |
| mlflow.log_artifacts | Log all files in a directory | local_dir: directory path, artifact_path: optional folder |
| mlflow.start_run | Start an MLflow run context | No parameters needed |
| mlflow.end_run | End the current MLflow run | No parameters needed |
Key Takeaways
Always call mlflow.start_run() before logging artifacts to create an active run.
Use mlflow.log_artifact() for single files and mlflow.log_artifacts() for directories.
Specify artifact_path to organize artifacts inside folders in the run's storage.
Check that file or directory paths exist before logging to avoid errors.
Artifacts are stored with the run and can be viewed or downloaded later from the MLflow UI.