Fixed: "RuntimeError: The size of tensor a (802) must match the size of tensor b (512) at non-singleton dimension 1"
A Hugging Face sentiment-analysis pipeline crashed on a long review with RuntimeError: The size of tensor a (802) must match the size of tensor b (512) at non-singleton dimension 1. The text was 802 tokens long, and the model reads at most 512. Passing truncation=True when calling the pipeline cuts the text to the model's limit, and the pipeline classifies it.
What I Was Doing
I was classifying a long movie review with a sentiment-analysis pipeline, in Transformers 5.26.0 on Python 3.14, on an Apple M1 Pro. To make the review long, the script repeats one sentence 50 times:
from transformers import pipeline
classifier = pipeline(
"sentiment-analysis",
model="distilbert/distilbert-base-uncased-finetuned-sst-2-english",
revision="714eb0fa89d2f80546fda750413ed43d93601a13",
)
review = "The film was slow at first, but the ending made it worth the wait. " * 50
print(classifier(review))
uv run python review.py
What I Expected
One label with its score, as the pipeline returns for a short text:
[{'label': 'POSITIVE', 'score': ...}]
What I Got
A warning, then a long traceback. Its first lines:
Loading weights: 100%|█████████████████████████████████████████| 104/104 [00:00<00:00, 10139.41it/s]
[transformers] Token indices sequence length is longer than the specified maximum sequence length for this model (802 > 512). Running this sequence through the model will result in indexing errors
Traceback (most recent call last):
And its last lines:
File "/Users/you/source/long-text/.venv/lib/python3.14/site-packages/transformers/models/distilbert/modeling_distilbert.py", line 114, in forward
embeddings = inputs_embeds + position_embeddings # (bs, max_seq_length, dim)
~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
RuntimeError: The size of tensor a (802) must match the size of tensor b (512) at non-singleton dimension 1

The warning came first: the text is 802 tokens long, and the model's maximum is 512. The error came from inside the model, on the line that adds the position embeddings to the tokens.
What I Checked
Is it the Mac's GPU? Without a device, the pipeline ran on the Apple GPU (MPS). I ran the same script on the processor, with device="cpu" added to pipeline(...):
RuntimeError: The size of tensor a (802) must match the size of tensor b (512) at non-singleton dimension 1
The same error, so the GPU isn't the cause.
What's 512? The model's config.json on the Hub has "max_position_embeddings": 512, and its tokenizer_config.json has "model_max_length": 512. The model has learned one position embedding for each place in a text, from the first token to the 512th, and nothing beyond. The last line of the traceback adds the 802 token vectors to the 512 position vectors, and the sizes don't match.
Why didn't the pipeline cut the text? The tokenizer reference describes its truncation argument as off by default:
Falseor'do_not_truncate'(default): No truncation (i.e., can output batch with sequence lengths greater than the model maximum admissible input size).
and truncation=True as:
Truncate to a maximum length specified with the argument
max_lengthor to the maximum acceptable input length for the model if that argument is not provided.
The text-classification pipeline passes the keyword arguments it doesn't use itself to its tokenizer. In transformers/pipelines/text_classification.py:
def _sanitize_parameters(self, function_to_apply=None, top_k="", **tokenizer_kwargs):
# ...
preprocess_params = tokenizer_kwargs
So truncation=True in the call reaches the tokenizer.
Why It Happens
Models like BERT and DistilBERT read a fixed maximum number of tokens, 512 for this one. The tokenizer doesn't cut longer texts unless it's asked to, so the pipeline sends all 802 tokens to the model. The tokenizer only warns, "Running this sequence through the model will result in indexing errors", and the model fails, because it has no position embedding for the 513th token or any after it.
The Fix
Pass truncation=True when you call the pipeline:
from transformers import pipeline
classifier = pipeline(
"sentiment-analysis",
model="distilbert/distilbert-base-uncased-finetuned-sst-2-english",
revision="714eb0fa89d2f80546fda750413ed43d93601a13",
)
review = "The film was slow at first, but the ending made it worth the wait. " * 50
print(classifier(review, truncation=True))

The tokenizer now keeps the first 512 tokens and drops the rest. The 512 include the two special tokens it adds, [CLS] at the start and [SEP] at the end: tokenizing the review with truncation=True gave 512 tokens, from [CLS] to [SEP].
After the Fix
uv run python review.py
Loading weights: 100%|█████████████████████████████████████████| 104/104 [00:00<00:00, 23826.07it/s]
[{'label': 'POSITIVE', 'score': 0.9892080426216125}]

The review was classified as POSITIVE, and the warning is gone too. The commands work unchanged in PowerShell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Ignoring the "Token indices sequence length is longer" warning | The model fails a moment later with this RuntimeError | Treat the warning as the cause: the text is too long for the model |
| Expecting truncation to read the whole text | Everything after the first 512 tokens is ignored | When the end of a text matters, split it into parts that fit and classify each part |
| Counting words instead of tokens | The 700-word review was 802 tokens: each sentence's comma and full stop are tokens too, and rare words split into several | Trust the numbers in the warning, which counts tokens |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
The numbers in the error differ, such as (722) and (512) | Another text length; the first number is your text's tokens | The same fix |
| The error comes from a list of texts | One of the texts is too long: a short text and this review in one list gave the same error | truncation=True applies to every text in the list: the same list then gave POSITIVE for both |
FAQ
Can I choose a shorter limit?
Yes. max_length sets the number of tokens to keep: classifier(review, truncation=True, max_length=256) read the first 256 tokens and returned POSITIVE with a score of 0.9949. Without it, truncation=True uses the model's own maximum.
Does truncation change the result?
It can, because the model doesn't see the cut-off part. Here every repeat of the sentence says the same thing, so the first 512 tokens carry the whole meaning. In a real review that changes its mind at the end, the end would be lost.
Do newer models read longer texts?
Some do. A model's config.json says how many positions it has, in max_position_embeddings: 512 for this DistilBERT model, 8,192 for answerdotai/ModernBERT-base.
Conclusion
RuntimeError: The size of tensor a (802) must match the size of tensor b (512) at non-singleton dimension 1 meant the text was 802 tokens long and the model reads 512. The pipeline doesn't cut texts by default; classifier(review, truncation=True) kept the first 512 tokens and returned POSITIVE.
Additional Resources
- Tokenizer reference —
truncation,max_lengthandpadding. - Pipelines — the text-classification pipeline and its arguments.
- distilbert/distilbert-base-uncased-finetuned-sst-2-english — the model's card.
