{"record":{"id":"587be8f0b5f4a4b2","repo":"Comfy-Org/ComfyUI","slug":"number-of-latents-len-latents-does-not-match-587be8","errorCode":null,"errorMessage":"Number of latents ({len(latents)}) does not match number of conditions ({len(conditioning)}). Something went wrong in dataset preparation.","messagePattern":"Number of latents \\((.+?)\\) does not match number of conditions \\((.+?)\\)\\. Something went wrong in dataset preparation\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"comfy_extras/nodes_dataset.py","lineNumber":1981,"sourceCode":"                    tooltip=\"Number of samples per shard file.\",\n                    advanced=True,\n                ),\n            ],\n            outputs=[],\n        )\n\n    @classmethod\n    def execute(cls, latents, conditioning, folder_name, shard_size):\n        # Extract scalars\n        folder_name = folder_name[0]\n        shard_size = shard_size[0]\n\n        # latents: list[{\"samples\": tensor}]\n        # conditioning: list[list[cond]]\n\n        # Validate lengths match\n        if len(latents) != len(conditioning):\n            raise ValueError(\n                f\"Number of latents ({len(latents)}) does not match number of conditions ({len(conditioning)}). \"\n                f\"Something went wrong in dataset preparation.\"\n            )\n\n        # Create output directory (inside the datasets root, traversal-safe)\n        output_dir = get_dataset_save_dir(folder_name)\n        os.makedirs(output_dir, exist_ok=True)\n\n        # Prepare data pairs\n        num_samples = len(latents)\n        num_shards = (num_samples + shard_size - 1) // shard_size  # Ceiling division\n\n        logging.info(\n            f\"Saving {num_samples} samples to {num_shards} shards in {output_dir}...\"\n        )\n\n        # Save data in shards\n        for shard_idx in range(num_shards):","sourceCodeStart":1963,"sourceCodeEnd":1999,"githubUrl":"https://github.com/Comfy-Org/ComfyUI/blob/1c6d8d45b3693bfbb32385b410d813a7fd6be216/comfy_extras/nodes_dataset.py#L1963-L1999","documentation":"Same length invariant as the packing node, but raised at dataset-shard-save time: the latents list and conditioning list handed to the shard writer must be equal length. The message 'Something went wrong in dataset preparation' signals the mismatch was created upstream, not by this node.","triggerScenarios":"Saving shards with a latents list built in one pass (e.g. VAE-encode pass) and a conditioning list built in another pass with different item counts — one image failed encoding, a filter ran on only one side, or buckets were re-run between passes.","commonSituations":"Long dataset-prep workflows where encoding and captioning are separate branches; partial re-runs after a crash (some latents cached, conditioning regenerated); manually merging shard fragments.","solutions":["Re-run the full preparation chain end-to-end in one pass so both lists are produced together.","Diff the two lists: log which sample index exists on one side only (by filename/order) and fix that sample.","If a sample fails encoding downstream, catch it before this node and drop it from BOTH lists."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"if len(latents) != len(conditioning):\n    m = min(len(latents), len(conditioning))\n    latents, conditioning = latents[:m], conditioning[:m]  # last-resort truncation\n    logging.warning('truncated dataset lists to %d aligned samples', m)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never assemble the latents and conditioning lists from separate runs; regenerate together.","Validate lengths immediately after the encode/caption stages, before the shard writer.","Drop failed samples from both lists at the point of failure."],"tags":["comfyui","training","sharding","validation"],"backgroundTag":null,"analyzedSha":"1c6d8d45b3693bfbb32385b410d813a7fd6be216","analyzedAt":"2026-08-14T19:37:18.893Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}