{"record":{"id":"80b2c9f229c4e5cb","repo":"invoke-ai/InvokeAI","slug":"text-encoder-did-not-return-hidden-states-ensure-80b2c9","errorCode":null,"errorMessage":"Text encoder did not return hidden_states. Ensure output_hidden_states=True is supported by this model.","messagePattern":"Text 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/z_image_text_encoder.py","lineNumber":176,"sourceCode":"            ):\n                removed_text = tokenizer.batch_decode(untruncated_ids[:, max_seq_len - 1 : -1])\n                context.logger.warning(\n                    f\"The following part of your input was truncated because `max_sequence_length` is set to \"\n                    f\"{max_seq_len} tokens: {removed_text}\"\n                )\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(","sourceCodeStart":158,"sourceCodeEnd":194,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/invocations/z_image_text_encoder.py#L158-L194","documentation":"The Qwen3 encoder forward is called with output_hidden_states=True; Z-Image needs intermediate hidden states, so _encode_prompt raises a RuntimeError if outputs.hidden_states is missing or None. This happens when the model class does not support the output_hidden_states flag or returns a non-standard output object.","triggerScenarios":"Calling the Z-Image text encoder where encoder(input_ids=..., attention_mask=..., output_hidden_states=True) returns an object with no hidden_states attribute or hidden_states=None.","commonSituations":"A substitute/incompatible text encoder model class that ignores output_hidden_states; wrapped or quantized model whose forward returns a custom output type; wrong model class loaded for the Qwen3 submodel.","solutions":["Load the standard transformers Qwen3 model class which supports output_hidden_states=True.","Check the model's config (e.g. output_hidden_states not disabled) and that its forward supports the kwarg.","Replace quantized/wrapper implementations that drop hidden_states with a standard loading path.","Update transformers to a version where Qwen3 returns ModelOutput with hidden_states."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"outputs = encoder(**inputs, output_hidden_states=True)\nif not hasattr(outputs, \"hidden_states\") or outputs.hidden_states is None:\n    fail_fast(\"encoder does not emit hidden_states\")","typeGuard":null,"tryCatchPattern":"try:\n    encode(context)\nexcept RuntimeError as e:\n    if \"did not return hidden_states\" in str(e):\n        load_standard_qwen3_class()\n    else:\n        raise","preventionTips":["Only use Qwen3 model classes that support output_hidden_states=True.","Avoid wrapper/quantization layers that replace the model output object.","Smoke-test the encoder forward once at model install time."],"tags":["hidden-states","qwen3","text-encoder","runtime-error"],"backgroundTag":"missing-model-output","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}