{"record":{"id":"26895a22e81412e6","repo":"Lightning-AI/pytorch-lightning","slug":"expected-n-items-but-only-found-len-v-for-k","errorCode":null,"errorMessage":"Expected {n} items but only found {len(v)} for {k}","messagePattern":"Expected (.+?) items but only found (.+?) for (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/loggers/wandb.py","lineNumber":496,"sourceCode":"        Can be defined either with `columns` and `data` or with `dataframe`.\n\n        \"\"\"\n\n        self.log_table(key, columns, data, dataframe, step)\n\n    @rank_zero_only\n    def log_image(self, key: str, images: list[Any], step: Optional[int] = None, **kwargs: Any) -> None:\n        \"\"\"Log images (tensors, numpy arrays, PIL Images or file paths).\n\n        Optional kwargs are lists passed to each image (ex: caption, masks, boxes).\n\n        \"\"\"\n        if not isinstance(images, list):\n            raise TypeError(f'Expected a list as \"images\", found {type(images)}')\n        n = len(images)\n        for k, v in kwargs.items():\n            if len(v) != n:\n                raise ValueError(f\"Expected {n} items but only found {len(v)} for {k}\")\n        kwarg_list = [{k: kwargs[k][i] for k in kwargs} for i in range(n)]\n\n        import wandb\n\n        metrics = {key: [wandb.Image(img, **kwarg) for img, kwarg in zip(images, kwarg_list)]}\n        self.log_metrics(metrics, step)  # type: ignore[arg-type]\n\n    @rank_zero_only\n    def log_audio(self, key: str, audios: list[Any], step: Optional[int] = None, **kwargs: Any) -> None:\n        r\"\"\"Log audios (numpy arrays, or file paths).\n\n        Args:\n            key: The key to be used for logging the audio files\n            audios: The list of audio file paths, or numpy arrays to be logged\n            step: The step number to be used for logging the audio files\n            \\**kwargs: Optional kwargs are lists passed to each ``Wandb.Audio`` instance (ex: caption, sample_rate).\n\n        Optional kwargs are lists passed to each audio (ex: caption, sample_rate).","sourceCodeStart":478,"sourceCodeEnd":514,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/loggers/wandb.py#L478-L514","documentation":"In WandbLogger.log_image, every kwarg (e.g. caption, masks) must be a list whose length equals len(images), because items are zipped positionally to build wandb.Image objects. A length mismatch raises this ValueError naming the offending kwarg.","triggerScenarios":"logger.log_image([img1, img2, img3], caption=[\"only one caption\"]) — 3 images but a 1-element caption list; same for masks/boxes lists of any differing length.","commonSituations":"Passing a single string caption for multiple images, or reusing a captions list computed for a previous batch size.","solutions":["Make each kwarg list length equal len(images)","For a shared caption, replicate it: caption=[c] * len(images)","Compute captions per image from the batch before calling"],"exampleFix":"# before\nlogger.log_image([im1, im2], caption=[\"generated\"])\n# after\nlogger.log_image([im1, im2], caption=[\"generated\"] * 2)","handlingStrategy":"validation","validationCode":"n = len(images)\nkwargs = {k: (v * n if isinstance(v, str) else v) for k, v in kwargs.items()}\nassert all(len(v) == n for v in kwargs.values())","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Broadcast scalar captions/sample_rates manually","Build kwargs in the same loop that builds the media list"],"tags":["wandb","lightning","logging","images","length-mismatch"],"backgroundTag":"list-length-mismatch","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}