{"record":{"id":"18c39cfe20d7da2c","repo":"roboflow/supervision","slug":"each-rle-payload-must-contain-size-and-counts","errorCode":null,"errorMessage":"Each RLE payload must contain 'size' and 'counts'.","messagePattern":"Each RLE payload must contain 'size' and 'counts'\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/supervision/detection/compact_mask.py","lineNumber":813,"sourceCode":"            )\n\n        if len(rles) == 0:\n            return cls(\n                [],\n                np.empty((0, 2), dtype=np.int32),\n                np.empty((0, 2), dtype=np.int32),\n                (img_h, img_w),\n            )\n\n        crop_rles: list[npt.NDArray[np.int32]] = []\n        crop_shapes_list: list[tuple[int, int]] = []\n        offsets_list: list[tuple[int, int]] = []\n\n        for mask_idx, rle in enumerate(rles):\n            if not isinstance(rle, Mapping):\n                raise ValueError(\"Each RLE payload must be a mapping.\")\n            if \"size\" not in rle or \"counts\" not in rle:\n                raise ValueError(\"Each RLE payload must contain 'size' and 'counts'.\")\n\n            try:\n                # COCO standard: size=[height, width] (h,w order per pycocotools spec)\n                rle_h, rle_w = rle[\"size\"]\n                rle_h = int(rle_h)\n                rle_w = int(rle_w)\n            except (TypeError, ValueError) as exc:\n                raise ValueError(\"RLE size must be [height, width].\") from exc\n\n            if (rle_h, rle_w) != (img_h, img_w):\n                raise ValueError(\n                    f\"RLE size {(rle_h, rle_w)} must match image_shape \"\n                    f\"{(img_h, img_w)}.\"\n                )\n\n            counts = _coco_rle_counts_to_array(rle[\"counts\"])\n            if int(np.sum(counts, dtype=np.int64)) != img_h * img_w:\n                raise ValueError(","sourceCodeStart":795,"sourceCodeEnd":831,"githubUrl":"https://github.com/roboflow/supervision/blob/7f254d9784d4c37e0f03cd89ddee164c8db099c0/src/supervision/detection/compact_mask.py#L795-L831","documentation":"Raised by CompactMask.from_coco_rle when an RLE mapping is missing the 'size' or 'counts' key. Both are mandatory: 'size' is validated against image_shape and 'counts' is decoded into the mask. The check is a friendly precondition that fails before a KeyError could escape.","triggerScenarios":"Passing {'counts': [0,2,...]} without 'size', or {'size': [h, w]} without 'counts'; renamed keys from a custom format (e.g. 'shape'/'runs') not mapped to COCO names.","commonSituations":"Hand-rolled RLE dicts from internal pipelines using different key names; partial copies of COCO segmentation dicts that dropped a key; version drift in a producer service.","solutions":["Rename your keys to exactly 'size' and 'counts' when building the payload.","If your source format is {'shape': ..., 'runs': ...}, translate: {'size': s['shape'], 'counts': s['runs']}.","Add an assertion loop over payloads checking both keys before calling from_coco_rle."],"exampleFix":"# before\nrles = [{\"shape\": [4, 4], \"runs\": [0, 2, 2, 2, 10]}]\n\n# after\nrles = [{\"size\": r[\"shape\"], \"counts\": r[\"runs\"]} for r in raw_rles]","handlingStrategy":"validation","validationCode":"missing = [i for i, r in enumerate(rles) if \"size\" not in r or \"counts\" not in r]\nassert not missing, f\"RLE payloads missing keys at indices {missing}\"","typeGuard":"def has_required_keys(rle) -> bool:\n    return isinstance(rle, dict) and \"size\" in rle and \"counts\" in rle","tryCatchPattern":null,"preventionTips":["Map custom field names to 'size'/'counts' once, at the ingestion boundary.","Use dict literals with the exact COCO keys in tests and fixtures.","Add a schema check (required keys) when consuming third-party RLE JSON."],"tags":["coco","rle","compact-mask","missing-key","api-contract"],"backgroundTag":null,"analyzedSha":"7f254d9784d4c37e0f03cd89ddee164c8db099c0","analyzedAt":"2026-08-15T05:13:01.950Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}