{"record":{"id":"b4c03bbeaf4abc45","repo":"roboflow/supervision","slug":"confidence-must-be-1d-np-ndarray-with-n-shape","errorCode":null,"errorMessage":"confidence must be 1d np.ndarray with (n, ) shape","messagePattern":"confidence must be 1d np\\.ndarray with \\(n, \\) shape","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/supervision/classification/core.py","lineNumber":29,"sourceCode":"\n\ndef _validate_class_ids(class_id: Any, n: int) -> None:\n    \"\"\"\n    Ensure that class_id is a 1d np.ndarray with (n, ) shape.\n    \"\"\"\n    is_valid = isinstance(class_id, np.ndarray) and class_id.shape == (n,)\n    if not is_valid:\n        raise ValueError(\"class_id must be 1d np.ndarray with (n, ) shape\")\n\n\ndef _validate_confidence(confidence: Any, n: int) -> None:\n    \"\"\"\n    Ensure that confidence is a 1d np.ndarray with (n, ) shape.\n    \"\"\"\n    if confidence is not None:\n        is_valid = isinstance(confidence, np.ndarray) and confidence.shape == (n,)\n        if not is_valid:\n            raise ValueError(\"confidence must be 1d np.ndarray with (n, ) shape\")\n\n\n@dataclass\nclass Classifications:\n    class_id: npt.NDArray[np.int_]\n    confidence: npt.NDArray[np.floating] | None = None\n\n    def __post_init__(self) -> None:\n        \"\"\"\n        Validate the classification inputs.\n        \"\"\"\n        n = len(self.class_id)\n\n        _validate_class_ids(self.class_id, n)\n        _validate_confidence(self.confidence, n)\n\n    def __eq__(self, other: object) -> bool:\n        \"\"\"","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/roboflow/supervision/blob/7f254d9784d4c37e0f03cd89ddee164c8db099c0/src/supervision/classification/core.py#L11-L47","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"# before\nsv.Classifications(class_id=np.array([0, 1, 2]), confidence=np.array([0.9]))\n# after\nsv.Classifications(class_id=np.array([0, 1, 2]), confidence=np.array([0.9, 0.5, 0.1]))","handlingStrategy":"validation","validationCode":"class_id = np.asarray(class_id)\nif confidence is not None:\n    confidence = np.asarray(confidence)\n    assert confidence.shape == class_id.shape, 'confidence must match class_id length'","typeGuard":"def confidence_aligned(confidence: Any, n: int) -> bool:\n    return confidence is None or (\n        isinstance(confidence, np.ndarray) and confidence.shape == (n,)\n    )","tryCatchPattern":null,"preventionTips":["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."],"tags":["classifications","numpy","shape-mismatch","confidence"],"backgroundTag":null,"analyzedSha":"7f254d9784d4c37e0f03cd89ddee164c8db099c0","analyzedAt":"2026-08-15T05:13:01.950Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}