Ahmed Doghri Logo Image
Ahmed Doghri

semanticentropy

A model that knows the answer says the same thing five different ways. A model that's guessing contradicts itself with the exact same unbothered confidence either time. This turns that instability into a number that predicts a real model's wrong answers at 0.878 AUROC, with no labels and no second model babysitting the first.

semanticentropy, a hallucination detector using semantic entropy

A Model That Knows the Answer Says It the Same Way Five Times

Here is a fact worth sitting with. A model that actually knows the answer says the same thing every time you ask, worded differently. A model that is confabulating gives you a different name, date, or number each time, with identical confidence in its voice. That instability is not a vibe. It is measurable, and it costs nothing extra beyond samples you probably already needed.

This is the method from Farquhar et al., published in Nature in 2024: sample the model several times at temperature, cluster the samples by meaning instead of by exact string, and measure entropy over the clusters. One cluster means consistent. Several clusters, roughly even, means the model is guessing.

Clustering by Meaning Is the Whole Trick

"Paris" and "It is Paris" are the same answer said two different ways by someone in a hurry. Count them as two distinct answers and you smear the signal until hallucinations stop looking any different from confidence, which defeats the whole point. semanticentropy clusters by content using a transparent containment check: the smaller answer's core content just needs to be mostly present in the larger one, and numbers are normalized so "300,000" and "300000" tokenize the same.

The Number

Two benchmarks, one honest and one real. On the zero-dependency synthetic benchmark, consistent answers average 0.08 normalized entropy and hallucinations average 0.90, a 0.81 gap. Then the real thing: Qwen2.5-0.5B answers 40 trivia questions, five samples each, clustered by actual NLI bidirectional entailment exactly as the paper prescribes. Semantic entropy predicts which greedy answers were wrong at 0.878 AUROC, knowing nothing about the gold labels. Wrong answers averaged 0.83 entropy, correct ones 0.25.

The model's confabulations are exactly the fun kind: it is confident J.D. Salinger won the 1976 Nobel and that Verdi wrote The Magic Flute, and the five scattered samples give it away every time.

A Confident Hedge Can Hide a Hallucination From the Containment Check

The containment check is a coverage ratio: the smaller answer's content words just need to mostly appear in the larger one. Fine for "Paris" versus "It is Paris." It breaks the moment a model hedges at length, because the shared boilerplate swamps the one word that actually differs.

I wrote five samples the way a hedging model actually talks: "Based on my knowledge and available historical records, the composer credited with this work was likely Verdi," then Mozart, then Rossini, Puccini, Wagner, same wrapper, five different composers. Fed straight into the detector: 0.00 normalized entropy, "consistent." Eleven of twelve content words are the shared hedge, so containment scores 0.909 coverage between any pair, comfortably past the 0.6 threshold. Five contradictory answers, called consistent. That is exactly the failure mode, fluent disagreement dressed in confident language, that semantic entropy exists to catch, and it walked straight through.

The fix is one narrow rule stacked on top of containment, not a replacement for it: if each answer has exactly one salient token, a word capitalized outside the sentence-initial position, or a number, and the two differ, the answers are never equivalent, no matter how much hedge text surrounds them. A capitalized mid-sentence word or a number is almost always the actual named answer. Requiring exactly one on each side keeps the rule from firing on answers that legitimately reference more than one entity. Caught a real bug while building it too: Python's str.isdigit() returns False for "2.3", so decimal-valued answers were silently falling out of the salience check until I swapped in a proper numeric pattern.

0% to 100% recall on four hand-written hedge patterns, confirmed on a three-group holdout evaluated exactly once, with zero regression on the original ten-question corpus or the short paraphrase cases containment already handled correctly. The original clustering is untouched, so the published containment behavior and the 0.878 AUROC real-model result are unaffected; the fix is opt-in through the exact same equivalence seam the NLI backend already uses.

29 tests pass in CI across Python 3.9, 3.11, and 3.13, with the NLI backend as an opt-in extra so the core stays dependency-free.

Tools Used

Python
Hallucination Detection
Uncertainty Estimation
Adversarial Testing
pytest
Ruff