invoke-ai/InvokeAI · error · TypeError

Expected ModelPatchRaw for LoRA '{lora.lora.key}', got {type

Error message

Expected ModelPatchRaw for LoRA '{lora.lora.key}', got {type(lora_info.model).__name__}. The LoRA model may be corrupted or incompatible.

What it means

LoRAs applied to the Qwen3 text encoder must be loaded as a ModelPatchRaw object; context.models.load() returned something else. InvokeAI raises this TypeError because a non-patch object cannot be used as a LoRA patch spec, usually meaning the model file is corrupt, the wrong format, or incompatible with this loader.

Source

Thrown at invokeai/app/invocations/z_image_text_encoder.py:205

            # Z-Image expects a 2D tensor [seq_len, hidden_dim] with only valid tokens
            # Based on diffusers ZImagePipeline implementation:
            # embeddings_list.append(prompt_embeds[i][prompt_masks[i]])
            # Since batch_size=1, we take the first item and filter by mask
            prompt_embeds = prompt_embeds[0][prompt_mask[0]]

        if not isinstance(prompt_embeds, torch.Tensor):
            raise TypeError(
                f"Expected torch.Tensor for prompt embeddings, got {type(prompt_embeds).__name__}. "
                "Text encoder returned unexpected type."
            )
        return prompt_embeds

    def _lora_iterator(self, context: InvocationContext) -> Iterator[PatchSpec]:
        """Iterate over LoRA models to apply to the Qwen3 text encoder."""
        for lora in self.qwen3_encoder.loras:
            lora_info = context.models.load(lora.lora)
            if not isinstance(lora_info.model, ModelPatchRaw):
                raise TypeError(
                    f"Expected ModelPatchRaw for LoRA '{lora.lora.key}', got {type(lora_info.model).__name__}. "
                    "The LoRA model may be corrupted or incompatible."
                )
            yield (lora_info.model, lora.weight, lora_info.model_in_ram())

View on GitHub (pinned to 0b6a024f2f)

Solutions

  1. Remove and re-import the LoRA through InvokeAI's model manager so it is scanned and converted to the expected raw-patch format
  2. Verify the LoRA file targets the Qwen3/Z-Image text encoder architecture, not SD or FLUX weights
  3. Re-download the LoRA file; check integrity (file size / safetensors header) to rule out truncation
  4. Delete stale model-manager DB entries pointing at the old file and rescan the model directory
Defensive patterns

Strategy: type-guard

Validate before calling

lora_info = context.models.load(lora.lora)
assert isinstance(lora_info.model, ModelPatchRaw), type(lora_info.model)

Type guard

from invokeai.backend.model_manager.load.model_util import ModelPatchRaw  # adjust import to project

def is_valid_lora(obj) -> bool:
    return isinstance(obj, ModelPatchRaw)

Try / catch

try:
    patches = list(self._lora_iterator(context))
except TypeError as e:
    if 'ModelPatchRaw' in str(e):
        logger.error(f"Skipping incompatible LoRA: {e}")
    else:
        raise

Prevention

When it happens

Trigger: Adding a LoRA to self.qwen3_encoder.loras whose backing file is not a valid ModelPatchRaw (e.g. a full checkpoint instead of a LoRA delta, a truncated download, or a LoRA saved for a different architecture/loader that deserializes to another type).

Common situations: Manually copied LoRA files in the LoRA directory; LoRAs converted for a different base model (SD/FLUX) being applied to the Qwen3 encoder; partially downloaded or corrupted safetensors; stale model-manager records pointing at wrong files.

Related errors


AI-assisted analysis of invoke-ai/InvokeAI@0b6a024f2f (2026-08-29). Data as JSON: /api/errors/58a878a30eb68ac7. Report an issue: GitHub.