{"record":{"id":"df7f28bb4ef63f57","repo":"invoke-ai/InvokeAI","slug":"expected-pretrainedmodel-for-text-encoder-got-ty-df7f28","errorCode":null,"errorMessage":"Expected PreTrainedModel for text encoder, got {type(text_encoder).__name__}. The Mistral encoder model may be corrupted or incompatible.","messagePattern":"Expected PreTrainedModel for text encoder, got (.+?)\\. The Mistral encoder model may be corrupted or incompatible\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"invokeai/app/invocations/flux2_dev_text_encoder.py","lineNumber":159,"sourceCode":"                f\"Recovered {repaired_tensors} required Mistral tensor(s) on {device} after a partial device mismatch.\"\n            )\n\n        # Apply any LoRAs attached 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=FLUX_LORA_T5_PREFIX,\n                dtype=lora_dtype,\n                cached_weights=cached_weights,\n            )\n        )\n\n        context.util.signal_progress(\"Running Mistral text encoder (FLUX.2 [dev])\")\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 Mistral encoder model may be corrupted or incompatible.\"\n            )\n\n        # Build the raw FLUX.2 [dev] prompt template — matches ComfyUI's\n        # `Flux2Tokenizer.llama_template.format(text)` byte-for-byte. `[SYSTEM_PROMPT]`,\n        # `[/SYSTEM_PROMPT]`, `[INST]`, `[/INST]` are Tekken special tokens, so any of\n        # the three processors we can land on (Pixtral/Mistral3 processor, plain HF\n        # LlamaTokenizerFast, our embedded-Tekken adapter) emit the same sequence.\n        text = FLUX2_DEV_PROMPT_TEMPLATE.format(system=FLUX2_DEV_SYSTEM_MESSAGE, prompt=self.prompt)\n\n        # Comfy pads on the LEFT (`pad_left=True`), keeping the meaningful tokens\n        # at the right edge of the sequence. HF processors expose this via the\n        # `padding_side` attribute on their underlying tokenizer; we set it\n        # explicitly so the call matches Comfy's behavior regardless of the\n        # tokenizer's default. `processor` is typed as the `AnyModel` union;\n        # narrow to `Any` for the duration of the tokenizer call.\n        proc = cast(Any, processor)","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/invocations/flux2_dev_text_encoder.py#L141-L177","documentation":"_encode_prompt throws this TypeError when the loaded Mistral text encoder object is not a transformers PreTrainedModel. This indicates the model loaded into memory is corrupted, incompatible, or the wrong type for FLUX.2 [dev] encoding. It is a defensive type check performed before building the prompt template and running the forward pass.","triggerScenarios":"invoke() -> _encode_prompt receives a text_encoder whose runtime type is not PreTrainedModel (e.g. a config object, a dict, or a differently-typed module returned by the model loader).","commonSituations":"The Mistral encoder model files are corrupted on disk, the wrong model was registered as the encoder, or an incompatible/patched transformers loading path returned a nonstandard object.","solutions":["Re-download or re-register the Mistral Small 3.1 text encoder model in the model manager to replace the corrupted/incompatible files.","Verify the 'Mistral Encoder' input points at an actual Mistral text encoder model, not another model type.","Update the transformers library version so Mistral models load as PreTrainedModel instances."],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":"encoder_info = context.models.load(mistral_encoder_ref)\nif not isinstance(encoder_info.model, PreTrainedModel):\n    raise TypeError(f\"Encoder is {type(encoder_info.model).__name__}, expected PreTrainedModel\")","typeGuard":"def is_valid_encoder(obj) -> bool:\n    return isinstance(obj, PreTrainedModel)","tryCatchPattern":"try:\n    output = text_encoder_invocation.invoke(context)\nexcept TypeError as e:\n    if \"Expected PreTrainedModel for text encoder\" in str(e):\n        reimport_mistral_encoder_model(context)\n        output = text_encoder_invocation.invoke(context)\n    else:\n        raise","preventionTips":["Verify checksums/integrity of downloaded Mistral encoder files.","Register only genuine Mistral Small 3.1 text encoder models in the model manager.","Keep the transformers library up to date."],"tags":["invokeai","type-mismatch","text-encoder","corrupted-model","flux2"],"backgroundTag":"unexpected-model-type","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}