{"record":{"id":"dc7be5d8770abfbb","repo":"invoke-ai/InvokeAI","slug":"expected-at-least-1-hidden-state-got-len-outputs","errorCode":null,"errorMessage":"Expected at least 1 hidden state, got {len(outputs.hidden_states)}.","messagePattern":"Expected at least 1 hidden state, got (.+?)\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"invokeai/app/invocations/anima_text_encoder.py","lineNumber":191,"sourceCode":"\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\n        return qwen3_embeds, t5xxl_ids, None\n","sourceCodeStart":173,"sourceCodeEnd":209,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/invocations/anima_text_encoder.py#L173-L209","documentation":"After confirming hidden_states exists, the code validates it is non-empty before indexing hidden_states[-1]. An empty tuple would produce an IndexError, so it raises a RuntimeError reporting how many hidden states were returned instead.","triggerScenarios":"During invoke → _encode_prompt, when outputs.hidden_states is an empty tuple/sequence — essentially only possible with a broken or stubbed model whose forward produces no layer outputs despite output_hidden_states=True.","commonSituations":"Custom or partially-loaded model implementations; exotic quantization/wrapper paths that skip emitting layer hidden states; mock models in testing returning an empty tuple.","solutions":["Use the standard, fully-initialized Qwen3 PreTrainedModel so its layers emit hidden states.","Verify the model weights loaded completely (no empty/pruned layer set).","If wrapping the encoder, forward output_hidden_states through to the underlying model's forward call."],"exampleFix":"// before\nqwen3_embeds = outputs.hidden_states[-1][0]  # IndexError if empty\n// after\nif len(outputs.hidden_states) == 0:\n    raise RuntimeError(\"no hidden states\")\nqwen3_embeds = outputs.hidden_states[-1][0]","handlingStrategy":"type-guard","validationCode":"out = enc(**inputs, output_hidden_states=True)\nassert out.hidden_states is not None and len(out.hidden_states) >= 1","typeGuard":"def has_hidden_states(output) -> bool:\n    hs = getattr(output, \"hidden_states\", None)\n    return hs is not None and len(hs) >= 1","tryCatchPattern":"try:\n    result = invocation.invoke(context)\nexcept RuntimeError as e:\n    if \"Expected at least 1 hidden state\" in str(e):\n        reload_fully_initialized_model()\n    else:\n        raise","preventionTips":["Ensure the model loads all layer weights (no empty/pruned loads).","Pass output_hidden_states=True through any wrapper to the underlying forward.","Sanity-check hidden_states length equals num_layers+1 after model changes."],"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"}