invoke-ai/InvokeAI · error · TypeError
Expected torch.Tensor for prompt embeddings, got {type(promp
Error message
Expected torch.Tensor for prompt embeddings, got {type(prompt_embeds).__name__}. Text encoder returned unexpected type. What it means
_encode_prompt in the Z-Image text encoder invocation expects the text encoder to return prompt embeddings as a torch.Tensor (after masking/batch-indexing). If the underlying encoder or pipeline path returns another type (list, tuple, nested tensors), this TypeError is thrown so downstream diffusion code never receives malformed embeddings. It is a defensive type check modeled on diffusers' ZImagePipeline expectations.
Source
Thrown at invokeai/app/invocations/z_image_text_encoder.py:194
raise RuntimeError(
"Text encoder did not return hidden_states. "
"Ensure output_hidden_states=True is supported by this model."
)
if len(outputs.hidden_states) < 2:
raise RuntimeError(
f"Expected at least 2 hidden states from text encoder, got {len(outputs.hidden_states)}. "
"This may indicate an incompatible model or configuration."
)
prompt_embeds = outputs.hidden_states[-2]
# Z-Image expects a 2D tensor [seq_len, hidden_dim] with only valid tokens
# Based on diffusers ZImagePipeline implementation:
# embeddings_list.append(prompt_embeds[i][prompt_masks[i]])
# Since batch_size=1, we take the first item and filter by mask
prompt_embeds = prompt_embeds[0][prompt_mask[0]]
if not isinstance(prompt_embeds, torch.Tensor):
raise TypeError(
f"Expected torch.Tensor for prompt embeddings, got {type(prompt_embeds).__name__}. "
"Text encoder returned unexpected type."
)
return prompt_embeds
def _lora_iterator(self, context: InvocationContext) -> Iterator[PatchSpec]:
"""Iterate over LoRA models to apply to the Qwen3 text encoder."""
for lora in self.qwen3_encoder.loras:
lora_info = context.models.load(lora.lora)
if not isinstance(lora_info.model, ModelPatchRaw):
raise TypeError(
f"Expected ModelPatchRaw for LoRA '{lora.lora.key}', got {type(lora_info.model).__name__}. "
"The LoRA model may be corrupted or incompatible."
)
yield (lora_info.model, lora.weight, lora_info.model_in_ram())
View on GitHub (pinned to 0b6a024f2f)
Solutions
- Inspect the actual return type of the text encoder (print(type(prompt_embeds))) at the call site and convert with torch.stack()/torch.cat() before calling _encode_prompt
- Verify the diffusers ZImagePipeline version matches what InvokeAI expects; upgrade/downgrade diffusers so encode_prompt returns a Tensor
- Ensure the mask-slicing path (prompt_embeds[0][prompt_mask[0]]) is applied to a Tensor, not to a list of per-item outputs
- If using a custom encoder, wrap it so it returns a single torch.Tensor for the embeddings
Example fix
// before prompt_embeds = text_encoder(input_ids) # returns list of tensors // after prompt_embeds = torch.stack(text_encoder(input_ids)) if isinstance(text_encoder_out, list) else text_encoder_out
Defensive patterns
Strategy: type-guard
Validate before calling
import torch
assert isinstance(prompt_embeds, torch.Tensor), f"got {type(prompt_embeds).__name__}" Type guard
def is_tensor(x) -> bool:
import torch
return isinstance(x, torch.Tensor) Try / catch
try:
embeds = invocation.invoke(context)
except TypeError as e:
if 'prompt embeddings' in str(e):
prompt_embeds = torch.as_tensor(raw_output)
else:
raise Prevention
- Keep diffusers at the version InvokeAI pins
- Always unpack tuple outputs from encoders before passing them along
- Add a unit test asserting the encoder returns a Tensor
When it happens
Trigger: Calling invoke() on the Z-Image text encoder when the text encoder output is not a torch.Tensor, e.g. an encoder wrapper returning a list of tensors, a tuple like (embeddings, attn_mask) that was not unpacked, or a mocked/stubbed encoder in tests returning a list.
Common situations: Using an incompatible or custom text-encoder implementation with the Z-Image pipeline; a diffusers version change altering the return type of encode_prompt; passing raw tokenizer output instead of encoded embeddings; batch/mask slicing producing a list when batch_size != 1 assumptions break.
Related errors
- Unsupported control_lllite type: {type(control_lllite)}
- Expected PreTrainedModel for Gemma encoder, got {type(gemma_
- Expected ModelPatchRaw for LoRA '{lora.lora.key}', got {type
- Model config discriminator value must be computed from a dic
- missing config.json at {config_path}
AI-assisted analysis of invoke-ai/InvokeAI@0b6a024f2f (2026-08-29).
Data as JSON: /api/errors/6d779067cc7b1014.
Report an issue: GitHub.