{"record":{"id":"1d342619a6143b48","repo":"AUTOMATIC1111/stable-diffusion-webui","slug":"bad-number-of-images-passed-len-imgs-expectin","errorCode":null,"errorMessage":"bad number of images passed: {len(imgs)}; expecting {self.batch_size} or less","messagePattern":"bad number of images passed: (.+?); expecting (.+?) or less","errorType":"validation","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"modules/processing.py","lineNumber":1722,"sourceCode":"\r\n            image = np.array(image).astype(np.float32) / 255.0\r\n            image = np.moveaxis(image, 2, 0)\r\n\r\n            imgs.append(image)\r\n\r\n        if len(imgs) == 1:\r\n            batch_images = np.expand_dims(imgs[0], axis=0).repeat(self.batch_size, axis=0)\r\n            if self.overlay_images is not None:\r\n                self.overlay_images = self.overlay_images * self.batch_size\r\n\r\n            if self.color_corrections is not None and len(self.color_corrections) == 1:\r\n                self.color_corrections = self.color_corrections * self.batch_size\r\n\r\n        elif len(imgs) <= self.batch_size:\r\n            self.batch_size = len(imgs)\r\n            batch_images = np.array(imgs)\r\n        else:\r\n            raise RuntimeError(f\"bad number of images passed: {len(imgs)}; expecting {self.batch_size} or less\")\r\n\r\n        image = torch.from_numpy(batch_images)\r\n        image = image.to(shared.device, dtype=devices.dtype_vae)\r\n\r\n        if opts.sd_vae_encode_method != 'Full':\r\n            self.extra_generation_params['VAE Encoder'] = opts.sd_vae_encode_method\r\n\r\n        self.init_latent = images_tensor_to_samples(image, approximation_indexes.get(opts.sd_vae_encode_method), self.sd_model)\r\n        devices.torch_gc()\r\n\r\n        if self.resize_mode == 3:\r\n            self.init_latent = torch.nn.functional.interpolate(self.init_latent, size=(self.height // opt_f, self.width // opt_f), mode=\"bilinear\")\r\n\r\n        if image_mask is not None:\r\n            init_mask = latent_mask\r\n            latmask = init_mask.convert('RGB').resize((self.init_latent.shape[3], self.init_latent.shape[2]))\r\n            latmask = np.moveaxis(np.array(latmask, dtype=np.float32), 2, 0) / 255\r\n            latmask = latmask[0]\r","sourceCodeStart":1704,"sourceCodeEnd":1740,"githubUrl":"https://github.com/AUTOMATIC1111/stable-diffusion-webui/blob/82a973c04367123ae98bd9abdf80d9eda9b910e2/modules/processing.py#L1704-L1740","documentation":"In img2img latent init: images are broadcast to the batch — 1 image is repeated to batch_size, and if len(imgs) <= batch_size the batch_size is shrunk to the image count. Only when MORE images than batch_size are supplied does this RuntimeError fire, since there is no defined way to map extra images onto the batch.","triggerScenarios":"Calling img2img/init_latent path with a list of images longer than processing.batch_size (e.g. batch_size=1 but 3 images passed, or an API payload whose image array exceeds its batch_size field).","commonSituations":"Batch img2img scripts that pass the whole folder at once while leaving batch_size at default 1; API clients that set n_iter for txt2img semantics but forget to raise batch_size for image arrays.","solutions":["Set batch_size >= len(images) (or chunk images into groups of at most batch_size and call once per chunk).","Or pass a single image and let the code replicate it across the batch.","For API users: keep the number of elements in 'images' consistent with batch_size * n_iter handling — batch mode uses batch_size."],"exampleFix":"# before\nproc = process_images(img2img_proc(images=[a, b, c], batch_size=1))  # RuntimeError\n\n# after\nproc = process_images(img2img_proc(images=[a, b, c], batch_size=3))","handlingStrategy":"validation","validationCode":"def prepare_img2img(proc, images):\n    if len(images) > proc.batch_size:\n        proc.batch_size = len(images)   # or chunk: images[i:i+proc.batch_size]\n    return proc\n\nproc = prepare_img2img(proc, imgs)","typeGuard":null,"tryCatchPattern":"try:\n    processed = process_images(p)\nexcept RuntimeError as e:\n    if 'bad number of images passed' in str(e):\n        p.batch_size = len(imgs)\n        processed = process_images(p)\n    else:\n        raise","preventionTips":["Chunk image lists into groups of size batch_size before calling img2img.","Keep API payload 'batch_size' >= number of images in the 'images' array."],"tags":["img2img","batch","processing","validation"],"backgroundTag":null,"analyzedSha":"82a973c04367123ae98bd9abdf80d9eda9b910e2","analyzedAt":"2026-08-14T16:46:43.225Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}