Metadata-Version: 2.4
Name: pytest-llm-confidence
Version: 0.1.0
Summary: pytest plugin that fails a test when a language model wasn't sure of its answer. Reads token probabilities: no judge model, no extra API cost, deterministic.
Author-email: Proprioceptive AI <logan@proprioceptiveai.com>
License-Expression: MIT
Project-URL: Homepage, https://proprioceptiveai.com/products.html
Project-URL: Repository, https://github.com/Loganwins/proprioceptive-confidence
Project-URL: Issues, https://github.com/Loganwins/proprioceptive-confidence/issues
Project-URL: Proprioceptive Lens, https://pypi.org/project/proprioceptive-lens/
Keywords: pytest,llm,testing,evals,confidence,logprobs,hallucination,ollama,ci
Classifier: Development Status :: 4 - Beta
Classifier: Framework :: Pytest
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Operating System :: OS Independent
Classifier: Topic :: Software Development :: Testing
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: pytest>=7
Requires-Dist: proprioceptive-confidence>=0.1.0
Dynamic: license-file

# 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
```

```python
# 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.

```python
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) |

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.

## 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.

## Related

- **proprioceptive-confidence**: the library this plugin uses, with LangChain and LiteLLM adapters.
- **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](https://proprioceptiveai.com).
