{"record":{"id":"bf11a0854d916504","repo":"invoke-ai/InvokeAI","slug":"expected-pretrainedmodel-for-text-encoder-got-ty-bf11a0","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/z_image_text_encoder.py","lineNumber":107,"sourceCode":"                context.logger.warning(\n                    f\"Recovered {repaired_tensors} required Qwen3 tensor(s) onto {device} after a partial device mismatch.\"\n                )\n\n            # Apply LoRA models to the text encoder\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=Z_IMAGE_LORA_QWEN3_PREFIX,\n                    dtype=lora_dtype,\n                    cached_weights=cached_weights,\n                )\n            )\n\n            context.util.signal_progress(\"Running Qwen3 text encoder\")\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            # Apply chat template similar to diffusers ZImagePipeline\n            # The chat template formats the prompt for the Qwen3 model\n            try:\n                prompt_formatted = tokenizer.apply_chat_template(\n                    [{\"role\": \"user\", \"content\": prompt}],\n                    tokenize=False,\n                    add_generation_prompt=True,\n                    enable_thinking=True,\n                )","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/invocations/z_image_text_encoder.py#L89-L125","documentation":"In z_image_text_encoder.py:_encode_prompt, the loaded Qwen3 text encoder is checked with isinstance(text_encoder, PreTrainedModel) before running. If the model loader returns a different type, a TypeError is raised with the actual class name and a note that the Qwen3 encoder may be corrupted or incompatible. This prevents calling a forward pass on a non-transformers object.","triggerScenarios":"Calling the Z-Image text encoder invocation where context.models.load(...).model_on_device() for the Qwen3 encoder returns an object failing isinstance(..., PreTrainedModel).","commonSituations":"Corrupted/partially downloaded Qwen3 encoder; incompatible conversion of the Qwen3 checkpoint; transformers version mismatch; wrong submodel bound to the text_encoder field of the invocation.","solutions":["Re-download or repair the Qwen3 text encoder model files.","Re-import the Z-Image model ensuring the text_encoder submodel is the standard HF Qwen3 encoder.","Upgrade transformers and InvokeAI to compatible versions so the encoder loads as PreTrainedModel.","Check that the invocation's text_encoder submodel reference points at the intended Qwen3 model."],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":"with text_encoder_info.model_on_device() as (_, enc):\n    if not isinstance(enc, PreTrainedModel):\n        fail_fast(enc)","typeGuard":"def is_pretrained_model(obj) -> bool:\n    from transformers import PreTrainedModel\n    return isinstance(obj, PreTrainedModel)","tryCatchPattern":"try:\n    encode(context)\nexcept TypeError as e:\n    if \"Expected PreTrainedModel for text encoder\" in str(e):\n        repair_qwen3_encoder()\n    else:\n        raise","preventionTips":["Download Qwen3 encoder from a trusted source and verify file sizes/hashes.","Do not substitute non-HF encoder implementations in the Z-Image graph.","Update transformers alongside InvokeAI releases."],"tags":["type-check","qwen3","text-encoder","z-image"],"backgroundTag":"unexpected-model-type","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}