{"record":{"id":"6369dc9e57bd1862","repo":"roboflow/supervision","slug":"top-k-could-not-be-calculated-confidence-is-none","errorCode":null,"errorMessage":"top_k could not be calculated, confidence is None","messagePattern":"top_k could not be calculated, confidence is None","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/supervision/classification/core.py","lineNumber":209,"sourceCode":"\n        Returns:\n            A tuple containing the top k class IDs and confidences.\n\n        Example:\n            ```pycon\n            >>> import numpy as np\n            >>> import supervision as sv\n            >>> classifications = sv.Classifications(\n            ...     class_id=np.array([0, 1, 2]),\n            ...     confidence=np.array([0.3, 0.9, 0.5])\n            ... )\n            >>> classifications.get_top_k(1)\n            (array([1]), array([0.9]))\n\n            ```\n        \"\"\"\n        if self.confidence is None:\n            raise ValueError(\"top_k could not be calculated, confidence is None\")\n\n        order = np.argsort(self.confidence)[::-1]\n        top_k_order = order[:k]\n        top_k_class_id = self.class_id[top_k_order]\n        top_k_confidence = self.confidence[top_k_order]\n\n        return top_k_class_id, top_k_confidence\n","sourceCodeStart":191,"sourceCodeEnd":217,"githubUrl":"https://github.com/roboflow/supervision/blob/7f254d9784d4c37e0f03cd89ddee164c8db099c0/src/supervision/classification/core.py#L191-L217","documentation":"Raised by `Classifications.get_top_k(k)` when `self.confidence` is None. Ranking by confidence is impossible without confidence scores, so the method refuses rather than returning arbitrary ordering. `Classifications` allows `confidence=None` at construction, but `get_top_k` does not.","triggerScenarios":"Calling `sv.Classifications(class_id=np.array([0, 1, 2])).get_top_k(1)` — no confidence argument supplied at construction; calling `get_top_k` on classifications produced by a classifier connector that does not emit confidences.","commonSituations":"Using a label-only classifier output (e.g. CLIP zero-shot labels without probabilities) and then trying to rank; code paths shared between models where one model omits confidence.","solutions":["Supply confidence at construction: pass a probability array aligned with class_id.","If your model gives no scores, rank by another criterion yourself (e.g. class_id order) instead of calling `get_top_k`.","Guard the call: `if classifications.confidence is not None: ... get_top_k(k)`."],"exampleFix":"# before\nclassifications = sv.Classifications(class_id=np.array([0, 1, 2]))\nclassifications.get_top_k(1)\n# after\nclassifications = sv.Classifications(\n    class_id=np.array([0, 1, 2]), confidence=np.array([0.3, 0.9, 0.5])\n)\nclassifications.get_top_k(1)","handlingStrategy":"validation","validationCode":"if classifications.confidence is None:\n    # rank by class_id order instead, or skip\n    top = classifications.class_id[:k]\nelse:\n    top_id, top_conf = classifications.get_top_k(k)","typeGuard":"def has_confidence(c: sv.Classifications) -> bool:\n    return c.confidence is not None","tryCatchPattern":"try:\n    top_id, top_conf = classifications.get_top_k(k)\nexcept ValueError:\n    top_id = classifications.class_id[:k]; top_conf = None","preventionTips":["Always pass confidence when constructing Classifications that will be ranked.","Check `.confidence is not None` before get_top_k.","If the model emits no probabilities, do not use get_top_k."],"tags":["classifications","top-k","confidence","none-check"],"backgroundTag":null,"analyzedSha":"7f254d9784d4c37e0f03cd89ddee164c8db099c0","analyzedAt":"2026-08-15T05:13:01.950Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}