Skip to main content

pytest-llm-confidence

Fail a test when the model wasn't sure of its answer.

Most LLM test tools ask a second model to judge the first, which costs money, adds latency and changes from run to run. pytest-llm-confidence reads the model's own token probabilities instead: no judge model, no extra API cost, and the same response always gets the same verdict.

$ pip install pytest-llm-confidence
# test_answers.py
def test_capital(llm_confidence):
    ans = llm_confidence.ask("What is the capital of Australia? Answer with one word.")
    assert ans.text == "Canberra"
    llm_confidence.assert_confident(ans)          # fails if any word was below 80%
$ pytest
...
FAILED test_answers.py::test_capital - ShakyAnswerError: model wasn't sure of its answer
  (shaky: 1 word(s) below 80%: "Syd" at 41%)
answer: 'Sydney'
  "Syd" at 41%   (alternatives: "Can" 38%)

============================ LLM confidence ============================
SHAKY test_answers.py::test_capital  lowest "Syd" at 41%
1 check(s), 1 shaky

(Illustrative output; real numbers depend on your model.)

Works with your model

  • Ollama 0.12+ (default): llm_confidence.ask talks to http://127.0.0.1:11434.
  • Any OpenAI-compatible server that returns logprobs: OpenAI, vLLM, llama.cpp, LM Studio: pytest --llm-confidence-api openai --llm-confidence-base-url http://127.0.0.1:8000/v1 --llm-confidence-model my-model
  • Responses you already have: assert_confident(response) accepts OpenAI-style responses requested with logprobs=True, Ollama responses requested with "logprobs": true, or (token, logprob) pairs.
from pytest_llm_confidence import assert_confident

def test_my_pipeline():
    response = my_app.call_model("...", logprobs=True)
    assert_confident(response, threshold=0.9)

Settings

Option ini key Default
--llm-confidence-threshold llm_confidence_threshold 0.8
--llm-confidence-model llm_confidence_model llama3.2
--llm-confidence-api llm_confidence_api ollama
--llm-confidence-base-url llm_confidence_base_url $OLLAMA_HOST or http://127.0.0.1:11434
--llm-confidence-double-check llm_confidence_double_check off
--llm-confidence-strict llm_confidence_strict off (skip when the server is down)
--llm-confidence-report PATH llm_confidence_report none (writes JSON for CI artifacts)
--llm-confidence-baseline PATH llm_confidence_baseline none (see below)
--llm-confidence-update-baseline llm_confidence_update_baseline off
--llm-confidence-baseline-tolerance llm_confidence_baseline_tolerance 0.10

Per test: @pytest.mark.llm_confidence(threshold=0.95, double_check=True).

CI-safe by default: if the model server can't be reached, llm_confidence.ask skips the test instead of failing it. Pass --llm-confidence-strict to make that a failure.

Double-check: with double_check=True, a shaky short answer is re-asked in full and ans.double_check.agrees tells you whether the answer held up.

Catch confidence drops with a baseline

A model upgrade, a different quantization or a prompt change can leave answers right but much less sure, and a plain correctness test won't notice. Save a baseline once, commit it, and compare every run against it:

$ pytest --llm-confidence-baseline llm-baseline.json --llm-confidence-update-baseline   # save
$ pytest --llm-confidence-baseline llm-baseline.json                                     # compare
...
FAILED test_answers.py::test_capital - llm-confidence: confidence dropped vs the baseline (check 1):
  lowest word 98% -> 61% (37 points, tolerance 10), now "Paris"
  baseline answer: 'Paris' from llama3.2
  answer now:      'Paris' from llama3.2:1b

============================ LLM confidence ============================
DROP  test_answers.py::test_capital  lowest "Paris" at 61%  (baseline 98%, -37 points)
baseline: 3 compared, 1 dropped more than 10 points, 0 new

(Illustrative output.) A test fails when one of its checks falls more than 10 points below its baseline; set --llm-confidence-baseline-tolerance 0.2 for 20. New checks are listed, not failed. When you accept the new levels, run once more with --llm-confidence-update-baseline; checks from tests that didn't run stay in the file.

Testing without a model

proprioceptive_confidence.testing.FakeModelServer serves scripted token probabilities over Ollama's and OpenAI's APIs, so you can test your confidence handling in CI without a GPU.

What it can't do

A model can be wrong and certain. This plugin catches shaky answers, not confident mistakes, so keep your correctness assertions too.

  • proprioceptive-confidence: the library this plugin uses, with LangChain, LiteLLM and Inspect adapters and proprioconf calibrate, which tells you which threshold to use for your model.
  • LLM Confidence Check: a GitHub Action that runs this plugin in CI against a local Ollama model.
  • proprioceptive-mcp: let Claude Code, Cursor and other agents ask your local model and see how sure it was.
  • Proprioceptive Lens: see every word's confidence live in a local app: pip install proprioceptive-lens.

MIT licensed. From Proprioceptive AI.

Metadata

Release files for pytest-llm-confidence 0.2.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 pytest-llm-confidence 0.2.0
File Size Uploaded Provenance Actions
pytest_llm_confidence-0.2.0.tar.gz 13.1 kB With provenance Details for pytest_llm_confidence-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pytest-llm-confidence 0.2.0
File Interpreter ABI Platform Actions
pytest_llm_confidence-0.2.0-py3-none-any.whl Python 3 none any Details for pytest_llm_confidence-0.2.0-py3-none-any.whl

Total release size: 23.0 kB

Release files / pytest_llm_confidence-0.2.0.tar.gz

Download URL pytest_llm_confidence-0.2.0.tar.gz
Size 13.1 kB
Tags Source
SHA-256 checksum
How to use checksums
4806f6687bb8f0e9e7b768c1beff7a8184f567b5bafa1bf7dfef1fbe4ff6e38e
BLAKE2b-256 checksum
How to use checksums
1994e031bf1714efc09cd02b7498d49e43172b3edf7bc20a79fcf26b2486bd80
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 4, 2026.

Transparency log

Release files / pytest_llm_confidence-0.2.0-py3-none-any.whl

Download URL pytest_llm_confidence-0.2.0-py3-none-any.whl
Size 10.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
06bcbf429f1599ae4ec08a3125f8dfb4b980a95091b3c50167c2ce706dfa474b
BLAKE2b-256 checksum
How to use checksums
77b701cd3f6719d13ca7ee71196e3c82562e2790a5021d1d9ff20ccacb7a55fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 4, 2026.

Transparency log

Security for pytest-llm-confidence 0.2.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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pytest_llm_confidence-0.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 4, 2026.

Transparency log

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.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files Full provenance

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