Score pairwise LLM-judge results with position swapping

Why it matters Position bias is one of the best known LLM-judge failure modes, and swap-and-agree is the standard mitigation.

LLM judges often favour whichever answer is shown first. A common fix is to judge each pair twice with the order swapped and count a win only if both orders agree.

Write swap_winrate(judgments). Each item is (v1, v2). v1 is the verdict when model X is shown as answer "A", and v2 the verdict when X is shown as answer "B"; each verdict is "A", "B" or "tie". X wins a pair if v1 == "A" and v2 == "B", loses if v1 == "B" and v2 == "A"; everything else is a tie. A pair is consistent if both verdicts prefer the same model or both are "tie". Return a dict with integer "win", "loss", "tie", and floats "win_rate" = (win + 0.5 * tie) / n and "consistency" = consistent pairs / n. For an empty list return zeros.

Signaturedef swap_winrate(judgments)

Example

swap_winrate([("A", "B"), ("A", "A"), ("tie", "tie"), ("B", "A")]) is {"win": 1, "loss": 1, "tie": 2, "win_rate": 0.5, "consistency": 0.75}.

Understand the idea

When an LLM grades two answers side by side, it tends to prefer whichever one it reads first. To cancel that, show the pair twice, once in each order, and only trust a verdict that survives the swap, like a taste test where the cups are swapped between rounds.

Example: round 1 shows X as A and the judge says "A" (X wins). Round 2 shows X as B; the judge says "B", which is X again. Consistent win. If round 2 had said "A", the judge simply liked the first slot both times: inconsistent, so it counts as a tie.

Rule: v2' = flip(v2); win if v1 = v2' = A; loss if v1 = v2' = B; tie otherwise; win_rate = (wins + 0.5 ties) / n; consistency = mean(v1 == v2').

Low consistency is a warning that the judge is unreliable for this pair of models. Arena-style and MT-Bench-style evaluations report position-swapped results for this reason.

solution.pyPython idle
Ctrl/Cmd + Enter runs sample tests

Run checks the first 2 tests. Submit checks all 4.

  • Sample 1: exampleswap_winrate([('A', 'B'), ('A', 'A'), ('tie', 'tie'), ('B', 'A')])
  • Sample 2: position biasswap_winrate([('A', 'A'), ('A', 'A'), ('A', 'B')])
  • Test 3: one-sided tieswap_winrate([('tie', 'B'), ('A', 'tie'), ('A', 'B')])
  • Test 4: emptyswap_winrate([])