Explain
Explain scikit-learn and XGBoost model predictions with per-row SHAP values and global feature importance
- Connector software
- Free and open source
- Service credentials
- Not needed
- Query Farm hosting
- Available · $10/month organization minimum
Use with an agent
Install the Orchard skill in your project to help your agent find connectors, configure them, and verify SQL results.
npx skills add Query-farm/vgi-skills --skill orchard
Copy this request into your coding agent to get started.
Use https://query.farm/products/orchard/connectors/explain/index.md to help me query Explain with DuckDB or Haybarn 2.0.1 and VGI protocol 3.4. Ask only for missing details about the result I need or whether I want local execution, self-hosting or Query Farm hosting. Check the connector's actual hosting availability, credentials and input requirements. Read one format of each needed reference and the exact object declarations before writing SQL. Use a released connector, and distinguish a saved example result from anything you actually execute. If you cannot run the query, tell me what remains to verify.
See which features drive a model’s predictions
Example output · captured
- Connector release
- v0.5.0
- Engine
- 2.0.1
- VGI protocol
- 3.4.0
SET VARIABLE m = (SELECT model FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/models.parquet') WHERE name = 'rf_clf'); SELECT feature, rank, method
FROM explain.main.feature_importance((SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')), model := getvariable('m'), id := 'id')
ORDER BY rank
| feature | rank | method |
|---|---|---|
| signal | 1 | mean_abs_shap |
| noise_b | 2 | mean_abs_shap |
| noise_a | 3 | mean_abs_shap |
Quick start
Choose where to run Explain, then follow the steps below.
The connector runs alongside DuckDB or Haybarn. No Query Farm account is needed.
1. Install the vgi extension
Run this in your SQL client. On DuckDB, keep FROM query_farm on LOAD in later sessions.
SET allow_extension_repositories = 'allowed';
CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
WITH PREFIX 'https://duckdb-extensions.query.farm';
INSTALL vgi FROM query_farm;
LOAD vgi FROM query_farm;
INSTALL vgi FROM community;
LOAD vgi;
2. Attach the connector
Attach with uvx
Needs uv. uvx builds the worker from its release tag on first attach and reuses the cached environment after that.
ATTACH 'explain' AS explain (
TYPE vgi,
LOCATION 'uvx --from git+https://github.com/Query-farm/[email protected] vgi-explain'
);
Attach the signed image
Needs Docker, Podman or another OCI runtime. The extension runs the container and talks to it over stdio.
ATTACH 'explain' AS explain (
TYPE vgi,
LOCATION 'oci://ghcr.io/query-farm/vgi-explain:0.5.0'
);
3. Run a query
SET VARIABLE m = (SELECT model FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/models.parquet') WHERE name = 'rf_clf'); SELECT feature, rank, method
FROM explain.main.feature_importance((SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')), model := getvariable('m'), id := 'id')
ORDER BY rank
Run the connector on a server you manage. You operate the service and choose who can connect.
1. Deploy the connector on your server
Follow the connector’s deployment instructions. Use its supported HTTP transport, or launch it remotely over SSH. The connection guide explains both options.
2. Install the vgi extension
Run this in your SQL client. On DuckDB, keep FROM query_farm on LOAD in later sessions.
SET allow_extension_repositories = 'allowed';
CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
WITH PREFIX 'https://duckdb-extensions.query.farm';
INSTALL vgi FROM query_farm;
LOAD vgi FROM query_farm;
INSTALL vgi FROM community;
LOAD vgi;
3. Attach the connector
For an HTTP deployment, replace the example URL with your endpoint. Add the authentication options your deployment requires. For SSH, use the SSH connection instructions.
ATTACH 'explain' AS explain
(TYPE vgi, LOCATION 'https://your-connector.example.com');
4. Run a query
SET VARIABLE m = (SELECT model FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/models.parquet') WHERE name = 'rf_clf'); SELECT feature, rank, method
FROM explain.main.feature_importance((SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')), model := getvariable('m'), id := 'id')
ORDER BY rank
Query Farm operates Explain for your team. Hosting has a $10 monthly minimum per organization; usage counts toward it. From $2.97 per instance-month ($10 monthly minimum).
1. Create your hosted connector
Open the console, choose your organization and deployment size, and add any service credentials there.
2. Install the vgi extension
SET allow_extension_repositories = 'allowed';
CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
WITH PREFIX 'https://duckdb-extensions.query.farm';
INSTALL vgi FROM query_farm;
LOAD vgi FROM query_farm;
INSTALL vgi FROM community;
LOAD vgi;
3. Attach your deployment
Copy the ATTACH statement from your deployment in the console and run it in DuckDB or Haybarn. Use explain as the catalog alias for the query below.
4. Run a query
SET VARIABLE m = (SELECT model FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/models.parquet') WHERE name = 'rf_clf'); SELECT feature, rank, method
FROM explain.main.feature_importance((SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')), model := getvariable('m'), id := 'id')
ORDER BY rank
Technical details
- Verified connector release
- v0.5.0
- Engine
- 2.0.1
- VGI protocol
- 3.4.0
- Verified on
- December 1, 2026
Pin a version for production
When running the connector yourself, keep a specific release until you’re ready to upgrade. Replace the ATTACH statement above with this one and follow the same extension and credential setup.
Attach with uvx
Needs uv. uvx builds the worker from its release tag on first attach and reuses the cached environment after that.
ATTACH 'explain' AS explain (
TYPE vgi,
LOCATION 'uvx --from git+https://github.com/Query-farm/[email protected] vgi-explain'
);
Attach the signed image
Needs Docker, Podman or another OCI runtime. The extension runs the container and talks to it over stdio.
ATTACH 'explain' AS explain (
TYPE vgi,
LOCATION 'oci://ghcr.io/query-farm/vgi-explain:0.5.0'
);
Overview
SHAP explanations for scikit-learn / XGBoost models as SQL functions.

Explainable AI for machine learning predictions, right inside DuckDB — compute SHAP (SHapley Additive exPlanations) feature attributions for fitted scikit-learn and XGBoost models with plain SQL.
The explain catalog turns model interpretability into a set of DuckDB/SQL table functions, so you can answer why a model made a prediction without leaving your database or writing any Python. It is built for data engineers, analysts, and ML practitioners who need transparent, auditable, per-prediction explanations — for model debugging, regulatory and fair-lending reviews, feature selection, or simply building trust in a model's output. Every explanation is computed over your data in place via Apache Arrow, so it composes naturally with joins, filters, and aggregates in ordinary SQL.
Under the hood the worker is powered by SHAP, the reference implementation of Shapley-value feature attribution (documentation), grounded in cooperative game theory's Shapley values. Importantly, vgi-explain does not train models — it interprets the self-contained model BLOB produced by the sibling vgi-sklearn (scikit-learn) and vgi-xgboost (XGBoost) workers. SHAP automatically selects an appropriate explainer (e.g. TreeExplainer for tree ensembles), so explanations are exact and fast for the supported model families. Because a DuckDB table function accepts only one subquery argument (reserved for the feature relation), the model travels as a scalar model argument, usually held in a session variable and read with getvariable('m') — see the schema's and each function's example queries for ready-to-run SQL.
Three functions make up the surface. shap_values streams a feature relation and emits per-row, per-feature signed contributions in long format (with a contribution per class for multi-class classifiers), aligning input columns to the model's fitted features by name. shap_base_value returns the explainer's expected value — the model's baseline output that the per-feature contributions add onto, so that base value + sum(contributions) reproduces the prediction (SHAP's additivity property). feature_importance aggregates mean(|SHAP|) across a relation to produce a global, ranked importance of which features matter most, falling back to the model's native importances when no rows are supplied. Together they cover local (single-prediction) and global (whole-dataset) interpretability for the models you already serve from VGI.
Catalog
Everything explain exposes once attached — read straight from the worker's
released build.
main 1 view · 3 functions
SHAP explanations for scikit-learn / XGBoost models as SQL functions
supported_models
Model families this worker can explain and the SHAP explainer chosen for each.
List every supported model family and the SHAP explainer chosen for it.
SELECT model_family, shap_explainer, exactness
FROM explain.main.supported_models
ORDER BY model_family;
Find which families get exact (rather than approximate) SHAP explanations.
SELECT model_family, shap_explainer
FROM explain.main.supported_models
WHERE exactness = 'exact'
ORDER BY model_family;
Count the supported model families grouped by their SHAP explainer.
SELECT shap_explainer, count(*) AS family_count
FROM explain.main.supported_models
GROUP BY shap_explainer
ORDER BY shap_explainer;
shap_base_value (model := BLOB)
The SHAP base (expected) value of a model -- one row, or one row per class
Read the model's expected (base) output — one row per class ('0' and '1') for this binary RandomForest classifier.
SET VARIABLE m = (SELECT model FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/models.parquet') WHERE name = 'rf_clf'); SELECT class, base_value
FROM explain.main.shap_base_value(model := getvariable('m'))
ORDER BY class;
Reconstruct each row's predicted class-1 probability as the SHAP base value plus the sum of the row's per-feature contributions (SHAP additivity), joining shap_values to shap_base_value on class, and list the rows the model is least sure about (probability closest to 0.5).
SET VARIABLE m = (SELECT model FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/models.parquet') WHERE name = 'rf_clf'); WITH contrib AS (SELECT id, class, sum(shap_value) AS total_shap FROM explain.main.shap_values((SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')), model := getvariable('m'), id := 'id') GROUP BY id, class) SELECT c.id, round(b.base_value, 4) AS base_value, round(c.total_shap, 4) AS total_shap, round(b.base_value + c.total_shap, 4) AS predicted_proba
FROM contrib AS c
JOIN explain.main.shap_base_value(model := getvariable('m')) AS b ON c.class IS NOT DISTINCT FROM b.class
WHERE c.class = '1'
ORDER BY round(abs(b.base_value + c.total_shap - 0.5), 4), c.id
LIMIT 6;
feature_importance (data TABLE, id := VARCHAR, model := BLOB)
Global feature importance: mean(|SHAP|) over the relation (or native importances if empty)
Rank features by their average SHAP magnitude across the dataset (model in session VARIABLE m).
SET VARIABLE m = (SELECT model FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/models.parquet') WHERE name = 'rf_clf'); SELECT feature, rank, method
FROM explain.main.feature_importance((SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')), model := getvariable('m'), id := 'id')
ORDER BY rank;
| feature | rank | method |
|---|---|---|
| signal | 1 | mean_abs_shap |
| noise_b | 2 | mean_abs_shap |
| noise_a | 3 | mean_abs_shap |
Compare each feature's global importance with the number of rows in which it is the dominant (largest |SHAP|) driver of the class-1 prediction, joining feature_importance to per-row shap_values.
SET VARIABLE m = (SELECT model FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/models.parquet') WHERE name = 'rf_clf'); WITH dominant AS (SELECT id, arg_max(feature, abs(shap_value)) AS feature FROM explain.main.shap_values((SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')), model := getvariable('m'), id := 'id') WHERE class = '1' GROUP BY id) SELECT g.feature, g.rank, round(g.importance, 4) AS importance, count(d.id) AS rows_dominated
FROM explain.main.feature_importance((SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')), model := getvariable('m'), id := 'id') AS g
LEFT JOIN dominant AS d ON d.feature = g.feature
GROUP BY g.feature, g.rank, g.importance
ORDER BY g.rank;
Drill into the globally top-ranked feature: the rows where its SHAP contribution pushes hardest toward class 1, shown next to the feature's raw input value.
SET VARIABLE m = (SELECT model FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/models.parquet') WHERE name = 'rf_clf'); SELECT s.id, s.feature, round(f.value, 4) AS feature_value, round(s.shap_value, 4) AS shap_value
FROM explain.main.shap_values((SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')), model := getvariable('m'), id := 'id') AS s
JOIN explain.main.feature_importance((SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')), model := getvariable('m'), id := 'id') AS g ON g.feature = s.feature AND g.rank = 1
JOIN (UNPIVOT (SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')) ON signal, noise_a, noise_b INTO NAME feature VALUE value) AS f ON f.id = s.id AND f.feature = s.feature
WHERE s.class = '1'
ORDER BY s.shap_value DESC, s.id
LIMIT 5;
shap_values (data TABLE, id := VARCHAR, model := BLOB)
Per-row, per-feature SHAP contributions in long format (one row per row x feature [x class])
Explain each row's prediction feature by feature (model held in session VARIABLE m).
SET VARIABLE m = (SELECT model FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/models.parquet') WHERE name = 'rf_clf'); SELECT id, feature, class, shap_value
FROM explain.main.shap_values((SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')), model := getvariable('m'), id := 'id')
ORDER BY id, feature, class
LIMIT 6;
Reconstruct each row's predicted class-1 probability as the SHAP base value plus the sum of the row's per-feature contributions (SHAP additivity), joining shap_values to shap_base_value on class, and list the rows the model is least sure about (probability closest to 0.5).
SET VARIABLE m = (SELECT model FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/models.parquet') WHERE name = 'rf_clf'); WITH contrib AS (SELECT id, class, sum(shap_value) AS total_shap FROM explain.main.shap_values((SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')), model := getvariable('m'), id := 'id') GROUP BY id, class) SELECT c.id, round(b.base_value, 4) AS base_value, round(c.total_shap, 4) AS total_shap, round(b.base_value + c.total_shap, 4) AS predicted_proba
FROM contrib AS c
JOIN explain.main.shap_base_value(model := getvariable('m')) AS b ON c.class IS NOT DISTINCT FROM b.class
WHERE c.class = '1'
ORDER BY round(abs(b.base_value + c.total_shap - 0.5), 4), c.id
LIMIT 6;
Try these queries
Read the model's expected (base) output — one row per class ('0' and '1') for this binary RandomForest classifier.
SET VARIABLE m = (SELECT model FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/models.parquet') WHERE name = 'rf_clf');
SELECT class, base_value FROM explain.main.shap_base_value(model := getvariable('m')) ORDER BY class;
Uses shap_base_value
Explain each row's prediction feature by feature (model held in session VARIABLE m).
SET VARIABLE m = (SELECT model FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/models.parquet') WHERE name = 'rf_clf');
SELECT id, feature, class, shap_value FROM explain.main.shap_values((SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')), model := getvariable('m'), id := 'id') ORDER BY id, feature, class LIMIT 6;
Uses shap_values
Reconstruct each row's predicted class-1 probability as the SHAP base value plus the sum of the row's per-feature contributions (SHAP additivity), joining shap_values to shap_base_value on class, and list the rows the model is least sure about (probability closest to 0.5).
SET VARIABLE m = (SELECT model FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/models.parquet') WHERE name = 'rf_clf');
WITH contrib AS (SELECT id, class, sum(shap_value) AS total_shap FROM explain.main.shap_values((SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')), model := getvariable('m'), id := 'id') GROUP BY id, class) SELECT c.id, round(b.base_value, 4) AS base_value, round(c.total_shap, 4) AS total_shap, round(b.base_value + c.total_shap, 4) AS predicted_proba FROM contrib AS c JOIN explain.main.shap_base_value(model := getvariable('m')) AS b ON c.class IS NOT DISTINCT FROM b.class WHERE c.class = '1' ORDER BY round(abs(b.base_value + c.total_shap - 0.5), 4), c.id LIMIT 6;
Compare each feature's global importance with the number of rows in which it is the dominant (largest |SHAP|) driver of the class-1 prediction, joining feature_importance to per-row shap_values.
SET VARIABLE m = (SELECT model FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/models.parquet') WHERE name = 'rf_clf');
WITH dominant AS (SELECT id, arg_max(feature, abs(shap_value)) AS feature FROM explain.main.shap_values((SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')), model := getvariable('m'), id := 'id') WHERE class = '1' GROUP BY id) SELECT g.feature, g.rank, round(g.importance, 4) AS importance, count(d.id) AS rows_dominated FROM explain.main.feature_importance((SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')), model := getvariable('m'), id := 'id') AS g LEFT JOIN dominant AS d ON d.feature = g.feature GROUP BY g.feature, g.rank, g.importance ORDER BY g.rank;
Drill into the globally top-ranked feature: the rows where its SHAP contribution pushes hardest toward class 1, shown next to the feature's raw input value.
SET VARIABLE m = (SELECT model FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/models.parquet') WHERE name = 'rf_clf');
SELECT s.id, s.feature, round(f.value, 4) AS feature_value, round(s.shap_value, 4) AS shap_value FROM explain.main.shap_values((SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')), model := getvariable('m'), id := 'id') AS s JOIN explain.main.feature_importance((SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')), model := getvariable('m'), id := 'id') AS g ON g.feature = s.feature AND g.rank = 1 JOIN (UNPIVOT (SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')) ON signal, noise_a, noise_b INTO NAME feature VALUE value) AS f ON f.id = s.id AND f.feature = s.feature WHERE s.class = '1' ORDER BY s.shap_value DESC, s.id LIMIT 5;
List every supported model family and the SHAP explainer chosen for it.
SELECT model_family, shap_explainer, exactness
FROM explain.main.supported_models
ORDER BY model_family
Uses supported_models
7 more examples in the Catalog, each listed under the table or function it uses.
Explain is an independent open-source connector from Query.Farm. It is not affiliated with, endorsed by or sponsored by the services, projects or organisations it works with. Product names, logos and trademarks belong to their respective owners.
Related workers
XGBoost
Statistics & ML
Train, tune, score and explain gradient-boosted tree models on your tables with plain SQL
LightGBM
Statistics & ML
Train, tune, evaluate, and explain gradient-boosted classifiers and regressors, with a saved-model registry
scikit-learn
Statistics & ML
scikit-learn — datasets, metrics, transforms and a train/predict model registry