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 ModelPatchRaw objects. If context.models.load returns a different model type for the LoRA, the file is not a FLUX.2-compatible LoRA patch or is corrupted, and it cannot be applied to the encoder.
Source
Thrown at invokeai/app/invocations/flux2_klein_text_encoder.py:205
out = out.to(dtype=text_encoder.dtype, device=device)
batch_size, num_channels, seq_len, hidden_dim = out.shape
prompt_embeds = out.permute(0, 2, 1, 3).reshape(batch_size, seq_len, num_channels * hidden_dim)
last_hidden_state = outputs.hidden_states[-1]
expanded_mask = attention_mask.unsqueeze(-1).expand_as(last_hidden_state).float()
sum_embeds = (last_hidden_state * expanded_mask).sum(dim=1)
num_tokens = expanded_mask.sum(dim=1).clamp(min=1)
pooled_embeds = sum_embeds / num_tokens
return prompt_embeds, pooled_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
- Use a FLUX.2/Klein-compatible LoRA converted for InvokeAI
- Re-convert/re-import the LoRA so the model manager stores it as ModelPatchRaw
- Check the LoRA model record's type/hash points to the intended file
- Re-download the LoRA if the file is corrupted
Example fix
// before: SDXL LoRA wired into qwen3_encoder.loras loras: [ModelIdentifierField(key='sdxl-lora-abc123')] // after: FLUX.2-compatible LoRA loras: [ModelIdentifierField(key='flux2-klein-lora-xyz789')]
Defensive patterns
Strategy: type-guard
Validate before calling
info = context.models.load(lora_field)
if not isinstance(info.model, ModelPatchRaw):
raise TypeError(f'{lora_field.key} is not a FLUX.2 LoRA patch') Type guard
from invokeai.backend.model_patcher import ModelPatchRaw
def is_lora_patch(info) -> bool:
return isinstance(info.model, ModelPatchRaw) Try / catch
try:
result = klein_encoder.invoke(context)
except TypeError as e:
if 'ModelPatchRaw' in str(e):
convert_lora_to_flux2_format(e)
raise Prevention
- Only add LoRAs tagged for FLUX.2/Klein to encoder LoRA lists
- Re-convert foreign-format LoRAs before use
- Verify LoRA records after model-manager migrations
When it happens
Trigger: A LoRA listed in self.qwen3_encoder.loras resolves, via context.models.load(lora.lora), to a model that is not ModelPatchRaw in _lora_iterator; e.g. the record points at a checkpoint-format LoRA or an entirely different model file.
Common situations: Using SD/SDXL LoRA files with FLUX.2 Klein; LoRA converted to a format InvokeAI stores differently; stale model-manager records after conversion; corrupted LoRA downloads.
Related errors
- Expected ModelPatchRaw for LoRA '{lora.lora.key}', got {type
- Expected PreTrainedModel for text encoder, got {type(text_en
- Expected PreTrainedTokenizerBase for tokenizer, got {type(to
- Expected PreTrainedModel for Gemma encoder, got {type(gemma_
- Expected PreTrainedTokenizerBase for Gemma tokenizer, got {t
AI-assisted analysis of invoke-ai/InvokeAI@0b6a024f2f (2026-08-29).
Data as JSON: /api/errors/544db5ca74a9f741.
Report an issue: GitHub.