Skip to main content
Archived

This project has been archived by its maintainers, and is no longer receiving any updates.

nlp-shap

Shapley-value attributions for conversational language models.

Explicit estimands (Shapley vs Banzhaf), reproducible run archives, pluggable backends (Hugging Face, LM Studio, OpenAI-compatible APIs).

PyPI Pytest Prek Checks Codecov Python 3.12-3.14 Documentation License

Installation

pip install nlp-shap

Optional backends and visualization:

pip install "nlp-shap[transformers]"   # Hugging Face text backend
pip install "nlp-shap[lmstudio]"       # LM Studio SDK
pip install "nlp-shap[api]"            # OpenAI-compatible HTTP API
pip install "nlp-shap[viz]"            # matplotlib token charts

The core package does not install PyTorch. Python 3.12 or newer; uv for development.

Why nlp-shap

  • Honest estimands. Shapley and Banzhaf are separate, labelled aggregators; every result and archive records which one produced it, and results report base_value and grand_value so efficiency (sum(values) == v(N) - v(empty)) can be verified; axiom tests enforce it.
  • Reproducible runs. One validated configuration, a run id derived from it, and an archive of every model generation, so attributions can be recomputed with another value function without calling the model again.
  • Pluggable everything. Partitions, absence policies, estimators, value functions, backends and renderers are entry-point plugins; exact, Monte Carlo, complementary and Neyman estimators ship built in.

Quickstart

import math

from nlp_shap import (
    ConversationSnapshot,
    ExplainConfig,
    ExplainRunner,
    Message,
    Role,
    Turn,
)

snapshot = ConversationSnapshot.from_turns((
    Turn(messages=(Message(role=Role.USER, text="refund my order"),)),
))
config = ExplainConfig.model_validate({
    "backend": {"kind": "mock", "model_id": "stub"},
    "explanation": {"estimator": "exact", "value_fn": "tfidf_cosine"},
})
result = ExplainRunner(config).explain_sync(snapshot).result

print(result.values)  # one attribution per token
print(result.base_value)  # v(empty): payoff of the fully masked prompt
print(result.grand_value)  # v(N): payoff of the full prompt
# Efficiency: Shapley values sum to v(N) - v(empty).
assert math.isclose(sum(result.values), result.grand_value - result.base_value)

The mock backend needs no model; swap backend.kind for transformers, lmstudio or api to explain a real model. Runs are archived under ./runs/{run_id}; running the same configuration twice raises ArchiveExistsError unless explanation.archive.on_exists is overwrite (or archive.path is "").

Command line

The same run without Python: write a snapshot as JSON, start from the commented config skeleton, and get the result as JSON (--render token_bar --png out.png also saves a chart).

echo '{"turns": [{"messages": [{"role": "user", "text": "refund my order"}]}]}' > snapshot.json
nlp-shap config --example > config.yaml
nlp-shap explain --config config.yaml --snapshot snapshot.json --out result.json
nlp-shap reanalyze --config config.yaml --archive runs/<run_id>   # rescore, no model calls
nlp-shap plugins

See the command-line guide.

Documentation

Full documentation: pawlo77.github.io/nlp-shap

Examples, vault and development

  • Runnable notebooks: examples/, also embedded in the docs.
  • Architecture notes, decisions, known issues and papers: the Obsidian vault in knowledge/.
  • Development setup, checks and pull requests: CONTRIBUTING.md; coding-agent rules: AGENTS.md.

Citing

If you use nlp-shap in research, please cite it (CITATION.cff; GitHub's "Cite this repository" button renders it as APA or BibTeX):

cff-version: 1.2.0
title: "nlp-shap: Shapley-value explainability for conversational language models"
type: software
authors:
  - family-names: Pozorski
    given-names: Paweł
repository-code: "https://github.com/Pawlo77/nlp-shap"
license: Apache-2.0

License

Apache License 2.0. See LICENSE.

Copyright 2026 Paweł Pozorski.

Metadata

Release files for nlp-shap 0.1.18

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nlp-shap 0.1.18
File Size Uploaded
nlp_shap-0.1.18.tar.gz 95.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for nlp-shap 0.1.18
File Interpreter ABI Platform
nlp_shap-0.1.18-py3-none-any.whl Python 3 none any Details

Total release size: 236.3 kB

Release files / nlp_shap-0.1.18.tar.gz

Download URL nlp_shap-0.1.18.tar.gz
Size 95.8 kB
Tags Source
SHA-256 checksum
How to use checksums
294c98240cf772517f1d2b191a868371566d95e8622dc587697321328fcbf03c
BLAKE2b-256 checksum
How to use checksums
45e3cc77d011778a0d93fc3b29e67a7b8e3a97365455bfb1da3cae67c4c356a0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / nlp_shap-0.1.18-py3-none-any.whl

Download URL nlp_shap-0.1.18-py3-none-any.whl
Size 140.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
18395d4d5d2faef6c94d24273e3567444441e589426b893b3e2c9002a1f7607d
BLAKE2b-256 checksum
How to use checksums
4c9b2c17db1746057094605500fc5f54067dadef8da30332585b1e215e0c63a0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page