Skip to content

LightGBM: Add decision_leaf output support#757

Draft
Copilot wants to merge 2 commits into
mainfrom
copilot/add-decision-path-output
Draft

LightGBM: Add decision_leaf output support#757
Copilot wants to merge 2 commits into
mainfrom
copilot/add-decision-path-output

Conversation

Copilot AI commented Feb 23, 2026

Copy link
Copy Markdown

LightGBM's predict(X, pred_leaf=True) has no ONNX equivalent — the converter had no mechanism to output which leaf node each sample reached per tree.

Approach

Adds a decision_leaf=False parameter to the converter. When True, a second TreeEnsembleRegressor node is generated alongside the main prediction node, configured to output the sequential leaf index per tree (cast to int64). This avoids modifying the existing prediction graph and keeps the two concerns independent.

Output shape: [N, n_trees], dtype int64.

Changes

  • convert/main.py + lightgbm/convert.py: Expose decision_leaf on the public convert_lightgbm() API and thread it through to parse_lightgbm()
  • lightgbm/_parse.py: Propagate decision_leaf through parse functions; attach it to the operator and append a leaf_indices output variable when enabled; fix LgbmZipMap to only receive the first two outputs (label + probs) as inputs
  • lightgbm/operator_converters/LightGbm.py: Add _add_decision_leaf_output() — builds target_* attrs from class_treeids/class_nodeids, assigns one output column per tree (n_targets = n_trees), and casts float output to int64; fix regressor branch to use operator.outputs[0].full_name instead of operator.output_full_names (which would incorrectly include leaf_indices)
  • Shape calculators (Classifier.py, Regressor.py, Ranker.py): Accept the optional extra output; set Int64TensorType([N, n_trees]) when decision_leaf=True
  • tests/lightgbm/test_lightgbm_decision_leaf.py: Tests for binary classifier, multiclass classifier, regressor, and the default no-leaf case

Usage

from onnxmltools.convert import convert_lightgbm
from onnxmltools.convert.common.data_types import FloatTensorType

onx = convert_lightgbm(
    model,
    initial_types=[("X", FloatTensorType([None, n_features]))],
    zipmap=False,
    decision_leaf=True,
)
# onx.graph.output → [label, probabilities, leaf_indices]
# leaf_indices: int64[N, n_trees]

sess = onnxruntime.InferenceSession(onx.SerializeToString())
label, proba, leaf_indices = sess.run(None, {"X": X})
Original prompt

This section details on the original issue you should resolve

<issue_title>LightGBM: Support for outputting decision path and leaf</issue_title>
<issue_description>Has any work already begun for outputting decision path and leaf data? I believe it would be similar to this implementation for decision tree and random forest in sklearn_onnx onnx/sklearn-onnx#563.

One challenge appears to be onnxmltools does not have a story for converters that accept options. I see that sklearn implemented their own topology and Scope that exposes an options API.</issue_description>

Comments on the Issue (you are @copilot in this section)

@xadupre I did that work for models in scikit-learn: https://onnx.ai/sklearn-onnx/auto_tutorial/plot_dbegin_options.html#option-decision-path but I did not do it for xgboost or lightgbm. The way to do it is to generate a second tree producing the leave id as a target, then link that id with the value you need to produce for that leave with a LabelEncoder.

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: xadupre <22452781+xadupre@users.noreply.github.com>
@sonarqubecloud

Copy link
Copy Markdown

Copilot AI changed the title [WIP] Add support for outputting decision path and leaf in LightGBM LightGBM: Add decision_leaf output support Feb 23, 2026
Copilot AI requested a review from xadupre February 23, 2026 20:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LightGBM: Support for outputting decision path and leaf

2 participants