Comfy-Org/ComfyUI · error · ValueError

Unrecognized optical flow model format: expected a torchvisi

Error message

Unrecognized optical flow model format: expected a torchvision RAFT-large state dict with 'feature_encoder.', 'context_encoder.' and 'update_block.' prefixes.

What it means

The optical flow loader expects a torchvision RAFT-large state dict identifiable by the 'feature_encoder.', 'context_encoder.', and 'update_block.' key prefixes. If none of these prefix families are present the file is not a RAFT checkpoint in the expected layout, and loading it into raft_large() would fail opaquely, so it is rejected up front.

Source

Thrown at comfy_extras/nodes_void.py:90

            ],
            outputs=[
                OpticalFlow.Output(),
            ],
        )

    @classmethod
    def execute(cls, model_name) -> io.NodeOutput:

        model_path = folder_paths.get_full_path_or_raise("optical_flow", model_name)
        sd = comfy.utils.load_torch_file(model_path, safe_load=True)

        has_raft_keys = (
            any(k.startswith("feature_encoder.") for k in sd)
            and any(k.startswith("context_encoder.") for k in sd)
            and any(k.startswith("update_block.") for k in sd)
        )
        if not has_raft_keys:
            raise ValueError(
                "Unrecognized optical flow model format: expected a torchvision "
                "RAFT-large state dict with 'feature_encoder.', 'context_encoder.' "
                "and 'update_block.' prefixes."
            )

        model = raft_large(weights=None, progress=False)
        model.load_state_dict(sd)
        model.eval().to(torch.float32)

        patcher = comfy.model_patcher.ModelPatcher(
            model,
            load_device=comfy.model_management.get_torch_device(),
            offload_device=comfy.model_management.unet_offload_device(),
        )
        return io.NodeOutput(patcher)


class VOIDQuadmaskPreprocess(io.ComfyNode):

View on GitHub (pinned to 1c6d8d45b3)

Solutions

  1. Download the torchvision raft_large weights (e.g. raft_large_C_T_V2) and place them in models/optical_flow
  2. If keys have a wrapper prefix like 'model.', strip it when saving so top-level keys start with feature_encoder/context_encoder/update_block
  3. Inspect the checkpoint keys with torch.load and list(sd.keys()) to confirm the layout

Example fix

# fix a wrapper-prefixed checkpoint
sd = torch.load('flow.pt', map_location='cpu')
sd = { k[len('model.'):]: v for k, v in sd.items() if k.startswith('model.') }
torch.save(sd, 'flow_fixed.pt')
Defensive patterns

Strategy: validation

Validate before calling

sd = comfy.utils.load_torch_file(path, safe_load=True)
need = ("feature_encoder.", "context_encoder.", "update_block.")
if not all(any(k.startswith(p) for k in sd) for p in need):
    raise SystemExit(f"{path} is not a torchvision RAFT-large state dict")

Type guard

def is_raft_large_sd(sd: dict) -> bool:
    return all(any(k.startswith(p) for k in sd) for p in ("feature_encoder.", "context_encoder.", "update_block."))

Prevention

When it happens

Trigger: Placing a non-RAFT flow model (e.g. FlowNet, SpyNet, or a custom architecture), a RAFT checkpoint saved with a wrapper prefix (e.g. 'model.' prefixed keys), or a corrupted/truncated file into models/optical_flow and selecting it.

Common situations: Downloading a RAFT variant whose keys are nested under a module wrapper; converting weights from another framework without stripping prefixes; pointing the node at a miscategorized checkpoint.

Related errors


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