Skip to main content

Serialize fitted scikit-learn models to safetensors + JSON

Project description

skeights logo

skeights

Serialize fitted scikit-learn models to safetensors + JSON.

No pickle. No joblib. Just weights and config.

Why?

Pickle is the default way to save sklearn models, but it's:

  • Insecure: arbitrary code execution on load
  • Fragile: breaks across sklearn versions, Python versions, and platforms
  • Opaque: you can't inspect what's inside

skops solves the security problem by replacing pickle with a safe binary format, but the output is still a single opaque blob; you can't easily inspect the hyperparameters or diff two versions of a model.

skeights separates structure from weights:

  • .json: hyperparameters and scalar fitted state, human-readable and diffable
  • .safetensors: numeric arrays (weights, fitted params) in a safe, fast, widely-supported format

Install

pip install skeights

Usage

import skeights
from sklearn.linear_model import Ridge
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler

# Fit your model as usual
pipe = Pipeline([
    ("scaler", StandardScaler()),
    ("model", Ridge(alpha=0.1)),
])
pipe.fit(X_train, y_train)

# Save
skeights.save(pipe, "model.safetensors", "model.json")

# Load and predict
loaded = skeights.load("model.safetensors", "model.json")
predictions = loaded.predict(X_test)

Supported estimators

  • Linear models: Ridge, Lasso, LinearRegression, LogisticRegression, etc.
  • MLPRegressor / Classifier: multi-layer perceptron
  • DecisionTreeRegressor / Classifier: full tree serialization
  • RandomForestRegressor / Classifier: full tree serialization
  • GradientBoostingRegressor / Classifier: including init estimator
  • HistGradientBoostingRegressor / Classifier: including bin mapper state
  • LGBMRegressor / Classifier: via booster model string
  • XGBRegressor / Classifier: via booster JSON
  • GaussianProcessRegressor / Classifier: including composite kernels
  • TransformedTargetRegressor: target scaling wrappers
  • Scalers: StandardScaler, MinMaxScaler, RobustScaler
  • Pipelines: any Pipeline composed of supported estimators

Compatibility

skeights requires scikit-learn >= 1.5 and tests against 1.5, 1.6, and latest in CI.

Saved models are forward-compatible on a best-effort basis: we test loading sklearn 1.5 fixtures on newer versions, but don't guarantee cross-version compatibility.

When loading a model saved with a different sklearn version, skeights will emit a warning.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

skeights-0.2.0.tar.gz (2.4 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

skeights-0.2.0-py3-none-any.whl (21.5 kB view details)

Uploaded Python 3

File details

Details for the file skeights-0.2.0.tar.gz.

File metadata

  • Download URL: skeights-0.2.0.tar.gz
  • Upload date:
  • Size: 2.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for skeights-0.2.0.tar.gz
Algorithm Hash digest
SHA256 5907d4a77fad357e4c21e06695f425836a7870348b9f04844d150cc4b7e5ba58
MD5 7d74ce70a278f0766d8d75bc3557d369
BLAKE2b-256 36a7489b7c6ef750acab8cc06c6c71900c6694e719c33e1bd4ee9e31c043eaec

See more details on using hashes here.

Provenance

The following attestation bundles were made for skeights-0.2.0.tar.gz:

Publisher: publish.yml on carbon-re/skeights

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file skeights-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: skeights-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 21.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for skeights-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b2877ec292ce5990ee475a4faf0e4202f3302b8ec6516633e52c816b70342c9c
MD5 27a2ef376e23739bd2bdf82d91e887ed
BLAKE2b-256 cc1ec5a95fbdc0c03a679b17cd2a6e9aee1ffefbd8552664e435646c985826df

See more details on using hashes here.

Provenance

The following attestation bundles were made for skeights-0.2.0-py3-none-any.whl:

Publisher: publish.yml on carbon-re/skeights

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page