{"record":{"id":"9cdcacbc16cb116f","repo":"keras-team/keras","slug":"if-bounding-boxes-boxes-and-bounding-boxes","errorCode":null,"errorMessage":"If `bounding_boxes['boxes']` and `bounding_boxes['labels']` are both lists, they must have the same length. Received: len(bounding_boxes['boxes'])={len(boxes)} and len(bounding_boxes['labels'])={len(labels)} and ","messagePattern":"If `bounding_boxes\\['boxes'\\]` and `bounding_boxes\\['labels'\\]` are both lists, they must have the same length\\. Received: len\\(bounding_boxes\\['boxes'\\]\\)=(.+?) and len\\(bounding_boxes\\['labels'\\]\\)=(.+?) and ","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/preprocessing/image_preprocessing/bounding_boxes/validation.py","lineNumber":139,"sourceCode":"        or \"labels\" not in bounding_boxes\n        or \"boxes\" not in bounding_boxes\n    ):\n        raise ValueError(\n            \"Expected `bounding_boxes` agurment to be a \"\n            \"dict with keys 'boxes' and 'labels'. Received: \"\n            f\"bounding_boxes={bounding_boxes}\"\n        )\n    boxes = bounding_boxes[\"boxes\"]\n    labels = bounding_boxes[\"labels\"]\n    if isinstance(boxes, list):\n        if not isinstance(labels, list):\n            raise ValueError(\n                \"If `bounding_boxes['boxes']` is a list, then \"\n                \"`bounding_boxes['labels']` must also be a list.\"\n                f\"Received: bounding_boxes['labels']={labels}\"\n            )\n        if len(boxes) != len(labels):\n            raise ValueError(\n                \"If `bounding_boxes['boxes']` and \"\n                \"`bounding_boxes['labels']` are both lists, \"\n                \"they must have the same length. Received: \"\n                f\"len(bounding_boxes['boxes'])={len(boxes)} and \"\n                f\"len(bounding_boxes['labels'])={len(labels)} and \"\n            )\n    elif tf_utils.is_ragged_tensor(boxes):\n        if not tf_utils.is_ragged_tensor(labels):\n            raise ValueError(\n                \"If `bounding_boxes['boxes']` is a Ragged tensor, \"\n                \" `bounding_boxes['labels']` must also be a \"\n                \"Ragged tensor. \"\n                f\"Received: bounding_boxes['labels']={labels}\"\n            )\n    else:\n        boxes_shape = current_backend.shape(boxes)\n        labels_shape = current_backend.shape(labels)\n        if len(boxes_shape) == 2:  # (boxes, 4)","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/preprocessing/image_preprocessing/bounding_boxes/validation.py#L121-L157","documentation":"When both 'boxes' and 'labels' are Python lists, validate_bounding_boxes requires len(boxes) == len(labels), because each element pair describes one image in the batch. A length mismatch means the batch has N images' boxes but M images' labels.","triggerScenarios":"densify_bounding_boxes with {'boxes': [b0, b1, b2], 'labels': [l0, l1]} — 3 per-image box arrays but only 2 label arrays.","commonSituations":"Filtering images or labels independently in a data pipeline; appending augmented boxes without appending labels; off-by-one errors when slicing batches.","solutions":["Rebuild the batch so boxes and labels are appended together per image","Log len(boxes) and len(labels) where the dict is constructed to find the divergence point","Use zip(images, boxes, labels) when assembling batches so lengths stay coupled"],"exampleFix":"# before\nboxes.append(img_boxes)\n# ... later labels appended conditionally\n# after\nfor img_boxes, img_labels in zip(all_boxes, all_labels):\n    boxes.append(img_boxes)\n    labels.append(img_labels)","handlingStrategy":"validation","validationCode":"assert len(bbs['boxes']) == len(bbs['labels']), (\n    f\"len(boxes)={len(bbs['boxes'])} len(labels)={len(bbs['labels'])}\")","typeGuard":null,"tryCatchPattern":"try:\n    out = densify_bounding_boxes(bbs)\nexcept ValueError as e:\n    if 'same length' in str(e):\n        n = min(len(bbs['boxes']), len(bbs['labels']))\n        bbs = {'boxes': bbs['boxes'][:n], 'labels': bbs['labels'][:n]}\n    else:\n        raise","preventionTips":["Append boxes and labels together in one loop per image","Assemble batches with zip(images, boxes, labels)"],"tags":["keras","bounding-boxes","length-mismatch","batch-data"],"backgroundTag":"input-shape-validation-failed","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}