keras-team/keras · error · ValueError

If `bounding_boxes['boxes']` and `bounding_boxes['labels']`

Error message

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 

What it means

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.

Source

Thrown at keras/src/layers/preprocessing/image_preprocessing/bounding_boxes/validation.py:139

        or "labels" not in bounding_boxes
        or "boxes" not in bounding_boxes
    ):
        raise ValueError(
            "Expected `bounding_boxes` agurment to be a "
            "dict with keys 'boxes' and 'labels'. Received: "
            f"bounding_boxes={bounding_boxes}"
        )
    boxes = bounding_boxes["boxes"]
    labels = bounding_boxes["labels"]
    if isinstance(boxes, list):
        if not isinstance(labels, list):
            raise ValueError(
                "If `bounding_boxes['boxes']` is a list, then "
                "`bounding_boxes['labels']` must also be a list."
                f"Received: bounding_boxes['labels']={labels}"
            )
        if len(boxes) != len(labels):
            raise ValueError(
                "If `bounding_boxes['boxes']` and "
                "`bounding_boxes['labels']` are both lists, "
                "they must have the same length. Received: "
                f"len(bounding_boxes['boxes'])={len(boxes)} and "
                f"len(bounding_boxes['labels'])={len(labels)} and "
            )
    elif tf_utils.is_ragged_tensor(boxes):
        if not tf_utils.is_ragged_tensor(labels):
            raise ValueError(
                "If `bounding_boxes['boxes']` is a Ragged tensor, "
                " `bounding_boxes['labels']` must also be a "
                "Ragged tensor. "
                f"Received: bounding_boxes['labels']={labels}"
            )
    else:
        boxes_shape = current_backend.shape(boxes)
        labels_shape = current_backend.shape(labels)
        if len(boxes_shape) == 2:  # (boxes, 4)

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Rebuild the batch so boxes and labels are appended together per image
  2. Log len(boxes) and len(labels) where the dict is constructed to find the divergence point
  3. Use zip(images, boxes, labels) when assembling batches so lengths stay coupled

Example fix

# before
boxes.append(img_boxes)
# ... later labels appended conditionally
# after
for img_boxes, img_labels in zip(all_boxes, all_labels):
    boxes.append(img_boxes)
    labels.append(img_labels)
Defensive patterns

Strategy: validation

Validate before calling

assert len(bbs['boxes']) == len(bbs['labels']), (
    f"len(boxes)={len(bbs['boxes'])} len(labels)={len(bbs['labels'])}")

Try / catch

try:
    out = densify_bounding_boxes(bbs)
except ValueError as e:
    if 'same length' in str(e):
        n = min(len(bbs['boxes']), len(bbs['labels']))
        bbs = {'boxes': bbs['boxes'][:n], 'labels': bbs['labels'][:n]}
    else:
        raise

Prevention

When it happens

Trigger: densify_bounding_boxes with {'boxes': [b0, b1, b2], 'labels': [l0, l1]} — 3 per-image box arrays but only 2 label arrays.

Common situations: Filtering images or labels independently in a data pipeline; appending augmented boxes without appending labels; off-by-one errors when slicing batches.

Related errors


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/9cdcacbc16cb116f. Report an issue: GitHub.