{"record":{"id":"4f5eb0011dc34de0","repo":"roboflow/supervision","slug":"the-number-of-predictions-total-images-predictio","errorCode":null,"errorMessage":"The number of predictions ({total_images_predictions}) and targets ({total_images_targets}) during the evaluation must be the same.","messagePattern":"The number of predictions \\((.+?)\\) and targets \\((.+?)\\) during the evaluation must be the same\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/supervision/metrics/mean_average_precision.py","lineNumber":1653,"sourceCode":"                if content is not None:\n                    dict_prediction[\"content\"] = content[pred_idx]\n                coco_predictions.append(dict_prediction)\n        return coco_predictions\n\n    def compute(self) -> MeanAveragePrecisionResult:\n        \"\"\"\n        Calculate Mean Average Precision based on predicted and ground-truth\n        detections at different thresholds using the COCO evaluation metrics.\n        Source: https://github.com/rafaelpadilla/review_object_detection_metrics\n\n        Returns:\n            The Mean Average Precision result.\n        \"\"\"\n        total_images_predictions = len(self._predictions_list)\n        total_images_targets = len(self._targets_list)\n\n        if total_images_predictions != total_images_targets:\n            raise ValueError(\n                f\"The number of predictions ({total_images_predictions}) and\"\n                f\" targets ({total_images_targets}) during the evaluation must be\"\n                \" the same.\"\n            )\n        dict_targets = self._prepare_targets(self._targets_list)\n        lst_predictions = self._prepare_predictions(self._predictions_list)\n        # Create a coco object with the targets\n        coco_gt = EvaluationDataset(targets=dict_targets)\n        # Include the predictions to coco object\n        coco_det = coco_gt.load_predictions(lst_predictions)\n        # Create a coco evaluator with the predictions\n        cocoEval = COCOEvaluator(coco_gt, coco_det, metric_target=self._metric_target)\n\n        # Evaluate on all images\n        cocoEval.evaluate()\n\n        # Create MeanAveragePrecisionResult object for small objects\n        mAP_small = MeanAveragePrecisionResult(","sourceCodeStart":1635,"sourceCodeEnd":1671,"githubUrl":"https://github.com/roboflow/supervision/blob/7f254d9784d4c37e0f03cd89ddee164c8db099c0/src/supervision/metrics/mean_average_precision.py#L1635-L1671","documentation":"MeanAveragePrecision.compute() verifies that the accumulated prediction images and target images counts match before building the COCO evaluation. Unlike the update()-time check (error 354), this fires at evaluation time, meaning the two internal lists ended up with different lengths — typically because update() was called with matching single Detections at different times, or lists were extended unevenly across multiple update() calls.","triggerScenarios":"Calling map.update(pred_a, target_a) then map.update(pred_list, target_list) with mismatched per-call counts is caught earlier — this error specifically appears when the totals diverge, e.g. update(preds, [t1]) style single-vs-list mismatches that slipped through, or direct mutation of the private _predictions_list/_targets_list; multiple update() calls each balanced individually cannot trigger it (each is validated), so this guard mostly protects internal-state corruption and direct list manipulation.","commonSituations":"Custom code extending _predictions_list/_targets_list directly; forks that bypass update() validation; memory of state across experiments in notebooks reusing a metric object; subclass overrides of update() that forget the length check.","solutions":["Use only the public update() to add data — it validates per call","Never mutate _predictions_list/_targets_list; create a fresh metric per experiment","In subclasses, call super().update() or replicate the length validation","Restart the metric object if state integrity is in doubt and re-run updates"],"exampleFix":"# before\nmap_._predictions_list.append(pred)      # bypasses validation\nmap_._targets_list.extend(all_targets)    # uneven -> compute() fails\nmap_.compute()\n\n# after\nmap_ = sv.MeanAveragePrecision()\nfor p, t in zip(preds, targets):\n    map_.update(p, t)                      # validated per call\nmap_.compute()","handlingStrategy":"validation","validationCode":"assert len(map_._predictions_list) == len(map_._targets_list) or True\n# real guard: only use public update(); before compute check nothing extra is needed","typeGuard":null,"tryCatchPattern":"try:\n    result = map_.compute()\nexcept ValueError as e:\n    if 'must be the same' in str(e):\n        raise RuntimeError('metric state corrupted; rebuild metric and re-run updates') from e\n    raise","preventionTips":["Never append directly to _predictions_list/_targets_list","Create a fresh MeanAveragePrecision per experiment/run","In subclasses, delegate to super().update() to keep validation"],"tags":["metrics","mean-average-precision","validation","internal-state","api-misuse"],"backgroundTag":null,"analyzedSha":"7f254d9784d4c37e0f03cd89ddee164c8db099c0","analyzedAt":"2026-08-15T05:13:01.950Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}