Install & Compatibility
Where this runs
tested against v4.7.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.940 runs
build_error
glibcpy 3.10–3.940 runs
installs and imports cleanly · install 7.4s · import 0.984s · 228MB
231MB installed
● package 231MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
treelite
✓ import treelite
treelite.frontend
✓ import treelite.frontend
Contains functions like `from_xgboost`, `load_lightgbm_model` for importing external models.
tl2cgen
✓ import tl2cgen
The dedicated module for model compilation and runtime prediction (post-Treelite 4.0).
treelite_runtime
✓ import tl2cgen
✗ import treelite_runtime
The `treelite_runtime` module was deprecated and its functionality migrated to `tl2cgen` in Treelite 4.0+. Use `tl2cgen.Predictor` and `tl2cgen.DMatrix` instead.
This quickstart demonstrates how to import a pre-trained XGBoost model into Treelite, compile it into a shared library using `tl2cgen`, and then use the compiled library for predictions. Note that Treelite itself does not train models.
import os
import numpy as np
import xgboost # Required to train a model to be imported
import treelite
import tl2cgen # The separate compiler and runtime library
# 1. Train a dummy XGBoost model (in a real scenario, you'd load a pre-trained model)
X = np.random.rand(100, 10).astype('float32')
y = np.random.rand(100).astype('float32')
dtrain = xgboost.DMatrix(X, label=y)
param = {'max_depth': 2, 'eta': 1, 'objective': 'reg:squarederror'}
bst = xgboost.train(param, dtrain, num_boost_round=10)
# 2. Import the XGBoost model into Treelite
model = treelite.frontend.from_xgboost(bst)
# 3. Compile the Treelite model into a shared library using TL2cgen
# Choose appropriate extension: .so for Linux, .dll for Windows, .dylib for macOS
libpath = "./predictor.so"
tl2cgen.export_lib(model, toolchain="gcc", libpath=libpath, verbose=True)
# 4. Load the compiled model with TL2cgen for prediction
predictor = tl2cgen.Predictor(libpath=libpath, verbose=True)
# 5. Make predictions
dtest = tl2cgen.DMatrix(X)
predictions = predictor.predict(dtest)
print("Predictions (first 5):", predictions[:5])
# Clean up the generated library
os.remove(libpath)
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'treelite'
The Treelite Python package is not installed in the active Python environment or is not accessible on the Python path.
fixInstall the package using pip: `pip install treelite`
AttributeError: 'ModelBuilder' object has no attribute 'tree_builder'
This error occurs when attempting to programmatically build decision trees using the deprecated `ModelBuilder` and `TreeBuilder` API, which was removed in Treelite 4.0.
fixMigrate to the new API by importing models from external frameworks (e.g., `treelite.frontend.xgboost.import_model`) or by programmatically constructing `treelite.Model` objects using `treelite.gtil` utilities.
AttributeError: 'treelite.model.Model' object has no attribute 'export_lib'
The method `export_lib` for compiling a Treelite model into a shared library was removed in Treelite 4.0, replaced by a new compilation API.
fixUse `treelite.gtil.compile_model` for ahead-of-time compilation, or `treelite.runtime.CompiledModel` for runtime compilation and prediction.
TypeError: predict() got an unexpected keyword argument 'pred_contribs'
The `predict` methods in Treelite (e.g., `treelite.Model.predict` or `treelite.runtime.CompiledModel.predict`) do not directly support keyword arguments like `pred_contribs` for SHAP value computation in the same way native frameworks (like XGBoost) do.
fixConsult Treelite's documentation for the supported arguments to `predict`. For SHAP contributions, use external SHAP libraries or Treelite's specific SHAP-related functionalities if available, as it's not a direct `predict` argument.
Upgrade
Version history
4.7.0latest on PyPI · released Mar 6, 2026
Audit
Dependencies
numpyrequiredUsed for numerical operations and data handling with models.
xgboostoptionalOptional: For importing models trained with XGBoost.
lightgbmoptionalOptional: For importing models trained with LightGBM.
scikit-learnoptionalOptional: For importing models trained with scikit-learn.
tl2cgenrequiredRequired: For compiling Treelite models into deployable shared libraries and for prediction runtime (since Treelite 4.0+).