{"record":{"id":"eea685cf6a5766f6","repo":"roboflow/supervision","slug":"the-length-of-custom-values-must-match-the-number","errorCode":null,"errorMessage":"The length of custom_values must match the number of detections.","messagePattern":"The length of custom_values must match the number of detections\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/supervision/annotators/core.py","lineNumber":2989,"sourceCode":"        custom_values: npt.NDArray[np.float64] | list[float] | None,\n        detections: Detections,\n    ) -> None:\n        if custom_values is None:\n            if detections.confidence is None:\n                raise ValueError(\n                    \"The provided detections do not contain confidence values. \"\n                    \"Please provide `custom_values` or ensure that the detections \"\n                    \"contain confidence values (e.g. by using a different model).\"\n                )\n\n        else:\n            if not isinstance(custom_values, (np.ndarray, list)):\n                raise TypeError(\n                    \"custom_values must be either a numpy array or a list of floats.\"\n                )\n\n            if len(custom_values) != len(detections):\n                raise ValueError(\n                    \"The length of custom_values must match the number of detections.\"\n                )\n\n            if not all(0 <= value <= 1 for value in custom_values):\n                raise ValueError(\"All values in custom_values must be between 0 and 1.\")\n\n    @staticmethod\n    @deprecated(  # type: ignore[untyped-decorator]\n        target=_validate_custom_values.__func__,  # type: ignore[attr-defined]\n        deprecated_in=\"0.29.0\",\n        remove_in=\"0.32.0\",\n    )\n    def validate_custom_values(\n        custom_values: npt.NDArray[np.float64] | list[float] | None,\n        detections: Detections,\n    ) -> None:\n        void(custom_values, detections)\n","sourceCodeStart":2971,"sourceCodeEnd":3007,"githubUrl":"https://github.com/roboflow/supervision/blob/7f254d9784d4c37e0f03cd89ddee164c8db099c0/src/supervision/annotators/core.py#L2971-L3007","documentation":"Raised by `PercentageBarAnnotator._validate_custom_values` when `custom_values` is a list/array but its length differs from the number of detections. Each bar is drawn per detection, so the values must align 1:1 with `detections`; a mismatch would silently skip or misattribute bars, hence the early failure.","triggerScenarios":"Passing `custom_values=[0.9, 0.4]` to annotate 5 detections; filtering detections after building the values array; computing per-class values (length = number of classes) instead of per-detection values; combining detections from two frames while reusing one values array.","commonSituations":"Detections resized by confidence filtering or NMS between metric computation and annotation; per-class aggregates mistaken for per-detection values; multi-camera loops reusing a cached values array.","solutions":["Build custom_values after the last filtering step so `len(custom_values) == len(detections)`.","For class-level values, expand to per-detection: `[class_score[c] for c in detections.class_id]`.","Apply the same boolean mask used on detections to the values array."],"exampleFix":"# before\nvalues = np.array([0.9, 0.4, 0.7, 0.2, 0.5])\ndetections = detections[detections.confidence > 0.3]  # len changed\nannotator.annotate(scene, detections, custom_values=values)  # ValueError\n\n# after\nkeep = detections.confidence > 0.3\nvalues = values[keep]\ndetections = detections[keep]\nannotator.annotate(scene, detections, custom_values=values)","handlingStrategy":"validation","validationCode":"custom_values = np.asarray(custom_values)\nassert len(custom_values) == len(detections), (\n    f\"{len(custom_values)} values for {len(detections)} detections\"\n)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Compute custom_values after the last filter applied to detections.","Apply the same mask to values and detections; never reuse a cached values array across frames."],"tags":["annotators","alignment","validation"],"backgroundTag":null,"analyzedSha":"7f254d9784d4c37e0f03cd89ddee164c8db099c0","analyzedAt":"2026-08-15T05:13:01.950Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}