{"record":{"id":"1708eb5a8e628608","repo":"Comfy-Org/ComfyUI","slug":"number-of-texts-len-texts-does-not-match-numb","errorCode":null,"errorMessage":"Number of texts ({len(texts)}) does not match number of images ({num_images}). Text list should have length {num_images}, 1, or 0.","messagePattern":"Number of texts \\((.+?)\\) does not match number of images \\((.+?)\\)\\. Text list should have length (.+?), 1, or 0\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"comfy_extras/nodes_dataset.py","lineNumber":1901,"sourceCode":"\n    @classmethod\n    def execute(cls, images, vae, clip, texts=None):\n        # Extract scalars (vae and clip are single values wrapped in lists)\n        vae = vae[0]\n        clip = clip[0]\n\n        # Handle text list\n        num_images = len(images)\n\n        if texts is None or len(texts) == 0:\n            # Treat as [\"\"] for unconditional training\n            texts = [\"\"]\n\n        if len(texts) == 1 and num_images > 1:\n            # Repeat single text for all images\n            texts = texts * num_images\n        elif len(texts) != num_images:\n            raise ValueError(\n                f\"Number of texts ({len(texts)}) does not match number of images ({num_images}). \"\n                f\"Text list should have length {num_images}, 1, or 0.\"\n            )\n\n        # Encode images with VAE\n        logging.info(f\"Encoding {num_images} images with VAE...\")\n        latents_list = []  # list[{\"samples\": tensor}]\n        for img_tensor in images:\n            # img_tensor is [1, H, W, 3]\n            latent_tensor = vae.encode(img_tensor[:, :, :, :3])\n            latents_list.append({\"samples\": latent_tensor})\n\n        # Encode texts with CLIP\n        logging.info(f\"Encoding {len(texts)} texts with CLIP...\")\n        conditioning_list = []  # list[list[cond]]\n        for text in texts:\n            if text == \"\":\n                cond = clip.encode_from_tokens_scheduled(clip.tokenize(\"\"))","sourceCodeStart":1883,"sourceCodeEnd":1919,"githubUrl":"https://github.com/Comfy-Org/ComfyUI/blob/1c6d8d45b3693bfbb32385b410d813a7fd6be216/comfy_extras/nodes_dataset.py#L1883-L1919","documentation":"Thrown by the image+text dataset-encoding node when the text list length is neither 0/None (treated as unconditional), nor 1 (broadcast to all images), nor exactly the number of images. The node intentionally supports only these three shapes before it VAE-encodes the images.","triggerScenarios":"Passing e.g. 5 captions for 7 images, or 7 captions for 5 images, into the texts input. Also passing a list-of-lists or nested structure that changes len(texts).","commonSituations":"Caption files (txt sidecars, CSV columns) that don't align 1:1 with images after filtering; manually curated caption lists where entries were added or deleted; image lists modified by resolution filtering after captions were loaded.","solutions":["Make len(texts) equal len(images), or pass exactly one caption to broadcast, or an empty list/None for unconditional.","Rebuild the caption list from the same filtered image list (zip images and caption files together when loading).","Print both lengths before the node to find which side drifted."],"exampleFix":"# before\ntexts = load_captions(folder)          # 5 entries\nimages = load_images(folder)            # 7 after filtering\n# after\nimages = load_images(folder)\ntexts = [captions[img.stem] for img in images]  # guaranteed same length","handlingStrategy":"validation","validationCode":"n = len(images)\nif texts is None or len(texts) == 0:\n    texts = ['']            # unconditional\nelif len(texts) == 1 and n > 1:\n    texts = texts * n       # broadcast\nassert len(texts) == n, f'texts={len(texts)} but images={n}'","typeGuard":"def texts_match_images(texts: list[str] | None, n: int) -> bool:\n    return texts is None or len(texts) in (0, 1, n)","tryCatchPattern":null,"preventionTips":["Build the caption list by zipping over the final (filtered) image list so lengths cannot drift.","Remember the three accepted shapes: 0/None, 1, or exactly len(images).","Re-generate captions after changing any image filter."],"tags":["comfyui","training","captions","validation"],"backgroundTag":null,"analyzedSha":"1c6d8d45b3693bfbb32385b410d813a7fd6be216","analyzedAt":"2026-08-14T19:37:18.893Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}