invoke-ai/InvokeAI · error · RuntimeError

Text encoder did not return hidden_states. Ensure output_hid

Error message

Text encoder did not return hidden_states. Ensure output_hidden_states=True is supported by this model.

What it means

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.

Source

Thrown at invokeai/app/invocations/flux2_klein_text_encoder.py:174

            text,
            return_tensors="pt",
            padding="max_length",
            truncation=True,
            max_length=self.max_seq_len,
        )

        input_ids = inputs["input_ids"].to(device)
        attention_mask = inputs["attention_mask"].to(device)

        # Forward pass through the model
        outputs = text_encoder(
            input_ids=input_ids,
            attention_mask=attention_mask,
            output_hidden_states=True,
            use_cache=False,
        )
        if not hasattr(outputs, "hidden_states") or outputs.hidden_states is None:
            raise RuntimeError(
                "Text encoder did not return hidden_states. "
                "Ensure output_hidden_states=True is supported by this model."
            )
        num_hidden_layers = len(outputs.hidden_states)

        hidden_states_list = []
        for layer_idx in KLEIN_EXTRACTION_LAYERS:
            if layer_idx >= num_hidden_layers:
                layer_idx = num_hidden_layers - 1
            hidden_states_list.append(outputs.hidden_states[layer_idx])

        out = torch.stack(hidden_states_list, dim=1)
        out = out.to(dtype=text_encoder.dtype, device=device)

        batch_size, num_channels, seq_len, hidden_dim = out.shape
        prompt_embeds = out.permute(0, 2, 1, 3).reshape(batch_size, seq_len, num_channels * hidden_dim)

        last_hidden_state = outputs.hidden_states[-1]

View on GitHub (pinned to 0b6a024f2f)

Solutions

  1. Verify the loaded model is the official Qwen3 encoder (see errors 350/351 checks)
  2. Update transformers to a version where Qwen3 supports output_hidden_states=True
  3. Remove or replace quantization/patch wrappers that drop hidden_states outputs
  4. Confirm config.json does not set output_hidden_states=False in a way that overrides the forward kwarg

Example fix

// before
outputs = text_encoder(input_ids=ids, attention_mask=mask, use_cache=False)
// after
outputs = text_encoder(input_ids=ids, attention_mask=mask, output_hidden_states=True, use_cache=False)
Defensive patterns

Strategy: validation

Validate before calling

cfg = AutoConfig.from_pretrained(qwen3_encoder_path)
if cfg.model_type != 'qwen3':
    raise ValueError('Not a Qwen3 encoder')
out = AutoModel.from_pretrained(qwen3_encoder_path)(
    input_ids=torch.zeros((1, 4), dtype=torch.long), output_hidden_states=True)
assert getattr(out, 'hidden_states', None) is not None

Type guard

def supports_hidden_states(outputs) -> bool:
    return getattr(outputs, 'hidden_states', None) is not None

Try / catch

try:
    result = klein_encoder.invoke(context)
except RuntimeError as e:
    if 'did not return hidden_states' in str(e):
        replace_with_official_qwen3_encoder()
    raise

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of invoke-ai/InvokeAI@0b6a024f2f (2026-08-29). Data as JSON: /api/errors/d5132da6ad0a685b. Report an issue: GitHub.