roboflow/supervision · error · ValueError
confidence must be a 1D np.ndarray with shape {expected_shap
Error message
confidence must be a 1D np.ndarray with shape {expected_shape}, but got shape {actual_shape} What it means
Raised by supervision.validators._validate_confidence when confidence is given to Detections but is neither None nor a 1D np.ndarray of shape (n,) matching the number of boxes. Confidence holds per-detection scores in [0, 1].
Source
Thrown at src/supervision/validators/__init__.py:114
@deprecated( # type: ignore[untyped-decorator]
target=_validate_class_id,
deprecated_in="0.29.0",
remove_in="0.32.0",
)
def validate_class_id(class_id: Any, n: int) -> None:
void(class_id, n)
def _validate_confidence(confidence: Any, n: int) -> None:
"""Validate detection-level confidence: 1D ``np.ndarray`` with shape ``(n,)``."""
expected_shape = f"({n},)"
actual_shape = str(getattr(confidence, "shape", None))
is_valid = confidence is None or (
isinstance(confidence, np.ndarray) and confidence.shape == (n,)
)
if not is_valid:
raise ValueError(
f"confidence must be a 1D np.ndarray with shape {expected_shape}, but got "
f"shape {actual_shape}"
)
@deprecated( # type: ignore[untyped-decorator]
target=_validate_confidence,
deprecated_in="0.29.0",
remove_in="0.32.0",
)
def validate_confidence(confidence: Any, n: int) -> None:
void(confidence, n)
def _validate_keypoint_confidence(confidence: Any, n: int, m: int) -> None:
"""Validate per-keypoint confidence: 2D ``np.ndarray`` with shape ``(n, m)``."""
actual_shape = str(getattr(confidence, "shape", None))
View on GitHub (pinned to 7f254d9784)
Solutions
- Convert to a flat NumPy array: confidence=np.asarray(scores).ravel().
- Apply the same boolean mask used on xyxy to confidence so lengths stay equal.
- Convert Torch tensors first: scores.detach().cpu().numpy().
- Omit confidence (None) if your detector does not produce scores.
Example fix
# before dets = Detections(xyxy=boxes, confidence=[[0.9], [0.8]]) # (2,1) -> ValueError # after dets = Detections(xyxy=boxes, confidence=np.array([0.9, 0.8]))
Defensive patterns
Strategy: type-guard
Validate before calling
n = len(xyxy) confidence = None if confidence is None else np.asarray(confidence, dtype=np.float32).ravel() assert confidence is None or confidence.shape == (n,) dets = Detections(xyxy=xyxy, confidence=confidence)
Type guard
def is_valid_confidence(confidence: Any, n: int) -> bool:
return confidence is None or (
isinstance(confidence, np.ndarray) and confidence.shape == (n,)
) Prevention
- ravel() (n, 1) score arrays before passing them.
- Convert Torch tensors with .detach().cpu().numpy().
- Filter confidence and xyxy with the identical mask.
When it happens
Trigger: Passing confidence as a Python list, a 2D array like (n, 1), or an array of length different from len(xyxy) when constructing Detections.
Common situations: Taking raw model output scores that come as (n, 1) and passing them unmodified; mixing Torch tensors (convert with .cpu().numpy()) or lists instead of NumPy arrays; filtering boxes without filtering confidence the same way.
Related errors
- xyxy must be a 2D np.ndarray with shape {expected_shape}, bu
- class_id must be a 1D np.ndarray with shape {expected_shape}
- tracker_id must be a 1D np.ndarray with shape {expected_shap
- Detections must have class_id attribute.
- Both Detections should have exactly 1 detected object.
AI-assisted analysis of roboflow/supervision@7f254d9784 (2026-08-15).
Data as JSON: /api/errors/c48fb13aac3e1846.
Report an issue: GitHub.