Evaluate Structured Extraction Field by Field

medium
Unit 13 · Evaluation I0/2 to milestone

An LLM extracts invoices into dicts like {"vendor": ..., "total": ..., "date": ...}. Whole-record exact match is too harsh: score each field.

Implement field_scores(predictions, gold, fields) for parallel lists of dicts:

  • Normalise values before comparing: strings are trimmed, lowercased and have internal whitespace collapsed; numbers (or numeric strings like "1,234.50") compare as floats within 1e-6. None and missing keys mean "not extracted".
  • For each field, count TP (predicted and correct), FP (predicted but wrong, or predicted when gold is empty) and FN (gold has a value but the prediction is missing or wrong).
  • Return {field: {"precision", "recall", "f1"}}, using 0.0 when a denominator is 0, plus a key "_record_exact_match": the fraction of records where every field matches (two missing values match).
#evaluation#structured-output#extraction2 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Evaluate Structured Extraction Field by Field · TechGradify