{"record":{"id":"a3338a9c174db982","repo":"Comfy-Org/ComfyUI","slug":"mask-and-image-must-be-the-same-size-a3338a","errorCode":null,"errorMessage":"Mask and Image must be the same size","messagePattern":"Mask and Image must be the same size","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"comfy_api_nodes/nodes_openai.py","lineNumber":219,"sourceCode":"        validate_string(prompt, strip_whitespace=False)\n        model = \"dall-e-2\"\n        path = \"/proxy/openai/images/generations\"\n        content_type = \"application/json\"\n        request_class = OpenAIImageGenerationRequest\n        img_binary = None\n\n        if image is not None and mask is not None:\n            path = \"/proxy/openai/images/edits\"\n            content_type = \"multipart/form-data\"\n            request_class = OpenAIImageEditRequest\n\n            input_tensor = image.squeeze().cpu()\n            height, width, channels = input_tensor.shape\n            rgba_tensor = torch.ones(height, width, 4, device=\"cpu\")\n            rgba_tensor[:, :, :channels] = input_tensor\n\n            if mask.shape[1:] != image.shape[1:-1]:\n                raise Exception(\"Mask and Image must be the same size\")\n            rgba_tensor[:, :, 3] = 1 - mask.squeeze().cpu()\n\n            rgba_tensor = downscale_image_tensor(rgba_tensor.unsqueeze(0)).squeeze()\n\n            image_np = (rgba_tensor.numpy() * 255).astype(np.uint8)\n            img = Image.fromarray(image_np)\n            img_byte_arr = BytesIO()\n            img.save(img_byte_arr, format=\"PNG\")\n            img_byte_arr.seek(0)\n            img_binary = img_byte_arr  # .getvalue()\n            img_binary.name = \"image.png\"\n        elif image is not None or mask is not None:\n            raise Exception(\"Dall-E 2 image editing requires an image AND a mask\")\n\n        response = await sync_op(\n            cls,\n            ApiEndpoint(path=path, method=\"POST\"),\n            response_model=OpenAIImageGenerationResponse,","sourceCodeStart":201,"sourceCodeEnd":237,"githubUrl":"https://github.com/Comfy-Org/ComfyUI/blob/1c6d8d45b3693bfbb32385b410d813a7fd6be216/comfy_api_nodes/nodes_openai.py#L201-L237","documentation":"Thrown by the Dall-E 2 image-edit path in nodes_openai.py when the supplied mask tensor's spatial dimensions do not match the input image's. The RGBA edit payload is built from the image and the mask is written into its alpha channel, so mismatched sizes would corrupt the payload. Checked as mask.shape[1:] != image.shape[1:-1].","triggerScenarios":"Calling OpenAIImageEdit (Dall-E 2 path) with a mask loaded at a different resolution than the image (e.g. 512x512 image with a 1024x1024 mask), or masks coming from a different workflow branch.","commonSituations":"Loading a mask PNG exported at another size; using an upscaled/downscaled image while keeping the original mask; forgetting that LoadImage returns the mask at the image file's own resolution.","solutions":["Resize the mask to exactly the image's HxW before connecting it (e.g. a resize node matching the image)","Load the mask from the same LoadImage node output so dimensions stay coupled","Verify shapes: mask.shape[1:] == image.shape[1:-1] before the edit call","If the mask must come from elsewhere, generate it from the image itself (e.g. MaskEditor) so it inherits size"],"exampleFix":"// before\nimage  # (1, 1024, 1024, 3)\nmask    # (1, 512, 512)\n// after\nimport torch\nmask = torch.nn.functional.interpolate(mask.unsqueeze(1).float(), size=image.shape[1:3], mode='nearest').squeeze(1)","handlingStrategy":"validation","validationCode":"assert mask.shape[1:] == image.shape[1:-1], f\"mask {tuple(mask.shape)} != image {tuple(image.shape)}\"","typeGuard":"def mask_matches_image(mask, image) -> bool:\n    return mask.shape[1:] == image.shape[1:-1]","tryCatchPattern":null,"preventionTips":["Always derive masks from the same LoadImage as the image","Resize mask and image together when changing resolution","Assert shape equality in a small check node before the edit node"],"tags":["openai","dalle2","mask","image-edit","dimensions"],"backgroundTag":null,"analyzedSha":"1c6d8d45b3693bfbb32385b410d813a7fd6be216","analyzedAt":"2026-08-14T19:37:18.893Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}