Comfy-Org/ComfyUI · error · ValueError

Number of latents ({len(latents)}) does not match number of

Error message

Number of latents ({len(latents)}) does not match number of conditions ({len(conditioning)}). Something went wrong in dataset preparation.

What it means

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.

Source

Thrown at comfy_extras/nodes_dataset.py:1981

                    tooltip="Number of samples per shard file.",
                    advanced=True,
                ),
            ],
            outputs=[],
        )

    @classmethod
    def execute(cls, latents, conditioning, folder_name, shard_size):
        # Extract scalars
        folder_name = folder_name[0]
        shard_size = shard_size[0]

        # latents: list[{"samples": tensor}]
        # conditioning: list[list[cond]]

        # Validate lengths match
        if len(latents) != len(conditioning):
            raise ValueError(
                f"Number of latents ({len(latents)}) does not match number of conditions ({len(conditioning)}). "
                f"Something went wrong in dataset preparation."
            )

        # Create output directory (inside the datasets root, traversal-safe)
        output_dir = get_dataset_save_dir(folder_name)
        os.makedirs(output_dir, exist_ok=True)

        # Prepare data pairs
        num_samples = len(latents)
        num_shards = (num_samples + shard_size - 1) // shard_size  # Ceiling division

        logging.info(
            f"Saving {num_samples} samples to {num_shards} shards in {output_dir}..."
        )

        # Save data in shards
        for shard_idx in range(num_shards):

View on GitHub (pinned to 1c6d8d45b3)

Solutions

  1. Re-run the full preparation chain end-to-end in one pass so both lists are produced together.
  2. Diff the two lists: log which sample index exists on one side only (by filename/order) and fix that sample.
  3. If a sample fails encoding downstream, catch it before this node and drop it from BOTH lists.
Defensive patterns

Strategy: validation

Validate before calling

if len(latents) != len(conditioning):
    m = min(len(latents), len(conditioning))
    latents, conditioning = latents[:m], conditioning[:m]  # last-resort truncation
    logging.warning('truncated dataset lists to %d aligned samples', m)

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of Comfy-Org/ComfyUI@1c6d8d45b3 (2026-08-14). Data as JSON: /api/errors/587be8f0b5f4a4b2. Report an issue: GitHub.