huggingface/transformers · error · ValueError

No valid predictions

Error message

No valid predictions

What it means

Raised by squad_metrics.compute_predictions_logits when the nbest list is empty after the nonce-prediction fallbacks. Normally the code inserts an 'empty' prediction when nbest is empty, making this branch nearly unreachable in practice; hitting it means every candidate start/end pair failed to map back to valid text (e.g. get_final_text produced nothing usable for any candidate) and even the fallbacks did not append.

Source

Thrown at src/transformers/data/metrics/squad_metrics.py:539

            nbest.append(_NbestPrediction(text=final_text, start_logit=pred.start_logit, end_logit=pred.end_logit))
        # if we didn't include the empty option in the n-best, include it
        if version_2_with_negative:
            if "" not in seen_predictions:
                nbest.append(_NbestPrediction(text="", start_logit=null_start_logit, end_logit=null_end_logit))

            # In very rare edge cases we could only have single null prediction.
            # So we just create a nonce prediction in this case to avoid failure.
            if len(nbest) == 1:
                nbest.insert(0, _NbestPrediction(text="empty", start_logit=0.0, end_logit=0.0))

        # In very rare edge cases we could have no valid predictions. So we
        # just create a nonce prediction in this case to avoid failure.
        if not nbest:
            nbest.append(_NbestPrediction(text="empty", start_logit=0.0, end_logit=0.0))

        if len(nbest) < 1:
            raise ValueError("No valid predictions")

        total_scores = []
        best_non_null_entry = None
        for entry in nbest:
            total_scores.append(entry.start_logit + entry.end_logit)
            if not best_non_null_entry:
                if entry.text:
                    best_non_null_entry = entry

        probs = _compute_softmax(total_scores)

        nbest_json = []
        for i, entry in enumerate(nbest):
            output = collections.OrderedDict()
            output["text"] = entry.text
            output["probability"] = probs[i]
            output["start_logit"] = entry.start_logit
            output["end_logit"] = entry.end_logit

View on GitHub (pinned to a597f97485)

Solutions

  1. Regenerate features and predictions with the same tokenizer, max_seq_length, and doc_stride so example/feature/result indices align.
  2. Check that SquadResult objects correspond feature-for-feature to the features passed in (same unique_id ordering).
  3. Inspect the failing qas_id: log its features' token_to_orig_map to confirm offsets are populated (requires a fast tokenizer during preprocessing).
Defensive patterns

Strategy: try-catch

Validate before calling

qas_ids = {ex.qas_id for ex in all_examples}
result_ids = {r.unique_id for r in all_results}
feature_ids = {f.unique_id for f in all_features}
assert result_ids <= feature_ids, 'results reference features that were not provided'

Try / catch

try:
    predictions = compute_predictions_logits(examples, features, results, ...)
except ValueError as e:
    if 'No valid predictions' in str(e):
        logger.error('feature/result misalignment for at least one example; regenerate both with same tokenizer/settings')
    raise

Prevention

When it happens

Trigger: Calling compute_predictions_logits with all_examples/features/results where, for some example, every predicted span fails text mapping (misaligned offsets between tokenizer features and original context, or features/results indexed by the wrong example).

Common situations: Mixing features from one preprocessing run with results from another (different max_seq_length or doc_stride); tokenizers whose offset mapping does not match the original examples; corrupted results files.

Related errors


AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14). Data as JSON: /api/errors/51185377f4c60ee2. Report an issue: GitHub.