{"record":{"id":"21ad0ad57a69a18e","repo":"invoke-ai/InvokeAI","slug":"expected-pretrainedmodel-for-text-encoder-got-ty","errorCode":null,"errorMessage":"Expected PreTrainedModel for text encoder, got {type(text_encoder).__name__}.","messagePattern":"Expected PreTrainedModel for text encoder, got (.+?)\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"invokeai/app/invocations/anima_text_encoder.py","lineNumber":146,"sourceCode":"\n            # Use the encoder's intended compute device, not its current parameter residency: partial loading may\n            # have temporarily offloaded all weights to RAM, which would wrongly run the whole encode on the CPU (see\n            # #9373). Qwen3 is fully autocast-capable, so nothing pins it to the compute device otherwise.\n            device = text_encoder_info.compute_device\n\n            # Apply LoRA models to the text encoder\n            lora_dtype = TorchDevice.choose_anima_inference_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=ANIMA_LORA_QWEN3_PREFIX,\n                    dtype=lora_dtype,\n                )\n            )\n\n            if not isinstance(text_encoder, PreTrainedModel):\n                raise TypeError(f\"Expected PreTrainedModel for text encoder, got {type(text_encoder).__name__}.\")\n            if not isinstance(tokenizer, PreTrainedTokenizerBase):\n                raise TypeError(f\"Expected PreTrainedTokenizerBase for tokenizer, got {type(tokenizer).__name__}.\")\n\n            context.util.signal_progress(\"Running Qwen3 0.6B text encoder\")\n\n            # Anima uses base Qwen3 (not instruct) — tokenize directly, no chat template.\n            # A safety cap is applied to prevent GPU OOM on extremely long prompts.\n            text_inputs = tokenizer(\n                prompt,\n                padding=False,\n                truncation=True,\n                max_length=QWEN3_MAX_SEQ_LEN,\n                return_attention_mask=True,\n                return_tensors=\"pt\",\n            )\n\n            text_input_ids = text_inputs.input_ids\n            attention_mask = text_inputs.attention_mask","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/invocations/anima_text_encoder.py#L128-L164","documentation":"_encode_prompt type-checks the loaded text encoder object, requiring it to be a transformers PreTrainedModel. The model manager returned some other object type (wrong model format, wrong model class, or a placeholder), so it raises a TypeError naming the actual type received.","triggerScenarios":"During invoke → _encode_prompt, after loading the Qwen3 text encoder via context.models, when isinstance(text_encoder, PreTrainedModel) fails — e.g. the model record points to a non-PreTrainedModel artifact or the wrong model type was loaded for the key.","commonSituations":"Corrupted or misconfigured model conversion; loading a model saved in a custom format; a model-manager record whose config/type does not match the actual on-disk weights; version drift between transformers and the stored model format.","solutions":["Re-convert/re-import the Qwen3 text encoder model so it is stored as a standard HF PreTrainedModel.","Verify the model manager record's type matches a Qwen3 text-encoder model, not another model class.","Update the transformers library to a version compatible with the stored model format and reload."],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":"from transformers import PreTrainedModel\ninfo = context.models.load(text_encoder_key)\nif not isinstance(info.model, PreTrainedModel):\n    print(f\"Text encoder is {type(info.model).__name__}, re-import required\")","typeGuard":"from transformers import PreTrainedModel\ndef is_valid_text_encoder(obj) -> bool:\n    return isinstance(obj, PreTrainedModel)","tryCatchPattern":"try:\n    result = invocation.invoke(context)\nexcept TypeError as e:\n    if \"Expected PreTrainedModel for text encoder\" in str(e):\n        reimport_text_encoder_model()\n    else:\n        raise","preventionTips":["Import text encoders through InvokeAI's model importer so the record type matches the artifact.","Keep transformers library version aligned with InvokeAI's requirements.","Re-convert models rather than hand-editing model-manager records."],"tags":["type-mismatch","text-encoder","qwen3","invokeai"],"backgroundTag":"unexpected-model-type","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}