{"record":{"id":"87e0c15cdcb6365c","repo":"invoke-ai/InvokeAI","slug":"wan-latents-to-image-expects-a-4d-or-5d-latent-ten","errorCode":null,"errorMessage":"Wan latents-to-image expects a 4D or 5D latent tensor [B, C, (T), H, W]; got {tuple(latents.shape)}.","messagePattern":"Wan latents-to-image expects a 4D or 5D latent tensor \\[B, C, \\(T\\), H, W\\]; got (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"invokeai/app/invocations/wan_latents_to_image.py","lineNumber":54,"sourceCode":"    \"wan_l2i\",\n    title=\"Latents to Image - Wan 2.2\",\n    tags=[\"latents\", \"image\", \"vae\", \"l2i\", \"wan\"],\n    category=\"latents\",\n    version=\"1.0.0\",\n    classification=Classification.Prototype,\n)\nclass WanLatentsToImageInvocation(BaseInvocation, WithMetadata, WithBoard):\n    \"\"\"Decodes Wan latents back to RGB.\"\"\"\n\n    latents: LatentsField = InputField(description=FieldDescriptions.latents, input=Input.Connection)\n    vae: VAEField = InputField(description=FieldDescriptions.vae, input=Input.Connection)\n\n    @torch.no_grad()\n    def invoke(self, context: InvocationContext) -> ImageOutput:\n        latents = context.tensors.load(self.latents.latents_name)\n\n        if latents.ndim not in (4, 5):\n            raise ValueError(\n                f\"Wan latents-to-image expects a 4D or 5D latent tensor [B, C, (T), H, W]; got {tuple(latents.shape)}.\"\n            )\n        if latents.shape[0] != 1:\n            raise ValueError(f\"Wan latents-to-image requires batch size 1; got {latents.shape[0]}.\")\n\n        # This node decodes exactly one image. Multi-frame video latents would otherwise\n        # run the full (expensive) multi-frame VAE decode — under a working-memory\n        # estimate that assumed one frame — and then die in an opaque einops rank error\n        # at the final rearrange. Checked before the VAE is even loaded.\n        if latents.ndim == 5 and latents.shape[2] != 1:\n            raise ValueError(\n                f\"These latents hold {latents.shape[2]} frames of video; this node decodes a single \"\n                \"image. Use 'Latents to Video - Wan 2.2' (wan_l2v) for video latents.\"\n            )\n\n        vae_info = context.models.load(self.vae.vae)\n        if not isinstance(vae_info.model, AutoencoderKLWan):\n            raise TypeError(f\"Expected AutoencoderKLWan for Wan VAE, got {type(vae_info.model).__name__}.\")","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/invocations/wan_latents_to_image.py#L36-L72","documentation":"Wan Latents to Image decodes a single image and expects the latents tensor to be 4D [B,C,H,W] or 5D [B,C,T,H,W]. Any other rank (2D, 3D, 6D, etc.) cannot be interpreted, so the node raises with the actual shape. This catches feeding incompatible latents from other pipelines.","triggerScenarios":"Loading latents produced by a non-Wan node or with an unexpected rank into the Wan latents-to-image node; passing preview/noise tensors or manually truncated tensors whose ndim is not 4 or 5.","commonSituations":"Wiring standard SD latents-to-image tensors into the Wan node; corrupt or hand-edited tensor files; intermediate debug tensors with squeezed batch/channel dims.","solutions":["Supply latents from Wan Image to Latents or a Wan denoise node ([B,C,H,W] or [B,C,1,H,W])","Check the upstream node type — replace non-Wan latents-to-image with the Wan one","Inspect the tensor with tensor.shape; reshape/pad to rank 4 or 5 before decoding"],"exampleFix":"// before\nlatents.shape == (C, H, W)  // ndim=3 -> error\n// after\nlatents = latents.unsqueeze(0)  // (1, C, H, W)","handlingStrategy":"type-guard","validationCode":"if latents.ndim not in (4, 5):\n    raise ValueError(f\"expected 4D/5D latents, got ndim={latents.ndim} shape={tuple(latents.shape)}\")","typeGuard":"def is_valid_wan_latents(t: torch.Tensor) -> bool:\n    return t.ndim in (4, 5) and t.shape[0] == 1","tryCatchPattern":"try:\n    out = wan_latents_to_image.invoke(context)\nexcept ValueError as e:\n    if '4D or 5D latent tensor' in str(e):\n        latents = latents.unsqueeze(0)  # reshape as appropriate\n    else:\n        raise","preventionTips":["Source latents only from Wan nodes","Check tensor.shape before passing between nodes","Keep batch and channel dims intact through intermediate steps"],"tags":["invokeai","wan","shape-mismatch","tensor-rank"],"backgroundTag":"tensor-rank-mismatch","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}