mlflow-shap
Log and load SHAP explainers as MLflow artifacts with a one-liner. Drop-in for any model that shap.Explainer accepts (tree models, linear models, deep nets, etc.).
Installation
pip install mlflow-shap
Quick start
Log an explainer
import mlflow
from sklearn.ensemble import RandomForestClassifier
from mlflow_shap import log_explainer
with mlflow.start_run() as run:
model = RandomForestClassifier().fit(X_train, y_train)
mlflow.sklearn.log_model(model, artifact_path="model")
log_explainer(model, X_train)
Load it back
from mlflow_shap import load_explainer
explainer = load_explainer(run_id="abc123")
shap_values = explainer(X_test)
That's it. The explainer is serialized with joblib and stored under the run's shap_explainer/ artifact directory.
API
log_explainer
log_explainer(
model,
data,
*,
explainer=None,
artifact_path="shap_explainer",
file_name="shap_explainer.pkl",
explainer_kwargs=None,
run_id=None,
) -> str
Returns the artifact URI (runs:/<id>/<artifact_path>/<file_name>).
| Argument | Description |
|---|---|
model |
Trained model. Ignored when explainer is supplied. |
data |
Background data passed to shap.Explainer. |
explainer |
Pre-built shap.Explainer (e.g. TreeExplainer, DeepExplainer). Skips auto-construction. |
artifact_path |
Sub-directory inside the run's artifact root. Default shap_explainer. |
file_name |
Pickle file name. Default shap_explainer.pkl. |
explainer_kwargs |
Extra kwargs forwarded to shap.Explainer(...). |
run_id |
Target run. Default uses the active run. |
load_explainer
load_explainer(
run_id,
artifact_path="shap_explainer/shap_explainer.pkl",
*,
dst_path=None,
) -> shap.Explainer
Downloads the artifact and deserializes via joblib.
Advanced usage
Use a specific explainer
import shap
from mlflow_shap import log_explainer
tree_explainer = shap.TreeExplainer(model)
log_explainer(model, X_train, explainer=tree_explainer)
Log into a specific run (no active run)
log_explainer(model, X_train, run_id="abc123")
Custom artifact location
log_explainer(model, X_train, artifact_path="explainability/shap", file_name="rf_v2.pkl")
Exceptions
| Exception | When raised |
|---|---|
NoActiveRunError |
No active MLflow run and no run_id was passed. |
ExplainerCreationError |
shap.Explainer(model, data) failed. Wraps the original error. |
MLflowSHAPError |
Base class for the above. |
Compatibility
- Python: 3.9, 3.10, 3.11, 3.12
- MLflow: >= 2.0
- SHAP: >= 0.42
Development
git clone https://github.com/princepongsakorn/mlflow-shap.git
cd mlflow-shap
pip install -e ".[dev]"
pytest
License
MIT — see LICENSE.
Metadata
Release files for mlflow-shap 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| mlflow_shap-0.1.1.tar.gz | 7.5 kB | With provenance | Details for mlflow_shap-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| mlflow_shap-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for mlflow_shap-0.1.1-py3-none-any.whl |
Total release size: 14.7 kB
Release files / mlflow_shap-0.1.1.tar.gz
| Download URL | mlflow_shap-0.1.1.tar.gz |
|---|---|
| Size | 7.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ab841c6850732cbc9dcb3e8a2f321bc693d4d41cc0de1a79312807667a1a103d
|
|
BLAKE2b-256 checksum How to use checksums |
0ce8731a19a6e7de050f95a57087d2ed8f682255541b7f636d5e67c4368095fe
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 11, 2026.
Transparency logRelease files / mlflow_shap-0.1.1-py3-none-any.whl
| Download URL | mlflow_shap-0.1.1-py3-none-any.whl |
|---|---|
| Size | 7.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
86a84b2201dddbe9b14219a786b7212b64ff7ccb418709aa054c74fb6ed7d7b7
|
|
BLAKE2b-256 checksum How to use checksums |
204c6278c208e3f3abb3f00f0e72fb5728c354ba8611b8586d5f054089012376
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 11, 2026.
Transparency logSecurity for mlflow-shap 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for mlflow_shap-0.1.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 11, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.