High severity beginner · Fix: 2-5 min

ValueError

transformers.TrainerCallback.ValueError

What this error means
Huggingface TrainerCallback raises an error when the specified evaluation metric is missing from the Trainer's compute_metrics output.

Stack trace

traceback
ValueError: Evaluation metric 'accuracy' not found in the metrics returned by compute_metrics.
    at transformers.Trainer.evaluate()
QUICK FIX
Verify and correct your compute_metrics function to return all required metric keys matching those expected by TrainerCallback.

Why it happens

The Huggingface Trainer expects the compute_metrics function to return a dictionary containing the evaluation metrics specified in the TrainerCallback. If the metric key is missing or misspelled, the callback cannot find it and raises this error.

Detection

Check that your compute_metrics function returns a dictionary with all expected metric keys exactly matching those used in callbacks or logging before calling Trainer.evaluate().

Causes & fixes

1

compute_metrics function does not return the expected metric key

✓ Fix

Ensure your compute_metrics function returns a dictionary with the exact metric keys required by your TrainerCallback, e.g., {'accuracy': value}.

2

Mismatch between metric names used in TrainerCallback and those returned by compute_metrics

✓ Fix

Align the metric names in your TrainerCallback configuration with the keys returned by compute_metrics, respecting case sensitivity.

3

compute_metrics function is missing or not passed to Trainer

✓ Fix

Define and pass a valid compute_metrics function to the Trainer constructor to enable metric calculation during evaluation.

Code: broken vs fixed

Broken - triggers the error
python
from transformers import Trainer

def compute_metrics(eval_pred):
    logits, labels = eval_pred
    predictions = logits.argmax(axis=-1)
    # Missing 'accuracy' key in returned dict
    return {'acc': (predictions == labels).mean()}

trainer = Trainer(
    model=model,
    args=training_args,
    compute_metrics=compute_metrics
)
trainer.evaluate()  # This line triggers the error because 'accuracy' metric is not found
Fixed - works correctly
python
import os
from transformers import Trainer

def compute_metrics(eval_pred):
    logits, labels = eval_pred
    predictions = logits.argmax(axis=-1)
    # Fixed: return 'accuracy' key as expected
    return {'accuracy': (predictions == labels).mean()}

trainer = Trainer(
    model=model,
    args=training_args,
    compute_metrics=compute_metrics
)
trainer.evaluate()  # Fixed: no error, metric found
print('Evaluation completed successfully')
Changed the compute_metrics function to return the expected 'accuracy' key so TrainerCallback can find the metric during evaluation.

Workaround

Wrap the evaluation call in try/except ValueError, and if the metric is missing, log the raw output of compute_metrics to debug and manually extract metrics as a fallback.

Prevention

Always define and test your compute_metrics function to return all metrics expected by callbacks before training or evaluation to avoid runtime errors.

Python 3.7+ · transformers >=4.0.0 · tested on 4.30.0
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.