Registry / ai-ml / onnxmltools

onnxmltools

JSON →
library1.16.0pypypiunverified

ONNXMLTools facilitates the conversion of various machine learning models (e.g., scikit-learn, LightGBM, XGBoost, TensorFlow, SparkML) into the ONNX (Open Neural Network Exchange) format. This enables model interoperability across different frameworks and hardware. The current version is 1.16.0, and it maintains an active development cycle with new releases every few months.

pip install onnxmltools
INSTALL
IMPORT
SIG · ONNXMLTOOLS
O
onnxmltools
ai-mlpythonv1.16.0
Install
Import
Disk
Pass rate
0/ 10
Env Coverage0 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.16.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
glibc
py 3.10
✕ build_error
4/8 runs
py 3.11
✕ build_error
4/8 runs
py 3.12
✕ build_error
4/8 runs
py 3.13
✕ build_error
4/8 runs
py 3.9
✕ build_error
4/8 runs
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

convert_lightgbm
from onnxmltools import convert_lightgbm
from onnxmltools import convert_lightgbm

This quickstart demonstrates converting a simple LightGBM classifier into the ONNX format. It highlights the use of `convert_lightgbm` and the importance of defining `initial_types` and `target_opset`. Remember to install `lightgbm`, `onnx`, and `onnx_lightgbm` separately for this to run.

import lightgbm as lgb from onnxmltools import convert_lightgbm from onnxmltools.convert.common.data_types import FloatTensorType from onnx.checker import check_model from onnx import save import numpy as np # Ensure required packages are installed try: import lightgbm # noqa: F401 import onnx_lightgbm # noqa: F401 import onnx # noqa: F401 except ImportError as e: print(f"Skipping quickstart: Missing dependency. Please install lightgbm, onnx, and onnx_lightgbm. Error: {e}") exit() # 1. Train a LightGBM model X = np.array([[0, 0], [1, 1], [2, 2], [3, 3]], dtype=np.float32) y = np.array([0, 1, 1, 0], dtype=np.int32) gbm = lgb.LGBMClassifier(n_estimators=3, max_depth=2, learning_rate=0.1, random_state=42) gbm.fit(X, y) # 2. Define initial types for ONNX conversion # 'None' in FloatTensorType([None, 2]) means variable batch size initial_type = [('float_input', FloatTensorType([None, 2]))] # 3. Convert to ONNX format, specifying a target opset (e.g., 17) target_opset = 17 # Or a lower opset depending on ONNX Runtime compatibility onnx_model = convert_lightgbm(gbm, initial_types=initial_type, target_opset=target_opset) # 4. Check the ONNX model for validity check_model(onnx_model) # 5. Save the ONNX model to a file save(onnx_model, "lightgbm_model.onnx") print("LightGBM model successfully converted to lightgbm_model.onnx")
Debug
Known issues
gotchaONNXMLTools itself does not include the framework-specific converter packages (e.g., `skl2onnx`, `onnx_lightgbm`, `tf2onnx`). You must install these separately for the respective conversion functions to work.
fix
Install the required converter package alongside `onnxmltools`. For example, for LightGBM models, use `pip install onnxmltools onnx_lightgbm lightgbm`.
affects: All versions
gotchaThe `initial_types` parameter is mandatory for most conversion functions and defines the input tensor's name, type, and shape. Incorrect specification is a common source of errors.
fix
Carefully define `initial_types` using data types like `FloatTensorType`, `Int64TensorType`, etc., from `onnxmltools.convert.common.data_types`. Ensure the shape (e.g., `[None, num_features]`) matches your model's expected input.
affects: All versions
breakingOpset version compatibility is crucial. Choosing a `target_opset` that is too low might not support newer model features, while a `target_opset` that is too high might not be supported by your ONNX Runtime version.
fix
Align `target_opset` with the capabilities of your ONNX Runtime environment and the features used in your original model. Consult the ONNX operator schema for supported opsets.
affects: All versions
gotchaFor binary classification models, the output shape (e.g., probabilities) can sometimes vary between `[N, 1]` and `[N, 2]`, leading to discrepancies with expected ONNX Runtime outputs.
fix
Explicitly check the output tensor shape after conversion and adjust post-processing logic if necessary. Newer versions of `onnxmltools` and underlying converters aim to standardize this, but vigilance is recommended.
affects: >=1.13.0
Upgrade
Version history
1.16.0latest on PyPI · released Jan 30, 2026
Audit
Dependencies
onnxrequiredCore ONNX graph manipulation, definition, and validation.
numpyrequiredRequired for numerical operations, especially tensor handling.
onnx_lightgbmoptionalRequired for converting LightGBM models to ONNX.
onnx_xgboostoptionalRequired for converting XGBoost models to ONNX.
skl2onnxoptionalRequired for converting scikit-learn models to ONNX.
tf2onnxoptionalRequired for converting TensorFlow and Keras models to ONNX.
sparkml2onnxoptionalRequired for converting SparkML models to ONNX.
Agent activity
20 hits · last 30 days
node
20
Resources
onnxmltools — pip install onnxmltools · libregistry