High severity beginner · Fix: 2-5 min

ValueError

transformers.tokenization_utils_base.TruncationOverflowError

What this error means
The HuggingFace tokenizer throws an error when input text exceeds the model's maximum token length during embedding generation.

Stack trace

traceback
ValueError: Token indices sequence length is longer than the specified maximum sequence length for this model (1025 > 1024). Running this sequence through the model will result in indexing errors
QUICK FIX
Add truncation=True to the tokenizer call to automatically truncate inputs exceeding max length.

Why it happens

HuggingFace tokenizers enforce a maximum token length limit defined by the model architecture. When input text exceeds this limit without proper truncation, the tokenizer raises a ValueError to prevent invalid model inputs.

Detection

Monitor input text length before tokenization or catch ValueError exceptions during tokenization to detect when inputs exceed the model's max token length.

Causes & fixes

1

Input text length exceeds the model's maximum token length without truncation enabled

✓ Fix

Enable truncation in the tokenizer call by setting truncation=True or manually truncate input text before tokenization.

2

Using a tokenizer with default max_length set too low or not aligned with the model's max input size

✓ Fix

Explicitly set tokenizer's max_length parameter to the model's maximum supported length or use tokenizer.model_max_length.

3

Passing very long documents or concatenated texts without chunking or splitting

✓ Fix

Split or chunk long texts into smaller segments that fit within the tokenizer's max length before embedding.

Code: broken vs fixed

Broken - triggers the error
python
from transformers import AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
text = "a" * 2000
# This line raises ValueError due to input length
tokens = tokenizer(text)['input_ids']
Fixed - works correctly
python
import os
from transformers import AutoTokenizer

os.environ['HF_HOME'] = '/tmp/hf_cache'  # Example environment setup

tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
text = "a" * 2000
# Fix: enable truncation to avoid max length error
tokens = tokenizer(text, truncation=True)['input_ids']
print(f'Tokenized length: {len(tokens)}')
Enabled truncation=True in tokenizer call to automatically truncate inputs exceeding the model's max token length, preventing the ValueError.
⚠

Workaround

Catch the ValueError exception, then manually truncate the input text to the tokenizer's max_length before retrying tokenization.

✓

Prevention

Always use tokenizer truncation or chunk long texts before tokenization to ensure inputs never exceed the model's maximum token length.

Python 3.7+ · transformers >=4.0.0 · tested on 4.30.0
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.