Bring your own attributions

Bring your own attributions

Store attributions you already compute, with TreeSHAP or anything else, next to the predictions they explain.

If you already compute attributions, with TreeSHAP, integrated gradients or anything else, store them in Kaimei so they sit next to the prediction, show in the dashboard with the same bars and feed the project's feature matrix. Pass attributions to POST /v1/explanations; Kaimei does not query the model, ranks the values into top_features and records the method as provided.

  • output and base_value are optional but make the record complete: the dashboard draws the waterfall from base_value to the output.
  • Values may be in any space your method uses, probabilities or log-odds; say which in metadata so readers know.
  • A model registered with inference.type = "none" and no structure can only be explained this way, which is a legitimate pattern for an audit trail.
import shap

explainer = shap.TreeExplainer(booster)
values = explainer.shap_values(frame.iloc[[0]])[0]          # log-odds contributions

client.explanations.create(
    model="credit-risk-xgb",
    input=frame.iloc[0].to_dict(),
    prediction_id="pred_8f2a19",
    attributions=dict(zip(frame.columns, map(float, values))),
    output={"label": "decline", "probability": 0.81},
    base_value=float(explainer.expected_value),
    metadata={"method": "treeshap", "space": "log_odds", "library": "shap==0.46"},
)
curl https://api.kaimei.ai/v1/explanations \
  -H "Authorization: Bearer $KAIMEI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "credit-risk-xgb",
    "input": {"income": 72000, "credit_score": 681, "utilization": 0.41, "num_delinquencies": 1},
    "prediction_id": "pred_8f2a19",
    "attributions": {"credit_score": -0.22, "utilization": 0.14, "income": -0.06, "num_delinquencies": 0.05},
    "output": {"label": "decline", "probability": 0.81},
    "base_value": 0.31,
    "metadata": {"method": "treeshap", "space": "log_odds"}
  }'
201 CreatedResponse
{
  "object": "explanation",
  "id": "exp_e9049ac910bdc382c0de",
  "model_id": "mdl_390bd6d9568f626851ef",
  "prediction_id": "pred_8f2a19",
  "method": "provided",
  "output": {"label": "decline", "probability": 0.81},
  "attributions": {
    "credit_score": -0.22,
    "utilization": 0.14,
    "income": -0.06,
    "num_delinquencies": 0.05
  },
  "base_value": 0.31,
  "top_features": [
    {
      "feature": "credit_score",
      "attribution": -0.22,
      "value": 681,
      "share": 0.4681,
      "direction": "negative",
      "rank": 1
    },
    {
      "feature": "utilization",
      "attribution": 0.14,
      "value": 0.41,
      "share": 0.2979,
      "direction": "positive",
      "rank": 2
    },
    …
  ],
  "counterfactual": null,
  "n_evals": 0,
  "latency_ms": 0.06,
  "metadata": {
    "method": "treeshap",
    "space": "log_odds",
    "api_key": "km_live_fPaB"
  },
  "created_at": "2026-10-31T22:40:23Z"
}