Registry / ai-ml / treelite

treelite

JSON →
library4.7.0pypypi✓ verified 86d ago

Treelite is a universal model exchange and serialization format for decision tree forests. It enables C++ applications to efficiently exchange and store decision trees from various sources like XGBoost, LightGBM, and scikit-learn. The current version is 4.7.0. It is actively maintained with irregular but feature-rich releases, including a significant architectural shift with version 4.0.

pip install treelite
INSTALL
IMPORT
SIG · TREELITE
T
treelite
ai-mlpythonv4.7.0
Install
7.4s avg
Import
984ms
Disk
231MB
Pass rate
5/ 10
Env Coverage5 / 10
glibc
3.93.13
musl
3.93.13
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
musl
py 3.103.940 runs
build_error
glibc
py 3.103.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)
Debug
Known issues
breakingStarting from Treelite 4.0, the tree compiler and prediction runtime functionality (e.g., `treelite.Model.compile`, `treelite.Model.export_lib`, and the `treelite_runtime` module) were migrated to a separate project called TL2cgen. Older code relying on these features will break.
fix
Install `tl2cgen` (e.g., `pip install tl2cgen`) and replace `treelite_runtime` imports with `tl2cgen`. Replace `treelite.Model.export_lib` with `tl2cgen.export_lib`, and `treelite_runtime.Predictor`/`DMatrix` with `tl2cgen.Predictor`/`DMatrix`.
affects: >=4.0.0
gotchaTreelite's primary role is model exchange and serialization; it does not train decision tree models. You must use other libraries like XGBoost, LightGBM, or scikit-learn to train models before importing them into Treelite.
fix
Ensure you have a pre-trained model object or file from a supported ML library before attempting to use Treelite for import and deployment.
affects: All versions
gotchaOn macOS, Treelite requires the OpenMP runtime. On Windows, Visual C++ Redistributable might be necessary for the generated shared libraries to function correctly.
fix
For macOS, run `brew install libomp`. For Windows, ensure Visual C++ Redistributable is installed.
affects: All versions
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.
fix
Install 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.
fix
Migrate 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.
fix
Use `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.
fix
Consult 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+).
Agent activity
27 hits · last 30 days
node
24
Amazon
1
OpenAI (training)
1
Resources
treelite — pip install treelite · libregistry