{"record":{"id":"35b4da8ff8abe9a5","repo":"invoke-ai/InvokeAI","slug":"text-encoder-did-not-return-hidden-states","errorCode":null,"errorMessage":"Text encoder did not return hidden_states.","messagePattern":"Text encoder did not return hidden_states\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"invokeai/app/invocations/anima_text_encoder.py","lineNumber":189,"sourceCode":"                    \"Consider shortening the prompt for best results.\"\n                )\n\n            # Ensure at least 1 token (empty prompts produce 0 tokens with padding=False)\n            if text_input_ids.shape[-1] == 0:\n                pad_id = tokenizer.pad_token_id if tokenizer.pad_token_id is not None else tokenizer.eos_token_id\n                text_input_ids = torch.tensor([[pad_id]])\n                attention_mask = torch.tensor([[1]])\n\n            # Get last hidden state from Qwen3 (final layer output)\n            prompt_mask = attention_mask.to(device).bool()\n            outputs = text_encoder(\n                text_input_ids.to(device),\n                attention_mask=prompt_mask,\n                output_hidden_states=True,\n            )\n\n            if not hasattr(outputs, \"hidden_states\") or outputs.hidden_states is None:\n                raise RuntimeError(\"Text encoder did not return hidden_states.\")\n            if len(outputs.hidden_states) < 1:\n                raise RuntimeError(f\"Expected at least 1 hidden state, got {len(outputs.hidden_states)}.\")\n\n            # Use last hidden state — only real tokens, no padding\n            qwen3_embeds = outputs.hidden_states[-1][0]  # Shape: (seq_len, 1024)\n\n        # --- Step 2: Tokenize with bundled T5-XXL tokenizer (IDs only, no model) ---\n        context.util.signal_progress(\"Tokenizing with T5-XXL\")\n        t5_tokenizer = load_bundled_t5_tokenizer()\n        t5_tokens = t5_tokenizer(\n            prompt,\n            padding=False,\n            truncation=True,\n            max_length=T5_MAX_SEQ_LEN,\n            return_tensors=\"pt\",\n        )\n        t5xxl_ids = t5_tokens.input_ids[0]  # Shape: (seq_len,)\n","sourceCodeStart":171,"sourceCodeEnd":207,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/invocations/anima_text_encoder.py#L171-L207","documentation":"The forward pass requests output_hidden_states=True, so the model output must carry a hidden_states tuple. If the output object lacks the attribute or it is None, _encode_prompt raises a RuntimeError, because it builds the prompt embeddings from the last hidden state.","triggerScenarios":"During invoke → _encode_prompt, when calling text_encoder(input_ids, attention_mask, output_hidden_states=True) returns an output object without hidden_states — e.g. the loaded model is not a real PreTrainedModel forward, or its config disables output hidden states / returns a plain tensor.","commonSituations":"A model subclass whose forward returns a tensor instead of a ModelOutput; incompatible transformers version changing the output type; a wrapper (e.g. for quantization/ONNX) that drops the hidden_states field.","solutions":["Load the official Qwen3 base model class via AutoModel so forward returns a ModelOutput with hidden_states.","Check that output_hidden_states=True is passed (as the code does) and that no wrapper strips it.","Align the transformers library version with InvokeAI's pinned requirements and reconvert the model."],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":"from transformers import AutoModel\nenc = AutoModel.from_pretrained(model_path)\nout = enc(**inputs, output_hidden_states=True)\nassert getattr(out, \"hidden_states\", None) is not None","typeGuard":"def returns_hidden_states(output) -> bool:\n    return hasattr(output, \"hidden_states\") and output.hidden_states is not None","tryCatchPattern":"try:\n    result = invocation.invoke(context)\nexcept RuntimeError as e:\n    if \"did not return hidden_states\" in str(e):\n        reload_with_stock_pretrained_model()\n    else:\n        raise","preventionTips":["Load the encoder with its stock transformers class so forward returns a ModelOutput.","Avoid wrappers (quantization/ONNX bridges) that drop output fields.","Test model forward output shape/fields after any conversion."],"tags":["text-encoder","hidden-states","transformers","invokeai"],"backgroundTag":"missing-model-output-field","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}