invoke-ai/InvokeAI · error · TypeError

Expected torch.Tensor for input_ids, got {type(text_input_id

Error message

Expected torch.Tensor for input_ids, got {type(text_input_ids).__name__}. Tokenizer returned unexpected type.

What it means

After tokenizer(prompt_formatted, padding=..., return_tensors="pt"), _encode_prompt asserts text_inputs.input_ids is a torch.Tensor before using it. If the tokenizer returns an unexpected type (e.g. list instead of tensor), a TypeError naming the actual type is raised. This guards against non-standard tokenizer outputs breaking the encoder forward call.

Source

Thrown at invokeai/app/invocations/z_image_text_encoder.py:144

            except (AttributeError, TypeError) as e:
                # Fallback if tokenizer doesn't support apply_chat_template or enable_thinking
                context.logger.warning(f"Chat template failed ({e}), using raw prompt.")
                prompt_formatted = prompt

            # Tokenize the formatted prompt
            text_inputs = tokenizer(
                prompt_formatted,
                padding="max_length",
                max_length=max_seq_len,
                truncation=True,
                return_attention_mask=True,
                return_tensors="pt",
            )

            text_input_ids = text_inputs.input_ids
            attention_mask = text_inputs.attention_mask
            if not isinstance(text_input_ids, torch.Tensor):
                raise TypeError(
                    f"Expected torch.Tensor for input_ids, got {type(text_input_ids).__name__}. "
                    "Tokenizer returned unexpected type."
                )
            if not isinstance(attention_mask, torch.Tensor):
                raise TypeError(
                    f"Expected torch.Tensor for attention_mask, got {type(attention_mask).__name__}. "
                    "Tokenizer returned unexpected type."
                )

            # Check for truncation
            untruncated_ids = tokenizer(prompt_formatted, padding="longest", return_tensors="pt").input_ids
            if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(
                text_input_ids, untruncated_ids
            ):
                removed_text = tokenizer.batch_decode(untruncated_ids[:, max_seq_len - 1 : -1])
                context.logger.warning(
                    f"The following part of your input was truncated because `max_sequence_length` is set to "
                    f"{max_seq_len} tokens: {removed_text}"

View on GitHub (pinned to 0b6a024f2f)

Solutions

  1. Use the standard transformers tokenizer for Qwen3 (AutoTokenizer / PreTrainedTokenizerFast) instead of a custom subclass.
  2. Ensure return_tensors="pt" is honoured by your tokenizer version; upgrade transformers if needed.
  3. Confirm the loaded tokenizer is a PreTrainedTokenizerBase instance before calling it.
  4. Convert manually if needed: torch.tensor(tokenizer_output.input_ids).

Example fix

// before
inputs = custom_tokenizer(prompt, return_tensors="pt")
// after
from transformers import AutoTokenizer
tok = AutoTokenizer.from_pretrained(model_path)
inputs = tok(prompt, padding="longest", return_tensors="pt")
Defensive patterns

Strategy: type-guard

Validate before calling

inputs = tok(prompt, padding="longest", return_tensors="pt")
if not isinstance(inputs.input_ids, torch.Tensor):
    fail_fast(inputs.input_ids)

Type guard

def is_tensor(x) -> bool:
    import torch
    return isinstance(x, torch.Tensor)

Try / catch

try:
    encode(context)
except TypeError as e:
    if "Expected torch.Tensor for input_ids" in str(e):
        swap_to_stock_tokenizer()
    else:
        raise

Prevention

When it happens

Trigger: Calling the Qwen3 tokenizer with return_tensors="pt" and receiving input_ids that is not a torch.Tensor — typically from a custom/incompatible tokenizer implementation or mocked tokenizer.

Common situations: A tokenizer subclass overriding __call__ and returning Python lists; a transformers version where tensor conversion failed; passing a raw tokenizer config object instead of the loaded tokenizer.

Related errors


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