{"record":{"id":"d5132da6ad0a685b","repo":"invoke-ai/InvokeAI","slug":"text-encoder-did-not-return-hidden-states-ensure","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/flux2_klein_text_encoder.py","lineNumber":174,"sourceCode":"            text,\n            return_tensors=\"pt\",\n            padding=\"max_length\",\n            truncation=True,\n            max_length=self.max_seq_len,\n        )\n\n        input_ids = inputs[\"input_ids\"].to(device)\n        attention_mask = inputs[\"attention_mask\"].to(device)\n\n        # Forward pass through the model\n        outputs = text_encoder(\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                \"Text encoder did not return hidden_states. \"\n                \"Ensure output_hidden_states=True is supported by this model.\"\n            )\n        num_hidden_layers = len(outputs.hidden_states)\n\n        hidden_states_list = []\n        for layer_idx in KLEIN_EXTRACTION_LAYERS:\n            if layer_idx >= num_hidden_layers:\n                layer_idx = num_hidden_layers - 1\n            hidden_states_list.append(outputs.hidden_states[layer_idx])\n\n        out = torch.stack(hidden_states_list, dim=1)\n        out = out.to(dtype=text_encoder.dtype, device=device)\n\n        batch_size, num_channels, seq_len, hidden_dim = out.shape\n        prompt_embeds = out.permute(0, 2, 1, 3).reshape(batch_size, seq_len, num_channels * hidden_dim)\n\n        last_hidden_state = outputs.hidden_states[-1]","sourceCodeStart":156,"sourceCodeEnd":192,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/invocations/flux2_klein_text_encoder.py#L156-L192","documentation":"The Qwen3 encoder forward pass was called with output_hidden_states=True, but the returned output object has no hidden_states attribute (or it is None). InvokeAI needs per-layer hidden states to build the FLUX.2 Klein conditioning tensor, so a model that does not support this option cannot be used.","triggerScenarios":"outputs = text_encoder(..., output_hidden_states=True) returns an object without hidden_states in _encode_prompt; the loaded model is not a real Qwen3 encoder or is a custom/older architecture that ignores output_hidden_states; a monkey-patched or quantized wrapper strips hidden states.","commonSituations":"Using a substitute/converted encoder model that returns only last_hidden_state; incompatible transformers version where config.output_hidden_states handling differs; heavily quantized (GGUF/4-bit) wrappers that drop auxiliary outputs.","solutions":["Verify the loaded model is the official Qwen3 encoder (see errors 350/351 checks)","Update transformers to a version where Qwen3 supports output_hidden_states=True","Remove or replace quantization/patch wrappers that drop hidden_states outputs","Confirm config.json does not set output_hidden_states=False in a way that overrides the forward kwarg"],"exampleFix":"// before\noutputs = text_encoder(input_ids=ids, attention_mask=mask, use_cache=False)\n// after\noutputs = text_encoder(input_ids=ids, attention_mask=mask, output_hidden_states=True, use_cache=False)","handlingStrategy":"validation","validationCode":"cfg = AutoConfig.from_pretrained(qwen3_encoder_path)\nif cfg.model_type != 'qwen3':\n    raise ValueError('Not a Qwen3 encoder')\nout = AutoModel.from_pretrained(qwen3_encoder_path)(\n    input_ids=torch.zeros((1, 4), dtype=torch.long), output_hidden_states=True)\nassert getattr(out, 'hidden_states', None) is not None","typeGuard":"def supports_hidden_states(outputs) -> bool:\n    return getattr(outputs, 'hidden_states', None) is not None","tryCatchPattern":"try:\n    result = klein_encoder.invoke(context)\nexcept RuntimeError as e:\n    if 'did not return hidden_states' in str(e):\n        replace_with_official_qwen3_encoder()\n    raise","preventionTips":["Use the official Qwen3 encoder weights, not converted substitutes","Avoid quantization wrappers that strip hidden_states","Smoke-test output_hidden_states=True support before wiring the model in"],"tags":["transformers","model-output","text-encoder"],"backgroundTag":"missing-model-output","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}