roboflow/supervision · error · ValueError
top_k could not be calculated, confidence is None
Error message
top_k could not be calculated, confidence is None
What it means
Raised by `Classifications.get_top_k(k)` when `self.confidence` is None. Ranking by confidence is impossible without confidence scores, so the method refuses rather than returning arbitrary ordering. `Classifications` allows `confidence=None` at construction, but `get_top_k` does not.
Source
Thrown at src/supervision/classification/core.py:209
Returns:
A tuple containing the top k class IDs and confidences.
Example:
```pycon
>>> import numpy as np
>>> import supervision as sv
>>> classifications = sv.Classifications(
... class_id=np.array([0, 1, 2]),
... confidence=np.array([0.3, 0.9, 0.5])
... )
>>> classifications.get_top_k(1)
(array([1]), array([0.9]))
```
"""
if self.confidence is None:
raise ValueError("top_k could not be calculated, confidence is None")
order = np.argsort(self.confidence)[::-1]
top_k_order = order[:k]
top_k_class_id = self.class_id[top_k_order]
top_k_confidence = self.confidence[top_k_order]
return top_k_class_id, top_k_confidence
View on GitHub (pinned to 7f254d9784)
Solutions
- Supply confidence at construction: pass a probability array aligned with class_id.
- If your model gives no scores, rank by another criterion yourself (e.g. class_id order) instead of calling `get_top_k`.
- Guard the call: `if classifications.confidence is not None: ... get_top_k(k)`.
Example fix
# before
classifications = sv.Classifications(class_id=np.array([0, 1, 2]))
classifications.get_top_k(1)
# after
classifications = sv.Classifications(
class_id=np.array([0, 1, 2]), confidence=np.array([0.3, 0.9, 0.5])
)
classifications.get_top_k(1) Defensive patterns
Strategy: validation
Validate before calling
if classifications.confidence is None:
# rank by class_id order instead, or skip
top = classifications.class_id[:k]
else:
top_id, top_conf = classifications.get_top_k(k) Type guard
def has_confidence(c: sv.Classifications) -> bool:
return c.confidence is not None Try / catch
try:
top_id, top_conf = classifications.get_top_k(k)
except ValueError:
top_id = classifications.class_id[:k]; top_conf = None Prevention
- Always pass confidence when constructing Classifications that will be ranked.
- Check `.confidence is not None` before get_top_k.
- If the model emits no probabilities, do not use get_top_k.
When it happens
Trigger: Calling `sv.Classifications(class_id=np.array([0, 1, 2])).get_top_k(1)` — no confidence argument supplied at construction; calling `get_top_k` on classifications produced by a classifier connector that does not emit confidences.
Common situations: Using a label-only classifier output (e.g. CLIP zero-shot labels without probabilities) and then trying to rank; code paths shared between models where one model omits confidence.
Related errors
- confidence must be 1d np.ndarray with (n, ) shape
- No edges defined for class_id={class_id}.
- KeyPoints detection_confidence must be given for NMS to be e
- Detections confidence must be given for NMS to be executed.
- Detections confidence must be given for Soft-NMS to be execu
AI-assisted analysis of roboflow/supervision@7f254d9784 (2026-08-15).
Data as JSON: /api/errors/6369dc9e57bd1862.
Report an issue: GitHub.