shapash icon indicating copy to clipboard operation
shapash copied to clipboard

feat(smartpredictor): add version/schema manifest sidecar to save/load

Open immu4989 opened this issue 5 months ago • 0 comments

Description

This PR implements Gap 1 of #707 (Gap 2 shipped via PR #711, merged 2026-06-15).

SmartPredictor.save(path) now writes a manifest sidecar at path + ".manifest.json" alongside the pickle, containing:

  • shapash_version, python_version
  • model_framework ({name, version} — recognises scikit-learn, xgboost, lightgbm, catboost)
  • shap_version
  • schema_fingerprint — deterministic SHA-256 over the canonical JSON of features_dict, features_types, columns_dict, and label_dict
  • saved_at (UTC ISO timestamp)

load_smartpredictor(path) uses the sidecar to detect version skew and schema tampering at load time:

Condition Behaviour
Manifest present and matching Silent load
Manifest absent (older pickle) DeprecationWarning, still loads
Schema fingerprint mismatch ValueError (catches tampering or stale manifest)
Major shapash version mismatch ValueError
Minor shapash version skew UserWarning
Model framework version differs UserWarning

The pickle file format is unchanged — pickles saved by older versions of shapash without a manifest still load successfully (with DeprecationWarning).

All new helpers (_build_predictor_manifest, _save_manifest, _load_manifest, _check_predictor_manifest, _compute_schema_fingerprint, _detect_model_framework, _try_package_version, _parse_major_minor) live in shapash/utils/io.py next to the existing save_pickle / load_pickle. Type hints on all of them per @guillaume-vignal's mypy-prep request landed on #711.

Fixes #707 (Gap 1 of two; Gap 2 shipped via PR #711).

Type of change

  • [ ] Bug fix (non-breaking change which fixes an issue)
  • [x] New feature (non-breaking change which adds functionality or feature that would cause existing functionality to not work as expected)
  • [ ] This change requires a documentation update

How Has This Been Tested?

8 new tests in tests/unit_tests/utils/test_load_smartpredictor.py:

  • [x] test_save_writes_manifest_sidecar — save() writes a sidecar with all expected fields, including model_framework.name == "catboost".
  • [x] test_load_with_matching_manifest_is_clean — load_smartpredictor on a freshly-saved predictor produces no warning (warnings.simplefilter("error") guard).
  • [x] test_load_without_manifest_warns_and_loads — save_pickle directly (no sidecar) followed by load_smartpredictor emits DeprecationWarning and still returns the predictor.
  • [x] test_load_with_schema_fingerprint_mismatch_raises — tampering with the manifest's schema_fingerprint raises ValueError.
  • [x] test_load_with_major_version_mismatch_raises — rewriting shapash_version to 999.0.0 raises ValueError.
  • [x] test_load_with_minor_version_mismatch_warns — rewriting shapash_version to X.(Y+1).0 emits UserWarning.
  • [x] test_schema_fingerprint_is_deterministic — same predictor yields the same fingerprint across calls.
  • [x] test_manifest_round_trip_preserves_fingerprint — _build_predictor_manifest's fingerprint matches _compute_schema_fingerprint directly.

Existing test_save_1 in test_smart_predictor.py updated to also clean up the new manifest sidecar after the test.

Reproduce locally:

.venv/bin/python -m pytest tests/unit_tests/explainer/test_smart_predictor.py tests/unit_tests/utils/test_load_smartpredictor.py -q
# → 45 passed (37 SmartPredictor + 1 existing roundtrip + 8 new manifest tests, minus a test-class restructure)

.venv/bin/ruff format --check shapash/utils/io.py shapash/utils/load_smartpredictor.py shapash/explainer/smart_predictor.py tests/unit_tests/utils/test_load_smartpredictor.py
# → 4 files already formatted

Test Configuration:

  • OS: macOS (Darwin)
  • Python version: 3.12.13
  • Shapash version: 2.9.0 (editable install of this branch off master 0c34c86)

Other library versions: pandas 3.0.3, numpy 2.4.4, catboost 1.2.10, pytest 9.0.3.

Checklist:

  • [x] My code follows the style guidelines of this project
  • [x] I have performed a self-review of my own code
  • [x] I have commented my code, particularly in hard to understand areas — docstrings on all new helpers explain the contract; the manifest schema is also documented in this PR description
  • [x] I have made corresponding changes to the documentation — docstrings on SmartPredictor.save and load_smartpredictor updated to describe the sidecar; Sphinx docs auto-generate from docstrings
  • [x] My changes generate no new warnings
  • [x] I have added tests that prove my fix is effective or that my feature works
  • [x] New and existing unit tests pass locally with my changes
  • [x] Any dependent changes have been merged and published in downstream modules — N/A, no dependent changes

Notes

  • Sidecar JSON vs predictor attribute. Original issue (#707) flagged this as an open shape question. Went with sidecar JSON (the original recommendation) because it keeps the pickle bytes byte-identical to the pre-PR format, lets external tools inspect provenance without unpickling, and avoids changing the public attribute surface of SmartPredictor. Happy to switch to attribute-based storage if that fits your roadmap better.
  • Backward compatibility: pickles saved by older versions of shapash (no manifest) still load — they just emit a DeprecationWarning. The warning category can be changed to UserWarning if DeprecationWarning is too strong a signal here.
  • The schema_fingerprint is computed over the user-declared schema (features_dict, features_types, columns_dict, label_dict), not over the model or training data. Catches the case where the schema is mutated in-place after saving (which would silently break downstream add_input validation); doesn't attempt to claim model reproducibility — that's a different scope.
  • No new dependencies. Uses stdlib hashlib, json, datetime, importlib.metadata.
  • Future follow-ups (for the SmartPredictor production-hardening series): structured logging on predict_proba / compute_contributions / summarize failures; schema-drift detection that compares incoming distributions to the saved schema at add_input time. Happy to file these as separate issues if there's interest.

immu4989 avatar Jun 15 '26 19:06 immu4989