Skip to main content

mllm-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 mllm-shap

Optional backends, estimators and visualization:

pip install "mllm-shap[transformers]"   # Hugging Face text backend
pip install "mllm-shap[lmstudio]"       # LM Studio SDK
pip install "mllm-shap[api]"            # OpenAI-compatible HTTP API
pip install "mllm-shap[viz]"            # matplotlib token charts
pip install "mllm-shap[proxy]"          # scikit-learn: the ProxySHAP estimator
pip install "mllm-shap[audio]"          # audio decoding and alignment
pip install "mllm-shap[semantic]"       # sentence, BERTScore and NLI encoders
pip install "mllm-shap[audio-semantic]" # SpeechBERTScore encoder and recognizer

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

Why mllm-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, Neyman, Leverage SHAP, Kernel Banzhaf and ProxySHAP estimators ship built in.

Quickstart

import math

from mllm_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
mllm-shap config --example > config.yaml
mllm-shap explain --config config.yaml --snapshot snapshot.json --out result.json
mllm-shap reanalyze --config config.yaml --archive runs/<run_id>   # rescore, no model calls
mllm-shap plugins

See the command-line guide.

Documentation

Full documentation: pawlo77.github.io/MLLM-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.

Citing

If you use mllm-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: "mllm-shap: Shapley-value explainability for conversational language models"
type: software
authors:
  - family-names: Pozorski
    given-names: Paweł
repository-code: "https://github.com/Pawlo77/MLLM-SHAP"
license: Apache-2.0

License

Apache License 2.0. See LICENSE.

Copyright 2026 Paweł Pozorski.

Metadata

Release files for mllm-shap 0.8.0

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

Source distribution (sdist)

Source distribution for mllm-shap 0.8.0
File Size Uploaded Provenance Actions
mllm_shap-0.8.0.tar.gz 145.8 kB Without provenance Details for mllm_shap-0.8.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mllm-shap 0.8.0
File Interpreter ABI Platform Actions
mllm_shap-0.8.0-py3-none-any.whl Python 3 none any Details for mllm_shap-0.8.0-py3-none-any.whl

Total release size: 354.7 kB

Release files / mllm_shap-0.8.0.tar.gz

Download URL mllm_shap-0.8.0.tar.gz
Size 145.8 kB
Tags Source
SHA-256 checksum
How to use checksums
260e98f216a56754c23d11376d4a7d2174018f8e737f719c6c72969cf17deb88
BLAKE2b-256 checksum
How to use checksums
24279cfc1b42e1759d9270725f44fadf94d99e1e64d4cb877db2a22b9722301e
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 / mllm_shap-0.8.0-py3-none-any.whl

Download URL mllm_shap-0.8.0-py3-none-any.whl
Size 208.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
be19db37d1abc87f48c1e7f1447b6bc3123ee421566958af12049526712ef466
BLAKE2b-256 checksum
How to use checksums
a72f074bb70af5f9f2be456117422c25b5b0feb992e92b763b6b9684100de191
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}

Security for mllm-shap 0.8.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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