Skip to content
mentoor

Topic 1 of 5 · 4 min read

Exact and normalized answer matching

Goal: Write a normalized match check for a classification or extraction task and name a case where exact match gives a false fail.

An exact-match grader compares the model's answer to a known right answer after you strip away differences that do not matter. It is the cheapest grader there is: no model call, no cost, the same verdict every run.

Check yourself · 1 of 3

A ticket classifier returns "Billing." and the key is "billing". Plain string equality marks it wrong. What is the right fix in the grader?

Choose one answer

Why it matters

Many production tasks have one correct answer: a ticket label, an invoice total, a country code, a yes or no. For these, a code grader is faster and more reliable than asking another model. It also never drifts. If the score moves, the system under test moved.

How to do it

  1. Fix the answer format in the prompt. Ask for one label from a list, or a JSON field. Free-form sentences cannot be matched.
  2. Normalize both sides, never only one. Lowercase, trim whitespace, strip trailing punctuation and quotes, and unify unicode forms.
  3. Parse before you compare. Turn "$1,204.50" into a number and compare numbers, with a tolerance if rounding is allowed. Compare dates as dates.
  4. Use sets for lists. If order does not matter, compare sorted or set versions.
  5. Keep the raw output. Store it beside the verdict so a human can see why a case failed.

Before and after

# Weak: fails on "Billing", "billing." and " billing\n"
def grade(output, expected):
    return output == expected
import re

def norm(s: str) -> str:
    return re.sub(r"[^a-z0-9_ ]", "", s.strip().lower())

def grade(output: str, expected: str) -> bool:
    return norm(output) == norm(expected)

The second version scores all three spellings as billing. Normalization encodes what you decided counts as the same answer.

What goes wrong

  • Over-normalizing. Stripping all punctuation makes "10.5" and "105" equal. Normalize only what you can defend.
  • False fails. "The category is billing" is a right answer that exact match rejects. Either tighten the prompt or extract the label first, and say which in the grader.
  • False passes by substring. Checking expected in output passes "not billing" and "billing or shipping". Match the whole value.
  • Single-answer assumption. If two labels are both defensible, exact match scores a good answer as wrong. Review the failing cases by hand before you trust the number.

Read the first twenty failures every time you add a normalizer. Most of them show you a rule you forgot or an answer key that is wrong.

Examples

Money amounts

The task extracts the total from an invoice. The key says 1204.5; the model returns "$1,204.50".

from decimal import Decimal

def to_amount(s: str) -> Decimal:
    return Decimal(s.replace("$", "").replace(",", ""))

def grade(out: str, key: str) -> bool:
    return abs(to_amount(out) - to_amount(key)) < Decimal("0.005")

String equality would fail this. Comparing parsed decimals passes it and still rejects 1240.50.

Label set with a fallback

A router must answer one of billing, shipping, other. The grader accepts a normalized match and records any output outside the set as invalid, a separate bucket from wrong. Then a drop from 94 to 80 passing can be traced to either bad labels or off-format answers.

Key points

  • Exact match is deterministic, free and the right first choice when a task has one correct answer.
  • Normalize both the output and the expected value in the same way, and parse numbers and dates before comparing.
  • Compare whole values: a substring check passes answers such as "not billing".
  • Every normalizer encodes a decision about what counts as the same answer, so keep it narrow and documented.
  • Read the failing cases by hand to find false fails caused by the grader or by a wrong answer key.

Common mistakes

  • Normalizing only the model output and leaving the key in its original form.
  • Using a substring or startswith check and passing answers with extra, contradicting text.
  • Stripping so much that different answers become equal.
  • Never reading the failures, so a wrong answer key counts against the model for months.

Check yourself · 2 of 3

A grader passes when expected in output. Which answer does it wrongly accept for the key "billing"?

Choose one answer

Check yourself · 3 of 3

The key for an invoice total is 1204.5 and the model answers "$1,204.50". What should the grader do?

Choose one answer

Practice items, not counted towards your mastery.

Start here