invoke-ai/InvokeAI · error · TypeError
Expected PreTrainedModel for text encoder, got {type(text_en
Error message
Expected PreTrainedModel for text encoder, got {type(text_encoder).__name__}. The Qwen3 encoder model may be corrupted or incompatible. What it means
InvokeAI's FLUX.2 Klein text encoder invocation requires the loaded Qwen3 text encoder to be a transformers PreTrainedModel instance. If the object loaded from the model manager is any other type, the model file is likely corrupted, partially downloaded, or is not actually a Qwen3 encoder (incompatible model directory). The check guards downstream forward passes that assume the transformers API.
Source
Thrown at invokeai/app/invocations/flux2_klein_text_encoder.py:136
f"Recovered {repaired_tensors} required Qwen3 tensor(s) onto {device} after a partial device mismatch."
)
# Apply LoRA models
lora_dtype = TorchDevice.choose_bfloat16_safe_dtype(device)
exit_stack.enter_context(
LayerPatcher.apply_smart_model_patches(
model=text_encoder,
patches=self._lora_iterator(context),
prefix=FLUX_LORA_T5_PREFIX,
dtype=lora_dtype,
cached_weights=cached_weights,
)
)
context.util.signal_progress("Running Qwen3 text encoder (Klein)")
if not isinstance(text_encoder, PreTrainedModel):
raise TypeError(
f"Expected PreTrainedModel for text encoder, got {type(text_encoder).__name__}. "
"The Qwen3 encoder model may be corrupted or incompatible."
)
if not isinstance(tokenizer, PreTrainedTokenizerBase):
raise TypeError(
f"Expected PreTrainedTokenizerBase for tokenizer, got {type(tokenizer).__name__}. "
"The Qwen3 tokenizer may be corrupted or incompatible."
)
messages = [{"role": "user", "content": prompt}]
text: str = tokenizer.apply_chat_template( # type: ignore[assignment]
messages,
tokenize=False,
add_generation_prompt=True,
enable_thinking=False,
)
View on GitHub (pinned to 0b6a024f2f)
Solutions
- Re-download the Qwen3 text encoder model (delete the model folder and re-add it in InvokeAI model manager)
- Verify the model config points to a genuine Qwen3 encoder directory containing config.json and safetensors weights
- Check that installed transformers version supports Qwen3 and returns PreTrainedModel instances
- Re-run 'invokeai-migrate' / model scan to repair broken model records
Example fix
// before (corrupt/mismatched encoder) ModelConfig(type='main', base='Flux2', path='/models/qwen3-encoder-wrong/') // after ModelConfig(type='main', base='Flux2', path='/models/Qwen/Qwen3-encoder/', name='Qwen3 encoder')
Defensive patterns
Strategy: type-guard
Validate before calling
from invokeai.backend.model_manager.load import ModelLoaderRegistry
info = context.models.load(qwen3_encoder.text_encoder)
if not isinstance(info.model, PreTrainedModel):
raise TypeError(f'Qwen3 encoder invalid: {type(info.model).__name__}') Type guard
from transformers import PreTrainedModel
def is_qwen3_encoder(obj) -> bool:
return isinstance(obj, PreTrainedModel) Try / catch
try:
result = klein_encoder.invoke(context)
except TypeError as e:
if 'PreTrainedModel for text encoder' in str(e):
reimport_model_manager_entry(qwen3_encoder.text_encoder)
raise Prevention
- Verify model integrity hashes after download
- Keep transformers updated to a Qwen3-compatible version
- Point configs only at verified Qwen3 encoder directories
- Re-import models that fail integrity checks
When it happens
Trigger: context.models.load() returns an object whose class is not PreTrainedModel when _encode_prompt places the Qwen3 encoder on device; e.g. the model folder points to a non-Qwen model, files are truncated/corrupt, or a loader fallback returned a raw module.
Common situations: Interrupted model downloads leaving incomplete safetensors files; users pointing a FLUX.2 Klein model config at the wrong encoder directory; transformers version changes causing a different wrapper class to be loaded; hash-mismatched model installs.
Related errors
- Expected PreTrainedModel for Gemma encoder, got {type(gemma_
- Expected LlavaOnevisionForConditionalGeneration, got {type(m
- Expected PreTrainedTokenizerBase for tokenizer, got {type(to
- Expected ModelPatchRaw for LoRA '{lora.lora.key}', got {type
- Expected PreTrainedTokenizerBase for Gemma tokenizer, got {t
AI-assisted analysis of invoke-ai/InvokeAI@0b6a024f2f (2026-08-29).
Data as JSON: /api/errors/c52c90fd69d9dccc.
Report an issue: GitHub.