# UQLM

> A Python library from CVS Health that detects LLM hallucinations with uncertainty quantification, giving every response a confidence score between 0 and 1

- Category: Evaluation & Production Ops / Evaluation & Red-Teaming
- Language: Python
- Licence: Apache-2.0
- Repository: https://github.com/cvs-health/uqlm
- Website: https://cvs-health.github.io/uqlm/
- Canonical page: https://ai-tldr.dev/tools/uqlm/

## In short

UQLM (Uncertainty Quantification for Language Models) is an open-source Python library, published by CVS Health, for detecting hallucinations in large language model output. It sits in the Evaluation & Red-Teaming category of the Evaluation & Production Ops tools and is open-source, written in Python and licensed Apache-2.0. To get started: install.

## Overview

UQLM (Uncertainty Quantification for Language Models) is an open-source Python library, published by CVS Health, for detecting hallucinations in large language model output. Instead of comparing an answer to a ground truth you rarely have in production, it estimates how uncertain the model was: each scorer returns a confidence score between 0 and 1, where a higher score means a lower likelihood of errors or hallucinations.

The scorers come in five families with different cost and compatibility trade-offs. Black-box scorers sample several responses to the same prompt and measure how consistent they are, so they work with any LLM. White-box scorers read token probabilities from a single generation, adding almost no latency or cost but requiring a model that exposes logprobs. LLM-as-a-judge scorers ask one or more judge models to grade the answer. Ensemble scorers combine the others by weighted averaging, either off the shelf or tuned on prompts with known answers. Long-text scorers work at the claim or sentence level and can rewrite a response by dropping low-confidence claims. A separate `CodeGenUQ` interface predicts whether generated code is functionally correct without running it.

Every class takes a LangChain chat model, so the same code runs against OpenAI, Azure OpenAI, Vertex AI, Ollama or any other LangChain-supported provider. The library is Apache-2.0 licensed, installs from PyPI, and is described in a paper in the Journal of Machine Learning Research (2026); the scorer methods and benchmarks are covered in companion papers in TMLR and at EMNLP 2026.

## What it does

- Black-box consistency scorers — discrete semantic entropy, number of semantic sets, non-contradiction and entailment probability, exact match, BERTScore and cosine similarity — that work with any LLM
- White-box scorers from token probabilities, including minimum token probability, length-normalized sequence probability and probability margin, plus P(True) self-reflection and multi-generation methods such as CoCoA and semantic density
- LLM-as-a-judge scoring with categorical, continuous or Likert-scale judges, or a panel of several judge models via `LLMPanel`
- `UQEnsemble` combines scorers with fixed weights or tunes the weights on prompts with ground-truth answers
- `LongTextUQ` scores each claim in a long answer and can refine the response by removing claims below a confidence threshold
- `CodeGenUQ` predicts whether LLM-generated code is functionally correct without executing it

## Getting started

UQLM is a pip-installable Python package. Each scorer class wraps a LangChain chat model and exposes an async `generate_and_score` method that returns results you can turn into a DataFrame; `prompts` below is your own list of prompt strings.

1. Install

   The package is published on PyPI and supports Python 3.10 and up.

```bash
pip install uqlm
```

2. Score responses with a black-box scorer

   `BlackBoxUQ` samples several responses per prompt and measures their consistency. `use_best=True` returns the uncertainty-minimized response as the answer.

```python
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o-mini")

from uqlm import BlackBoxUQ
bbuq = BlackBoxUQ(llm=llm, scorers=["semantic_negentropy"], use_best=True)

results = await bbuq.generate_and_score(prompts=prompts, num_responses=5)
results.to_df()
```

3. Use token probabilities when your model exposes them

   `WhiteBoxUQ` scores a single generation from its token probabilities, with no extra LLM calls for the single-generation scorers.

```python
from langchain_google_vertexai import ChatVertexAI
llm = ChatVertexAI(model='gemini-2.5-pro')

from uqlm import WhiteBoxUQ
wbuq = WhiteBoxUQ(llm=llm, scorers=["min_probability"])

results = await wbuq.generate_and_score(prompts=prompts)
results.to_df()
```

4. Grade answers with a panel of LLM judges

   `LLMPanel` sends each question-answer pair to several judge models and aggregates their verdicts into one confidence score.

```python
from langchain_ollama import ChatOllama
llama = ChatOllama(model="llama3")
mistral = ChatOllama(model="mistral")
qwen = ChatOllama(model="qwen3")

from uqlm import LLMPanel
panel = LLMPanel(llm=llama, judges=[llama, mistral, qwen])

results = await panel.generate_and_score(prompts=prompts)
results.to_df()
```

5. Tune an ensemble on labelled prompts

   `UQEnsemble` mixes black-box, white-box and judge scorers. Use it off the shelf, or tune its weights on prompts with known answers before scoring new ones.

```python
from uqlm import UQEnsemble
scorers = [
    "exact_match", "noncontradiction",  # black-box scorers
    "min_probability",  # white-box scorer
    llm  # use same LLM as a judge
]
uqe = UQEnsemble(llm=llm, scorers=scorers)

tune_results = await uqe.tune(
    prompts=tuning_prompts, ground_truth_answers=ground_truth_answers
)
results = await uqe.generate_and_score(prompts=prompts)
results.to_df()
```

6. Score long answers claim by claim

   `LongTextUQ` splits each response into claims, scores every claim against sampled responses, and with `response_refinement=True` rebuilds the answer from the claims it keeps.

```python
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o")

from uqlm import LongTextUQ
luq = LongTextUQ(llm=llm, scorers=["entailment"], response_refinement=True)

results = await luq.generate_and_score(prompts=prompts, num_responses=5)
results_df = results.to_df()
```

## When to use it

- Flag low-confidence answers in a production LLM app so they can be withheld, routed to a human or regenerated
- Pick the most self-consistent of several sampled responses instead of trusting a single generation
- Strip unsupported claims from long-form answers such as summaries or biographies before they reach a reader
- Check generated code for likely functional errors without running it, and compare UQ methods on your own data for research
