{"record":{"id":"d39f219eb2a335cc","repo":"roboflow/supervision","slug":"invalid-coco-rle-counts","errorCode":null,"errorMessage":"Invalid COCO RLE counts.","messagePattern":"Invalid COCO RLE counts\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/supervision/detection/compact_mask.py","lineNumber":406,"sourceCode":"        if isinstance(counts, bytes):\n            counts = counts.decode(\"utf-8\")\n        if isinstance(counts, str):\n            decoded_counts = _delta_decode(_base48_decode(counts))\n            counts_arr = np.array(decoded_counts, dtype=np.int32)\n        else:\n            # Convert to int64 first, then range-check against int32 bounds before\n            # narrowing. A direct int32 cast wraps silently on some numpy versions\n            # and raises on others; this makes overflow detection deterministic.\n            counts_arr64 = np.asarray(counts, dtype=np.int64)\n            int32_info = np.iinfo(np.int32)\n            if counts_arr64.size and (\n                counts_arr64.max() > int32_info.max\n                or counts_arr64.min() < int32_info.min\n            ):\n                raise ValueError(\"COCO RLE counts exceed int32 range.\")\n            counts_arr = counts_arr64.astype(np.int32)\n    except (TypeError, ValueError, OverflowError) as exc:\n        raise ValueError(\"Invalid COCO RLE counts.\") from exc\n\n    if counts_arr.ndim != 1:\n        raise ValueError(\"COCO RLE counts must be one-dimensional.\")\n    if counts_arr.size == 0:\n        raise ValueError(\"COCO RLE counts cannot be empty.\")\n    if np.any(counts_arr < 0):\n        raise ValueError(\"COCO RLE counts must be non-negative.\")\n    return counts_arr\n\n\ndef _rle_resize(\n    rle: npt.NDArray[np.int32],\n    crop_h: int,\n    crop_w: int,\n    new_crop_h: int,\n    new_crop_w: int,\n) -> npt.NDArray[np.int32]:\n    \"\"\"Resize an F-order RLE-encoded crop via nearest-neighbour resampling.","sourceCodeStart":388,"sourceCodeEnd":424,"githubUrl":"https://github.com/roboflow/supervision/blob/7f254d9784d4c37e0f03cd89ddee164c8db099c0/src/supervision/detection/compact_mask.py#L388-L424","documentation":"Umbrella error raised while converting the COCO RLE counts payload into an int64 NumPy array: the conversion itself raised TypeError, ValueError, or OverflowError. This means the payload is not interpretable as a sequence of integers at all — e.g. strings that are not numeric, None values, dicts, or unhashable/malformed objects. The original exception is chained (from exc) for debugging.","triggerScenarios":"Passing rle['counts'] containing non-numeric entries such as 'abc', None, [None, 2], or a bytes/str object that np.asarray(..., dtype=np.int64) cannot parse, to CompactMask.from_coco_rle's counts parser.","commonSituations":"Passing pycocotools' compressed ASCII bytes counts where a numeric list was expected; JSON with null entries; mixing up field order and passing 'size' as 'counts'; partially deserialized payloads.","solutions":["Print/inspect the chained original exception (raise ... from exc preserves it) to see which element failed conversion.","If counts is a compressed string/bytes from pycocotools, decode it first (e.g. mask_utils.decode or the library's compressed handling) so you pass an integer sequence.","Ensure every element of counts is an int (or int-like) before calling from_coco_rle."],"exampleFix":"# before — compressed bytes passed as counts\nrles = [{\"size\": [4, 4], \"counts\": b\"Xfg01\"}]\n\n# after — decode to uncompressed integer counts first\ncounts = my_decompressed_int_list\nrles = [{\"size\": [4, 4], \"counts\": counts}]","handlingStrategy":"type-guard","validationCode":"def to_int_counts(counts):\n    \"\"\"Return flat list[int] or raise if payload is not numeric.\"\"\"\n    if isinstance(counts, (bytes, str)):\n        raise TypeError(\"compressed counts must be decoded first\")\n    return [int(c) for c in counts]","typeGuard":"def is_numeric_counts(counts) -> bool:\n    return isinstance(counts, (list, tuple)) and all(\n        isinstance(c, (int, np.integer)) and not isinstance(c, bool) for c in counts\n    )","tryCatchPattern":"try:\n    cm = sv.CompactMask.from_coco_rle(rles, xyxy, image_shape=shape)\nexcept ValueError as e:\n    if \"Invalid COCO RLE counts\" in str(e) and e.__cause__ is not None:\n        log.error(\"bad counts payload: %r caused by %s\", rles, e.__cause__)\n    raise","preventionTips":["Decode compressed (bytes/str) pycocotools counts to integer lists before calling from_coco_rle.","Inspect the chained __cause__ exception — it names the exact element that failed.","Keep 'size' and 'counts' keys straight when reshaping payloads."],"tags":["coco","rle","compact-mask","type-error","validation"],"backgroundTag":null,"analyzedSha":"7f254d9784d4c37e0f03cd89ddee164c8db099c0","analyzedAt":"2026-08-15T05:13:01.950Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}