{"record":{"id":"c52c90fd69d9dccc","repo":"invoke-ai/InvokeAI","slug":"expected-pretrainedmodel-for-text-encoder-got-ty-c52c90","errorCode":null,"errorMessage":"Expected PreTrainedModel for text encoder, got {type(text_encoder).__name__}. The Qwen3 encoder model may be corrupted or incompatible.","messagePattern":"Expected PreTrainedModel for text encoder, got (.+?)\\. The Qwen3 encoder model may be corrupted or incompatible\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"invokeai/app/invocations/flux2_klein_text_encoder.py","lineNumber":136,"sourceCode":"                f\"Recovered {repaired_tensors} required Qwen3 tensor(s) onto {device} after a partial device mismatch.\"\n            )\n\n        # Apply LoRA models\n        lora_dtype = TorchDevice.choose_bfloat16_safe_dtype(device)\n        exit_stack.enter_context(\n            LayerPatcher.apply_smart_model_patches(\n                model=text_encoder,\n                patches=self._lora_iterator(context),\n                prefix=FLUX_LORA_T5_PREFIX,\n                dtype=lora_dtype,\n                cached_weights=cached_weights,\n            )\n        )\n\n        context.util.signal_progress(\"Running Qwen3 text encoder (Klein)\")\n\n        if not isinstance(text_encoder, PreTrainedModel):\n            raise TypeError(\n                f\"Expected PreTrainedModel for text encoder, got {type(text_encoder).__name__}. \"\n                \"The Qwen3 encoder model may be corrupted or incompatible.\"\n            )\n        if not isinstance(tokenizer, PreTrainedTokenizerBase):\n            raise TypeError(\n                f\"Expected PreTrainedTokenizerBase for tokenizer, got {type(tokenizer).__name__}. \"\n                \"The Qwen3 tokenizer may be corrupted or incompatible.\"\n            )\n\n        messages = [{\"role\": \"user\", \"content\": prompt}]\n\n        text: str = tokenizer.apply_chat_template(  # type: ignore[assignment]\n            messages,\n            tokenize=False,\n            add_generation_prompt=True,\n            enable_thinking=False,\n        )\n","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/invocations/flux2_klein_text_encoder.py#L118-L154","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before (corrupt/mismatched encoder)\nModelConfig(type='main', base='Flux2', path='/models/qwen3-encoder-wrong/')\n// after\nModelConfig(type='main', base='Flux2', path='/models/Qwen/Qwen3-encoder/', name='Qwen3 encoder')","handlingStrategy":"type-guard","validationCode":"from invokeai.backend.model_manager.load import ModelLoaderRegistry\ninfo = context.models.load(qwen3_encoder.text_encoder)\nif not isinstance(info.model, PreTrainedModel):\n    raise TypeError(f'Qwen3 encoder invalid: {type(info.model).__name__}')","typeGuard":"from transformers import PreTrainedModel\n\ndef is_qwen3_encoder(obj) -> bool:\n    return isinstance(obj, PreTrainedModel)","tryCatchPattern":"try:\n    result = klein_encoder.invoke(context)\nexcept TypeError as e:\n    if 'PreTrainedModel for text encoder' in str(e):\n        reimport_model_manager_entry(qwen3_encoder.text_encoder)\n    raise","preventionTips":["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"],"tags":["model-loading","type-check","transformers"],"backgroundTag":"model-type-mismatch","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}