roboflow/supervision · error · ValueError
confidence must be 1d np.ndarray with (n, ) shape
Error message
confidence must be 1d np.ndarray with (n, ) shape
What it means
Raised by `sv.Classifications.__post_init__` when `confidence` is provided but is not a 1-D np.ndarray whose length equals `len(class_id)`. Confidence is optional (may be None), but when present it must align one-to-one with `class_id` so `get_top_k` and annotators can index both consistently.
Source
Thrown at src/supervision/classification/core.py:29
def _validate_class_ids(class_id: Any, n: int) -> None:
"""
Ensure that class_id is a 1d np.ndarray with (n, ) shape.
"""
is_valid = isinstance(class_id, np.ndarray) and class_id.shape == (n,)
if not is_valid:
raise ValueError("class_id must be 1d np.ndarray with (n, ) shape")
def _validate_confidence(confidence: Any, n: int) -> None:
"""
Ensure that confidence is a 1d np.ndarray with (n, ) shape.
"""
if confidence is not None:
is_valid = isinstance(confidence, np.ndarray) and confidence.shape == (n,)
if not is_valid:
raise ValueError("confidence must be 1d np.ndarray with (n, ) shape")
@dataclass
class Classifications:
class_id: npt.NDArray[np.int_]
confidence: npt.NDArray[np.floating] | None = None
def __post_init__(self) -> None:
"""
Validate the classification inputs.
"""
n = len(self.class_id)
_validate_class_ids(self.class_id, n)
_validate_confidence(self.confidence, n)
def __eq__(self, other: object) -> bool:
"""View on GitHub (pinned to 7f254d9784)
Solutions
- Ensure `len(confidence) == len(class_id)` and convert with `np.asarray(confidence)`.
- When filtering classifications, apply the same mask to both arrays.
- For score matrices, pass a 1-D slice: `scores.max(axis=1)` or the selected class scores.
Example fix
# before sv.Classifications(class_id=np.array([0, 1, 2]), confidence=np.array([0.9])) # after sv.Classifications(class_id=np.array([0, 1, 2]), confidence=np.array([0.9, 0.5, 0.1]))
Defensive patterns
Strategy: validation
Validate before calling
class_id = np.asarray(class_id)
if confidence is not None:
confidence = np.asarray(confidence)
assert confidence.shape == class_id.shape, 'confidence must match class_id length' Type guard
def confidence_aligned(confidence: Any, n: int) -> bool:
return confidence is None or (
isinstance(confidence, np.ndarray) and confidence.shape == (n,)
) Prevention
- Filter class_id and confidence with the same mask.
- Extract 1-D score columns from score matrices before passing.
- Construct both arrays in one place so they share a source of truth.
When it happens
Trigger: Passing `confidence=[0.9, 0.8]` (list, 2 items) with `class_id` of length 3; passing a confidence array from a previous inference run against a filtered class_id list; passing a 2-D scores array without selecting a column.
Common situations: Top-k filtering of class ids without filtering confidences; slicing one array and not the other after NMS; model score matrices where `scores[:, 0]` extraction was forgotten.
Related errors
- class_id must be 1d np.ndarray with (n, ) shape
- Shape of np.ndarray for key '{key}' must be ({n},)
- First dimension of np.ndarray for key '{key}' must have size
- top_k could not be calculated, confidence is None
- NumPy image must have at least 2 dimensions (H, W, ...). Rec
AI-assisted analysis of roboflow/supervision@7f254d9784 (2026-08-15).
Data as JSON: /api/errors/b4c03bbeaf4abc45.
Report an issue: GitHub.