{"record":{"id":"9c6799f38942d136","repo":"roboflow/supervision","slug":"both-detections-should-have-exactly-1-detected-obj","errorCode":null,"errorMessage":"Both Detections should have exactly 1 detected object.","messagePattern":"Both Detections should have exactly 1 detected object\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/supervision/detection/core.py","lineNumber":3424,"sourceCode":"\n    Example:\n        ```python\n        from supervision import _cv2 as cv2\n        import supervision as sv\n        from inference import get_model\n\n        image = cv2.imread(\"<SOURCE_IMAGE_PATH>\")\n        model = get_model(model_id=\"yolov8s-640\")\n\n        result = model.infer(image)[0]\n        detections = sv.Detections.from_inference(result)\n\n        merged_detections = merge_object_detection_pair(\n            detections[0], detections[1])\n        ```\n    \"\"\"\n    if len(detections_1) != 1 or len(detections_2) != 1:\n        raise ValueError(\"Both Detections should have exactly 1 detected object.\")\n\n    _validate_fields_both_defined_or_none(detections_1, detections_2)\n\n    xyxy_1 = detections_1.xyxy[0]\n    xyxy_2 = detections_2.xyxy[0]\n    if detections_1.confidence is None and detections_2.confidence is None:\n        merged_confidence = None\n    else:\n        assert detections_1.confidence is not None\n        assert detections_2.confidence is not None\n        detection_1_area = (xyxy_1[2] - xyxy_1[0]) * (xyxy_1[3] - xyxy_1[1])\n        detections_2_area = (xyxy_2[2] - xyxy_2[0]) * (xyxy_2[3] - xyxy_2[1])\n        merged_confidence = (\n            detection_1_area * detections_1.confidence[0]\n            + detections_2_area * detections_2.confidence[0]\n        ) / (detection_1_area + detections_2_area)\n        merged_confidence = np.array([merged_confidence])\n","sourceCodeStart":3406,"sourceCodeEnd":3442,"githubUrl":"https://github.com/roboflow/supervision/blob/7f254d9784d4c37e0f03cd89ddee164c8db099c0/src/supervision/detection/core.py#L3406-L3442","documentation":"merge_object_detection_pair (and its deprecated alias merge_inner_detection_object_pair) merges exactly two single-object Detections into one. It indexes xyxy[0] on each input, so both inputs must contain exactly one detection each; anything else raises this ValueError immediately.","triggerScenarios":"Calling merge_object_detection_pair(detections_1, detections_2) where len(detections_1) != 1 or len(detections_2) != 1 — e.g. passing full multi-object Detections from model.infer(), passing Detections.empty(), or passing slices like detections[0:2] that keep 2 rows.","commonSituations":"Copying the docstring example but forgetting that detections[0] (single index) yields a 1-row Detections while detections[0:2] does not; passing an empty result from a frame with no objects; piping tracker/model output straight into the merge helper.","solutions":["Slice each input to exactly one row: merge_object_detection_pair(detections_1[i:i+1], detections_2[j:j+1]) — note the i:i+1 slice form, not detections[i] alone unless that returns a length-1 Detections in your version.","Check len(detections_1) == 1 and len(detections_2) == 1 before calling; skip or handle empty frames explicitly.","If merging more than two overlapping objects, note this API is pair-only — use group_detections / merge_object_detections instead."],"exampleFix":"# before\nmerged = merge_object_detection_pair(dets_a, dets_b)  # both multi-row\n\n# after\nassert len(dets_a) == 1 and len(dets_b) == 1, 'pair merge needs single-object inputs'\nmerged = merge_object_detection_pair(dets_a, dets_b)","handlingStrategy":"validation","validationCode":"def assert_single_detection_pair(d1: sv.Detections, d2: sv.Detections) -> None:\n    if len(d1) != 1 or len(d2) != 1:\n        raise ValueError(\n            f'pair merge needs len 1 inputs, got {len(d1)} and {len(d2)}'\n        )\n\nassert_single_detection_pair(dets_a, dets_b)\nmerged = merge_object_detection_pair(dets_a, dets_b)","typeGuard":"def is_single_detection(d: sv.Detections) -> bool:\n    return isinstance(d, sv.Detections) and len(d) == 1","tryCatchPattern":"try:\n    merged = merge_object_detection_pair(d1, d2)\nexcept ValueError as e:\n    if 'exactly 1 detected object' in str(e):\n        merged = d1 if len(d1) else d2  # or log & skip frame\n    else:\n        raise","preventionTips":["Slice with [i:i+1] not [i] when unsure of indexing semantics","Guard len()==1 before calling pair-merge helpers","Use group_detections for multi-object merge workflows"],"tags":["detections","merge","validation","supervision"],"backgroundTag":null,"analyzedSha":"7f254d9784d4c37e0f03cd89ddee164c8db099c0","analyzedAt":"2026-08-15T05:13:01.950Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}