{"record":{"id":"54ffe594351cc634","repo":"invoke-ai/InvokeAI","slug":"source-is-missing-key-after-prefix-strip-and-k","errorCode":null,"errorMessage":"{source} is missing {key} after prefix strip and key conversion","messagePattern":"(.+?) is missing (.+?) after prefix strip and key conversion","errorType":"validation","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"invokeai/backend/model_manager/load/model_loaders/wan.py","lineNumber":342,"sourceCode":"    release is described by its own weights rather than by a hard-coded table of\n    known repos.\n\n    ``source`` only flavours the error messages.\n    \"\"\"\n    num_layers = 0\n    for key in sd.keys():\n        if isinstance(key, str) and key.startswith(\"blocks.\"):\n            parts = key.split(\".\")\n            if len(parts) >= 2:\n                try:\n                    num_layers = max(num_layers, int(parts[1]) + 1)\n                except ValueError:\n                    pass\n\n    def require(key: str) -> tuple[int, ...]:\n        tensor = sd.get(key)\n        if tensor is None:\n            raise RuntimeError(f\"{source} is missing {key} after prefix strip and key conversion\")\n        return _tensor_shape(tensor)\n\n    # Patch embedding gives us in_channels (16/36=A14B, 48=TI2V-5B) and inner dim.\n    patch_shape = require(\"patch_embedding.weight\")\n    inner_dim = patch_shape[0]\n    in_channels = patch_shape[1]\n\n    # Wan uses head_dim=128 throughout the family; num_heads = inner_dim / 128.\n    attention_head_dim = 128\n    num_attention_heads = inner_dim // attention_head_dim\n\n    ffn_dim = require(\"blocks.0.ffn.net.0.proj.weight\")[0]\n\n    text_w = sd.get(\"condition_embedder.text_embedder.linear_1.weight\")\n    text_dim = _tensor_shape(text_w)[1] if text_w is not None else 4096\n\n    # out_channels is read from proj_out.weight directly rather than assumed\n    # equal to in_channels: I2V-A14B has in_channels=36 (16 noise + 16","sourceCodeStart":324,"sourceCodeEnd":360,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/backend/model_manager/load/model_loaders/wan.py#L324-L360","documentation":"_build_wan_transformer_config probes specific tensor keys (starting with patch_embedding.weight) to infer architecture parameters. If, after stripping diffusers prefixes and converting key names, a required tensor is absent from the state dict, the local `require` closure raises RuntimeError naming the missing key.","triggerScenarios":"Loading a single-file Wan checkpoint whose state dict lacks expected keys like patch_embedding.weight — e.g., a non-transformer file, a checkpoint with entirely different naming conventions, or a GGUF/compressed file misdetected as a standard checkpoint.","commonSituations":"Pointing a Wan checkpoint model record at a VAE or T5 encoder file by mistake; exotic community repackaging with unfamiliar key layout; corrupt or partially written safetensors file.","solutions":["Verify the file actually contains Wan transformer weights (not a VAE/text-encoder or unrelated model).","Re-download the checkpoint; a truncated file can lose early tensors.","Compare the file's key names against expected Wan keys; if naming is nonstandard, use a repackaged diffusers-compatible checkpoint.","Update InvokeAI so the latest prefix-strip/key-conversion rules apply."],"exampleFix":"// before: wrong file registered as transformer\npath = \"wan_vae.safetensors\"  # no patch_embedding.weight\n\n// after\npath = \"wan2.1_t2v_1.3b_transformer.safetensors\"","handlingStrategy":"validation","validationCode":"from safetensors import safe_open\n\ndef validate_has_patch_embedding(path):\n    with safe_open(path, framework='pt') as f:\n        keys = set(f.keys())\n    if not any('patch_embedding.weight' in k for k in keys):\n        raise ValueError(f\"{path} is not a Wan transformer checkpoint (no patch_embedding.weight)\")","typeGuard":null,"tryCatchPattern":"try:\n    model = loader.load_model(config, SubModelType.Transformer)\nexcept RuntimeError as e:\n    if 'after prefix strip and key conversion' in str(e):\n        verify_file_is_wan_transformer(config.path)  # correct the record or re-download\n    else:\n        raise","preventionTips":["Ensure model records point at transformer checkpoints, not VAE/T5 files.","Re-download files whose size differs from the published size.","Prefer diffusers-standard key naming repackaging for community checkpoints.","Update InvokeAI for the newest key-mapping rules."],"tags":["state-dict","checkpoint","model-loading","wan"],"backgroundTag":"state-dict-missing-keys","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}