{"record":{"id":"adbe26db36bc9427","repo":"sgl-project/sglang","slug":"image-index-index-out-of-range","errorCode":null,"errorMessage":"Image index {index} out of range","messagePattern":"Image index (.+?) out of range","errorType":"validation","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"python/sglang/multimodal_gen/apps/ComfyUI_SGLDiffusion/core/server_api.py","lineNumber":436,"sourceCode":"\n    def decode_image_from_response(\n        self, response_data: Dict[str, Any], index: int = 0\n    ) -> Image.Image:\n        \"\"\"\n        Decode base64 image from API response.\n\n        Args:\n            response_data: API response dictionary\n            index: Index of the image in the response (default: 0)\n\n        Returns:\n            PIL Image object\n        \"\"\"\n        if \"data\" not in response_data or not response_data[\"data\"]:\n            raise ValueError(\"No image data in response\")\n\n        if index >= len(response_data[\"data\"]):\n            raise IndexError(f\"Image index {index} out of range\")\n\n        image_data = response_data[\"data\"][index]\n        if \"b64_json\" not in image_data or not image_data[\"b64_json\"]:\n            raise ValueError(\"No base64 image data found\")\n\n        image_bytes = base64.b64decode(image_data[\"b64_json\"])\n        image = Image.open(io.BytesIO(image_bytes))\n\n        # Convert to RGB if needed\n        if image.mode != \"RGB\":\n            image = image.convert(\"RGB\")\n\n        return image\n\n    def set_lora(\n        self,\n        lora_nickname: str,\n        lora_path: Optional[str] = None,","sourceCodeStart":418,"sourceCodeEnd":454,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/apps/ComfyUI_SGLDiffusion/core/server_api.py#L418-L454","documentation":"IndexError from decode_image_from_response: 'data' exists but the requested index is beyond its length. The caller asked for image N in a response that returned fewer images.","triggerScenarios":"Calling decode_image_from_response(response, index=2) when only 1-2 images were returned; n smaller than the index used; negative indices also bypass the >= check and fail oddly.","commonSituations":"Hardcoding an index in a loop over multiple responses; server returning fewer images than requested n; off-by-one when iterating.","solutions":["Use index within range: 0 <= index < len(response['data']).","Iterate with enumerate(response['data']) instead of hardcoded indices.","Verify the requested n matches how many images you try to decode.","Log the data length when the error occurs."],"exampleFix":"// before\nimg = decode_image_from_response(response, index=2)\n// after\nidx = min(2, len(response[\"data\"]) - 1)\nimg = decode_image_from_response(response, index=idx)","handlingStrategy":"type-guard","validationCode":"n = len(response['data']); assert 0 <= index < n, f'index {index} >= {n}'","typeGuard":"def valid_index(r: dict, i: int) -> bool:\n    return 0 <= i < len(r.get('data') or [])","tryCatchPattern":null,"preventionTips":["Derive indices from len(data), never hardcode.","Iterate with enumerate over data.","Request n images equal to what you consume."],"tags":["validation","index-out-of-range","image-decoding","sgldiffusion"],"backgroundTag":"index-out-of-range","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}