{"record":{"id":"b339957a1a8f61b0","repo":"invoke-ai/InvokeAI","slug":"wan-reference-condition-requires-expected-got","errorCode":null,"errorMessage":"Wan reference condition requires {expected}; got {condition.shape[2]}.","messagePattern":"Wan reference condition requires (.+?); got (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"invokeai/app/invocations/wan_denoise.py","lineNumber":111,"sourceCode":"\n\ndef _validate_ref_condition_shape(\n    condition: torch.Tensor,\n    *,\n    channels: int,\n    frames: int,\n    height: int,\n    width: int,\n) -> None:\n    if condition.ndim != 5:\n        raise ValueError(f\"Wan reference condition must be a 5D tensor; got shape {tuple(condition.shape)}.\")\n    if condition.shape[0] != 1:\n        raise ValueError(f\"Wan reference condition requires batch size 1; got {condition.shape[0]}.\")\n    if condition.shape[1] != channels:\n        raise ValueError(f\"Wan reference condition requires {channels} channels; got {condition.shape[1]}.\")\n    if condition.shape[2] != frames:\n        expected = \"a single latent frame\" if frames == 1 else f\"{frames} latent frames\"\n        raise ValueError(f\"Wan reference condition requires {expected}; got {condition.shape[2]}.\")\n    if condition.shape[3:] != (height, width):\n        raise ValueError(\n            f\"Wan reference condition requires {width}x{height} latent spatial dimensions; \"\n            f\"got {condition.shape[4]}x{condition.shape[3]}.\"\n        )\n\n\ndef _scheduler_path_for_transformer(context: InvocationContext, transformer_field: WanTransformerField) -> Path | None:\n    \"\"\"Return the on-disk ``scheduler/`` directory for the main model, or None.\"\"\"\n    config = context.models.get_config(transformer_field.transformer)\n    model_root = context.models.get_absolute_path(config)\n    if model_root.is_file():\n        return None\n    candidate = model_root / \"scheduler\"\n    if (candidate / \"scheduler_config.json\").exists():\n        return candidate\n    return None\n","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/invocations/wan_denoise.py#L93-L129","documentation":"The frame (temporal) dimension of the reference condition, shape[2], must equal the number of latent frames the denoise run expects (1 for a single reference image frame, otherwise the computed latent frame count of the video). Mismatches are rejected with a message stating the expected count.","triggerScenarios":"Passing a multi-frame video latent as a ref condition where a single latent frame is expected (or vice versa), misconfiguring the latent frame count / num_frames so the computed latent frames differ from the condition's, Wan 2.2 latent temporal compression (4x) miscalculations.","commonSituations":"Using an image-latent squeezed into a 5D shape with T>1 by mistake, supplying a clip's full latent sequence as a reference for a short denoise, off-by-one in frames-to-latent-frames conversion ((frames-1)/4+1 style formulas).","solutions":["Slice the condition to the expected latent frames: condition[:, :, :frames]","For a single reference image, ensure the temporal dim is exactly 1 (e.g. take latents[:, :, 0:1])","Recompute the expected latent frame count from the requested video frames and match it","Regenerate the condition with the correct num_frames in the conditioning node"],"exampleFix":"// before\ncondition = video_latents  # T = 21 latent frames, expected 1\n// after\ncondition = video_latents[:, :, :1]  # single latent frame","handlingStrategy":"validation","validationCode":"expected_latent_frames = 1 if is_reference_image else (num_frames - 1) // 4 + 1  # per variant's temporal compression\nif condition.shape[2] != expected_latent_frames:\n    condition = condition[:, :, :expected_latent_frames]","typeGuard":"def has_expected_frames(t, frames: int) -> bool:\n    import torch\n    return isinstance(t, torch.Tensor) and t.ndim == 5 and t.shape[2] == frames","tryCatchPattern":"try:\n    result = denoise.invoke(context)\nexcept ValueError as e:\n    if \"latent frame\" in str(e):\n        expected = int(str(e).split('requires ')[1].split(' latent')[0]) if 'latent frames' in str(e) else 1\n        denoise.ref_condition = denoise.ref_condition[:, :, :expected]\n        result = denoise.invoke(context)\n    else:\n        raise","preventionTips":["Compute latent frames with the variant's temporal compression factor","For single-image refs, slice latents to exactly one temporal frame","Keep num_frames consistent between conditioning and denoise nodes","Re-encode conditions whenever the output frame count changes"],"tags":["wan","tensor-shape","temporal"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}