{"record":{"id":"c48fb13aac3e1846","repo":"roboflow/supervision","slug":"confidence-must-be-a-1d-np-ndarray-with-shape-exp","errorCode":null,"errorMessage":"confidence must be a 1D np.ndarray with shape {expected_shape}, but got shape {actual_shape}","messagePattern":"confidence must be a 1D np\\.ndarray with shape (.+?), but got shape (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/supervision/validators/__init__.py","lineNumber":114,"sourceCode":"\n@deprecated(  # type: ignore[untyped-decorator]\n    target=_validate_class_id,\n    deprecated_in=\"0.29.0\",\n    remove_in=\"0.32.0\",\n)\ndef validate_class_id(class_id: Any, n: int) -> None:\n    void(class_id, n)\n\n\ndef _validate_confidence(confidence: Any, n: int) -> None:\n    \"\"\"Validate detection-level confidence: 1D ``np.ndarray`` with shape ``(n,)``.\"\"\"\n    expected_shape = f\"({n},)\"\n    actual_shape = str(getattr(confidence, \"shape\", None))\n    is_valid = confidence is None or (\n        isinstance(confidence, np.ndarray) and confidence.shape == (n,)\n    )\n    if not is_valid:\n        raise ValueError(\n            f\"confidence must be a 1D np.ndarray with shape {expected_shape}, but got \"\n            f\"shape {actual_shape}\"\n        )\n\n\n@deprecated(  # type: ignore[untyped-decorator]\n    target=_validate_confidence,\n    deprecated_in=\"0.29.0\",\n    remove_in=\"0.32.0\",\n)\ndef validate_confidence(confidence: Any, n: int) -> None:\n    void(confidence, n)\n\n\ndef _validate_keypoint_confidence(confidence: Any, n: int, m: int) -> None:\n    \"\"\"Validate per-keypoint confidence: 2D ``np.ndarray`` with shape ``(n, m)``.\"\"\"\n    actual_shape = str(getattr(confidence, \"shape\", None))\n","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/roboflow/supervision/blob/7f254d9784d4c37e0f03cd89ddee164c8db099c0/src/supervision/validators/__init__.py#L96-L132","documentation":"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].","triggerScenarios":"Passing confidence as a Python list, a 2D array like (n, 1), or an array of length different from len(xyxy) when constructing Detections.","commonSituations":"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.","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."],"exampleFix":"# before\ndets = Detections(xyxy=boxes, confidence=[[0.9], [0.8]])  # (2,1) -> ValueError\n\n# after\ndets = Detections(xyxy=boxes, confidence=np.array([0.9, 0.8]))","handlingStrategy":"type-guard","validationCode":"n = len(xyxy)\nconfidence = None if confidence is None else np.asarray(confidence, dtype=np.float32).ravel()\nassert confidence is None or confidence.shape == (n,)\ndets = Detections(xyxy=xyxy, confidence=confidence)","typeGuard":"def is_valid_confidence(confidence: Any, n: int) -> bool:\n    return confidence is None or (\n        isinstance(confidence, np.ndarray) and confidence.shape == (n,)\n    )","tryCatchPattern":null,"preventionTips":["ravel() (n, 1) score arrays before passing them.","Convert Torch tensors with .detach().cpu().numpy().","Filter confidence and xyxy with the identical mask."],"tags":["detections","validation","shape","numpy"],"backgroundTag":null,"analyzedSha":"7f254d9784d4c37e0f03cd89ddee164c8db099c0","analyzedAt":"2026-08-15T05:13:01.950Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}