rohitg00/ai-engineering-from-scratch · error · ValueError

unknown metric_name

Error message

unknown metric_name: {metric_name}

What it means

score dispatches to exact_match, accuracy, f1, bleu_4, or rouge_l by metric_name; any other string falls through every if-branch to the generic guard, meaning the caller asked for a metric this module does not implement (likely a typo or a newer metric name).

Solutions

  1. Use one of the supported names: exact_match, accuracy, f1, bleu_4, rouge_l
  2. Add the new metric to the METRIC_AGGREGATION table and a matching branch before using it
  3. Validate metric names at task-spec load time rather than at scoring time
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at phases/19-capstone-projects/71-classical-metrics/code/main.py:159 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of rohitg00/ai-engineering-from-scratch@39ea8a1c6d (2026-08-26). Data as JSON: /api/errors/6d9822e5819a3fb5. Report an issue: GitHub.

Appendix: source

Thrown at phases/19-capstone-projects/71-classical-metrics/code/main.py:159

    "bleu_4": "per_target_max",
    "rouge_l": "per_target_max",
}


def score(metric_name: str, prediction: str, targets: list[str]) -> float:
    if not targets:
        return 0.0
    if metric_name == "exact_match":
        return exact_match(prediction, targets)
    if metric_name == "accuracy":
        return accuracy(prediction, targets)
    if metric_name == "f1":
        return max(f1_score(prediction, t) for t in targets)
    if metric_name == "bleu_4":
        return max(bleu4(prediction, t) for t in targets)
    if metric_name == "rouge_l":
        return max(rouge_l(prediction, t) for t in targets)
    raise ValueError(f"unknown metric_name: {metric_name}")


def corpus_mean(scores: list[float]) -> float:
    if not scores:
        return 0.0
    return float(np.mean(scores))


def corpus_bleu(predictions: list[str], references: list[str], max_n: int = 4) -> float:
    if len(predictions) != len(references):
        raise ValueError("predictions and references must align")
    if not predictions:
        return 0.0
    total_cand_len = 0
    total_ref_len = 0
    clipped_sums = [0] * max_n
    total_sums = [0] * max_n
    for cand_text, ref_text in zip(predictions, references):

View on GitHub (pinned to 39ea8a1c6d)