EasyEdit icon indicating copy to clipboard operation
EasyEdit copied to clipboard

normalize_answer's article/punctuation stripping lets degenerate replies pass exact_match_score in the LLM-judge no-api_key fallback

Open shaurya416 opened this issue 2 months ago • 1 comments

easyeditor/evaluate/evaluate_utils.py:19-36 (normalize_answer, exact_match_score), reached from the fallback at evaluate_utils.py:170 inside test_prediction_acc_LLM_judge, which is called from evaluate.py:124 (compute_rewrite_or_rephrase_quality) and evaluate.py:213 (compute_portability_quality) whenever hparams.evaluation_type == "LLM-judge" and hparams.api_key is falsy.

def normalize_answer(s):
    def remove_articles(text):
        return regex.sub(r'\b(a|an|the)\b', ' ', text)
    def white_space_fix(text):
        return ' '.join(text.split())
    def remove_punc(text):
        exclude = set(string.punctuation)
        return ''.join(ch for ch in text if ch not in exclude)
    def lower(text):
        return text.lower()
    return white_space_fix(remove_articles(remove_punc(lower(s))))

def exact_match_score(prediction, ground_truth):
    return normalize_answer(prediction) == normalize_answer(ground_truth)
# evaluate_utils.py:166-171, inside test_prediction_acc_LLM_judge
for prompt, target, gen_content in zip(prompts, targets, all_response):
    if hasattr(hparams, 'api_key') and hparams.api_key:
        score = llm_judge(prompt, target, gen_content, hparams.api_key)
    else:
        score = float(exact_match_score(gen_content, target))

What happens (reproduced on the real module)

exact_match_score's article/punctuation stripping lets degenerate replies pass against short gold answers, in the unguarded LLM-judge-without-api_key fallback.

Where it is reached. The fallback is at evaluate_utils.py:170 inside test_prediction_acc_LLM_judge (def line 156), using normalize_answer/exact_match_score at evaluate_utils.py:19-36. It is reached from evaluate.py:124 (compute_rewrite_or_rephrase_quality) and evaluate.py:213 (compute_portability_quality) whenever hparams.evaluation_type=='LLM-judge' and hparams.api_key is falsy/unset. This is an easy misconfiguration, since no HyperParams class in the repo sets api_key by default.

Mechanism. remove_articles + white_space_fix collapse any gold answer that reduces to a bare article (or is itself blank/punctuation) to the empty string. Any blank/punctuation-only/whitespace-only model generation also collapses to '', so '' == '' reports a match.

Reproduction. Reproduced exactly against the real code (4/4 defect cases True, 2/2 controls correct), with stdlib re substituted 1:1 for the third-party regex package.

History. This predates PR #702 (2026-06-24/07-03): that PR's diff shows the fallback branch ('the user do not provide api key, using exact match as an alternative') was carried over unchanged through the refactor. Nothing since (current HEAD 2026-07-14) has touched evaluate_utils.py, and there is no test suite or CI covering this file.

Labelling.

  • For the compute_rewrite_or_rephrase_quality path (evaluate.py:124), metric_meta.py's build_lm_metric_meta (wired via attach_metric_meta at evaluate.py:165) tags the returned result with protocol free_generation_exact_match_fallback when no api_key is present, so that call site is not entirely unlabeled internally. The tag only marks which scorer ran; it does not change or guard the scoring, so the false positive still occurs.
  • compute_portability_quality (evaluate.py:213) has no such tagging: it returns before any metric_meta call, so portability accuracy is fully silent.

Suggested fix. Guard the empty-normalization case, e.g. normalize_answer(prediction)=='' or normalize_answer(ground_truth)=='' => False, in exact_match_score/normalize_answer.

Happy to open the PR.

shaurya416 avatar Sep 24 '26 03:09 shaurya416

Hi, thanks for tracing this through the evaluation code so carefully. You're right: without an API key, an answer like the and a punctuation-only reply can both normalize to an empty string, which makes exact_match_score count them as a match.

We've put a small fix in #717. If either answer becomes empty after normalization, it now counts as a non-match; other answers keep the existing exact-match behavior. Thanks again for catching this!

TengJiao33 avatar Sep 24 '26 07:09 TengJiao33