{"record":{"id":"39fc11581f69e5a5","repo":"invoke-ai/InvokeAI","slug":"expected-at-least-2-hidden-states-from-text-encode","errorCode":null,"errorMessage":"Expected at least 2 hidden states from text encoder, got {len(outputs.hidden_states)}. This may indicate an incompatible model or configuration.","messagePattern":"Expected at least 2 hidden states from text encoder, got (.+?)\\. This may indicate an incompatible model or configuration\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"invokeai/app/invocations/z_image_text_encoder.py","lineNumber":181,"sourceCode":"                )\n\n            # Get hidden states from the text encoder\n            # Use the second-to-last hidden state like diffusers does\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            # Validate hidden_states output\n            if not hasattr(outputs, \"hidden_states\") or outputs.hidden_states is None:\n                raise RuntimeError(\n                    \"Text encoder did not return hidden_states. \"\n                    \"Ensure output_hidden_states=True is supported by this model.\"\n                )\n            if len(outputs.hidden_states) < 2:\n                raise RuntimeError(\n                    f\"Expected at least 2 hidden states from text encoder, got {len(outputs.hidden_states)}. \"\n                    \"This may indicate an incompatible model or configuration.\"\n                )\n            prompt_embeds = outputs.hidden_states[-2]\n\n            # Z-Image expects a 2D tensor [seq_len, hidden_dim] with only valid tokens\n            # Based on diffusers ZImagePipeline implementation:\n            # embeddings_list.append(prompt_embeds[i][prompt_masks[i]])\n            # Since batch_size=1, we take the first item and filter by mask\n            prompt_embeds = prompt_embeds[0][prompt_mask[0]]\n\n        if not isinstance(prompt_embeds, torch.Tensor):\n            raise TypeError(\n                f\"Expected torch.Tensor for prompt embeddings, got {type(prompt_embeds).__name__}. \"\n                \"Text encoder returned unexpected type.\"\n            )\n        return prompt_embeds\n","sourceCodeStart":163,"sourceCodeEnd":199,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/invocations/z_image_text_encoder.py#L163-L199","documentation":"After confirming hidden_states exists, _encode_prompt requires at least 2 entries because it takes hidden_states[-2] (the second-to-last layer) as prompt_embeds. Fewer than 2 indicates the encoder ran with an incompatible configuration or model, and a RuntimeError reports the actual count.","triggerScenarios":"Z-Image text encoding where outputs.hidden_states has length < 2 (e.g. length 1: only embedding output, or 0) despite output_hidden_states=True.","commonSituations":"A truncated/tiny Qwen3 model (0 hidden layers) or misconfigured num_hidden_layers=0; incompatible checkpoint converted incorrectly; model wrapper stripping hidden layers; wrong submodel selected.","solutions":["Use the full Qwen3 encoder checkpoint intended for Z-Image (with multiple hidden layers).","Verify the model config's num_hidden_layers > 0 and re-download config.json if suspect.","Re-import the Z-Image model so the correct text encoder submodel is bound.","Update transformers/InvokeAI so all layer hidden states are collected."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"outputs = encoder(**inputs, output_hidden_states=True)\nif len(outputs.hidden_states) < 2:\n    fail_fast(f\"need >=2 hidden states, got {len(outputs.hidden_states)}\")","typeGuard":null,"tryCatchPattern":"try:\n    encode(context)\nexcept RuntimeError as e:\n    if \"Expected at least 2 hidden states\" in str(e):\n        reload_full_qwen3_checkpoint()\n    else:\n        raise","preventionTips":["Verify num_hidden_layers > 0 in the Qwen3 config.json before use.","Use the official Z-Image Qwen3 encoder checkpoint, not a pruned variant.","Re-check submodel bindings after converting or quantizing models."],"tags":["hidden-states","qwen3","model-config","runtime-error"],"backgroundTag":"missing-model-output","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}