{"record":{"id":"eb86d2d0766846b5","repo":"roboflow/supervision","slug":"class-id-must-be-1d-np-ndarray-with-n-shape","errorCode":null,"errorMessage":"class_id must be 1d np.ndarray with (n, ) shape","messagePattern":"class_id must be 1d np\\.ndarray with \\(n, \\) shape","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/supervision/classification/core.py","lineNumber":19,"sourceCode":"from __future__ import annotations\n\nfrom dataclasses import dataclass\nfrom typing import TYPE_CHECKING, Any\n\nimport numpy as np\nimport numpy.typing as npt\n\nif TYPE_CHECKING:\n    import torch  # type: ignore[import-not-found, unused-ignore]\n\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:","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/roboflow/supervision/blob/7f254d9784d4c37e0f03cd89ddee164c8db099c0/src/supervision/classification/core.py#L1-L37","documentation":"Raised by `sv.Classifications.__post_init__` when `class_id` is not a 1-D np.ndarray of shape `(n,)`. Since `n` is derived from `len(class_id)`, the check effectively fails when `class_id` is a Python list, a 2-D array, or any other type. The library requires ndarray internally so vectorized ops (argsort, indexing in `get_top_k`) work.","triggerScenarios":"Calling `sv.Classifications(class_id=[0, 1, 2], confidence=...)` with a plain list; passing `class_id=np.array([[0], [1]])` (2-D); passing a tensor or other array-like that is not np.ndarray.","commonSituations":"Coming from model wrappers that return lists of class indices; converting from a framework tensor and forgetting `.cpu().numpy()`; passing results of `np.asarray(list_of_lists)` producing 2-D.","solutions":["Wrap the value: `class_id=np.asarray(class_id)` before constructing `sv.Classifications`.","If it is 2-D, flatten it explicitly: `np.asarray(x).reshape(-1)`.","Convert framework tensors first: `tensor.detach().cpu().numpy()`."],"exampleFix":"# before\nclassifications = sv.Classifications(class_id=[0, 1, 2], confidence=np.array([0.3, 0.9, 0.5]))\n# after\nclassifications = sv.Classifications(class_id=np.array([0, 1, 2]), confidence=np.array([0.3, 0.9, 0.5]))","handlingStrategy":"type-guard","validationCode":"class_id = np.asarray(class_id)\nassert class_id.ndim == 1, f'class_id must be 1-D, got {class_id.shape}'","typeGuard":"def is_valid_class_id(x: Any) -> bool:\n    return isinstance(x, np.ndarray) and x.ndim == 1","tryCatchPattern":null,"preventionTips":["Always wrap class ids with np.asarray(...) at the boundary.","Convert framework tensors with .detach().cpu().numpy().","Flatten accidental 2-D inputs with .reshape(-1)."],"tags":["classifications","numpy","shape-mismatch","constructor"],"backgroundTag":null,"analyzedSha":"7f254d9784d4c37e0f03cd89ddee164c8db099c0","analyzedAt":"2026-08-15T05:13:01.950Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}