keras-team/keras · error · ValueError

If providing `bounding_boxes['labels']` as a list, it should

Error message

If providing `bounding_boxes['labels']` as a list, it should contain integers labels. Received: bounding_boxes['labels']={labels}

What it means

densify_bounding_boxes converts ragged (variable-length) box lists into dense padded tensors. When boxes are given as nested Python lists (batched case: list of list of box), the parallel labels structure must contain Python ints at labels[batch][box]. If the first label element is not an int (e.g. a float, string, or tensor), this ValueError is raised.

Source

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

def densify_bounding_boxes(
    bounding_boxes,
    is_batched=False,
    max_boxes=None,
    boxes_default_value=0,
    labels_default_value=-1,
    backend=None,
):
    validate_bounding_boxes(bounding_boxes)
    boxes = bounding_boxes["boxes"]
    labels = bounding_boxes["labels"]
    backend = backend or current_backend
    if isinstance(boxes, list):
        if boxes and isinstance(boxes[0], list):
            if boxes[0] and isinstance(boxes[0][0], list):
                # Batched case
                if not isinstance(labels[0][0], int):
                    raise ValueError(
                        "If providing `bounding_boxes['labels']` as a list, "
                        "it should contain integers labels. Received: "
                        f"bounding_boxes['labels']={labels}"
                    )
                if max_boxes is not None:
                    max_boxes = max([len(b) for b in boxes])
                new_boxes = []
                new_labels = []
                for b, l in zip(boxes, labels):
                    if len(b) >= max_boxes:
                        new_boxes.append(b[:max_boxes])
                        new_labels.append(l[:max_boxes])
                    else:
                        num_boxes_to_add = max_boxes - len(b)
                        added_boxes = [
                            [
                                boxes_default_value,
                                boxes_default_value,

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Cast labels to Python ints: labels=[[int(l) for l in b] for b in labels].
  2. Convert numpy label arrays with .astype(int).tolist() before passing.
  3. Alternatively pass boxes and labels as tensors/ragged tensors, which skips the int-only list path.

Example fix

# before
bb = {"boxes": [[[0,0,10,10],[5,5,20,20]]], "labels": [[0.0, 1.0]]}
out = densify_bounding_boxes(bb)
# after
bb = {"boxes": [[[0,0,10,10],[5,5,20,20]]], "labels": [[int(0.0), int(1.0)]]}
out = densify_bounding_boxes(bb)
Defensive patterns

Strategy: type-guard

Validate before calling

labels = [[int(l) for l in batch] for batch in labels]  # before passing list-mode input

Type guard

def labels_are_int_lists(labels):
    return (
        isinstance(labels, list)
        and labels
        and isinstance(labels[0], list)
        and bool(labels[0])
        and isinstance(labels[0][0], int)
    )

Prevention

When it happens

Trigger: Calling a preprocessing layer or transform_bounding_boxes with bounding_boxes={'boxes': [[[...],[...]]], 'labels': [[0.0, 1.0]]} - float labels in list-mode input.

Common situations: Labels coming from a numpy array of float dtype, a JSON parse that produced floats, or a model output grafted into a list-of-lists structure with tensor elements.

Understand the failure class

Background: Invalid argument type errors: "must be of type string", "expected X, got Y", and ERR_INVALID_ARG_TYPE explained — this error's family across 15 libraries.

Related errors


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