{"record":{"id":"4e2c972c2d982105","repo":"invoke-ai/InvokeAI","slug":"mistral-encoder-did-not-return-hidden-states-ensu","errorCode":null,"errorMessage":"Mistral encoder did not return hidden_states. Ensure output_hidden_states=True is supported by this model.","messagePattern":"Mistral encoder did not return hidden_states\\. Ensure output_hidden_states=True is supported by this model\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"invokeai/app/invocations/flux2_dev_text_encoder.py","lineNumber":213,"sourceCode":"            max_length=self.max_seq_len,\n        )\n        input_ids = inputs[\"input_ids\"].to(device)\n        attention_mask = inputs[\"attention_mask\"].to(device)\n\n        # Mistral3ForConditionalGeneration wraps the LM under `.language_model`.\n        # For pure text encoding, run that sub-module to skip the (unused) vision\n        # tower and to avoid emitting a generation; for plain MistralModel /\n        # MistralForCausalLM, run the model directly.\n        forward_target = getattr(text_encoder, \"language_model\", None) or text_encoder\n\n        outputs = forward_target(\n            input_ids=input_ids,\n            attention_mask=attention_mask,\n            output_hidden_states=True,\n            use_cache=False,\n        )\n        if not hasattr(outputs, \"hidden_states\") or outputs.hidden_states is None:\n            raise RuntimeError(\n                \"Mistral encoder did not return hidden_states. \"\n                \"Ensure output_hidden_states=True is supported by this model.\"\n            )\n        num_hidden_states = len(outputs.hidden_states)  # = num_hidden_layers + 1 (embedding output)\n        num_layers = num_hidden_states - 1\n\n        # The standalone Mistral encoder loaders only accept 30-layer cow or 40-layer\n        # Mistral Small 3 weights, so hidden_states[] should always contain the layers\n        # FLUX.2 [dev]'s joint attention was trained to read (10/20/30). A text encoder\n        # extracted from a Main_Diffusers_Flux2 pipeline, however, is loaded via generic\n        # from_pretrained with no layer-count validation — so a nonstandard pipeline with\n        # a <30-layer encoder could reach here. Fail loudly instead of inventing extraction\n        # indices that would silently produce off-distribution (degraded) embeddings.\n        if num_layers < max(DEV_EXTRACTION_LAYERS):\n            raise RuntimeError(\n                f\"Mistral encoder returned only {num_layers} hidden layer(s), but FLUX.2 [dev] reads \"\n                f\"layers {DEV_EXTRACTION_LAYERS} and requires at least {max(DEV_EXTRACTION_LAYERS)}. \"\n                \"This is not a supported FLUX.2 [dev] text encoder.\"","sourceCodeStart":195,"sourceCodeEnd":231,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/invocations/flux2_dev_text_encoder.py#L195-L231","documentation":"_encode_prompt throws this RuntimeError when the Mistral encoder's forward pass returns no hidden_states even though it was called with output_hidden_states=True. FLUX.2 [dev] conditioning requires per-layer hidden states for extraction, so a model that cannot supply them is unusable. This typically means the model class does not support the output_hidden_states argument.","triggerScenarios":"The encoder forward call (input_ids, attention_mask, output_hidden_states=True, use_cache=False) completes, but the returned outputs object has no hidden_states attribute or it is None.","commonSituations":"A custom, quantized, or nonstandard Mistral-compatible model class ignores output_hidden_states; an old transformers version returns an object without the field; or a wrapper model silently drops the option.","solutions":["Use the standard Mistral Small 3.1 text encoder model/class that supports output_hidden_states.","Upgrade the transformers library to a version where MistralForCausalLM supports output_hidden_states correctly.","Ensure no custom wrapper or patch of the encoder strips the hidden_states output."],"exampleFix":"// before\ncustom_encoder = load_custom_quantized_encoder()  // ignores output_hidden_states\n// after\nencoder = MistralForCausalLM.from_pretrained(mistral_3_1_path)","handlingStrategy":"validation","validationCode":"outputs = encoder(input_ids=ids, attention_mask=mask, output_hidden_states=True, use_cache=False)\nif not hasattr(outputs, \"hidden_states\") or outputs.hidden_states is None:\n    raise RuntimeError(\"Encoder does not support output_hidden_states\")","typeGuard":"def returns_hidden_states(encoder) -> bool:\n    out = encoder(input_ids=torch.tensor([[0]]), output_hidden_states=True, use_cache=False)\n    return getattr(out, \"hidden_states\", None) is not None","tryCatchPattern":"try:\n    output = text_encoder_invocation.invoke(context)\nexcept RuntimeError as e:\n    if \"did not return hidden_states\" in str(e):\n        encoder = load_standard_mistral_encoder()\n        output = retry_encoding(encoder)\n    else:\n        raise","preventionTips":["Use standard MistralForCausalLM loading rather than custom quantized wrappers.","Smoke-test the encoder once with output_hidden_states=True before wiring it into workflows.","Pin a transformers version known to support Mistral hidden-state outputs."],"tags":["invokeai","runtime-error","text-encoder","transformers","flux2"],"backgroundTag":"missing-hidden-states-output","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}