Skip to content

BERTScore: OverflowError with transformers>=5 due to undefined model_max_length #739

Open

Description

@uriyapesso

When using evaluate.load("bertscore") with transformers>=5.0 and models that don't define model_max_length (e.g., microsoft/deberta-xlarge-mnli), the computation fails with:

OverflowError: int too big to convert
The error occurs at transformers/tokenization_utils_tokenizers.py in self._tokenizer.enable_truncation(**target), where the Rust tokenizers backend receives an integer (~10^30) that exceeds its maximum value.

Root Cause
Models like DeBERTa don't define model_max_length in their tokenizer config. Transformers assigns VERY_LARGE_INTEGER (~10^30) as default. In transformers>=5, this value is passed to the Rust tokenizers backend via enable_truncation(), which overflows.

Reproduction:

import evaluate

bertscore = evaluate.load("bertscore")
results = bertscore.compute(
    predictions=["Hello world"],
    references=["Hi world"],
    model_type="microsoft/deberta-xlarge-mnli",
    num_layers=40,
    rescale_with_baseline=True,
    lang="en",
)

Environment:
transformers==5.2.0
tokenizers==0.22.2
evaluate==0.4.6
bert-score==0.3.13
Python 3.11

Suggested Fixes:
Pass through max_length parameter: The underlying bert_score.score() function supports a max_length parameter, but evaluate's BERTScore wrapper (BERTScore._compute()) does not accept or forward it. Adding support for this parameter would allow users to work around the issue.

Cap model_max_length internally: Before calling the tokenizer, check if model_max_length exceeds a reasonable threshold and cap it (e.g., to the model's actual supported max length).

Activity

  1. meivelan commented on Apr 25, 2026

    @meivelan

    Hi, I'd like to work on this. I'm planning to implement both suggested fixes

    1. Cap model_max_length internally when it exceeds a reasonable threshold before passing to the tokenizer backend.
    2. Add max_length parameter to BERTScore._compute() so users can explicitly override it.
  2. meivelan commented on May 1, 2026

    @meivelan

    After reading the code, I see that evaluate's _compute() doesn't load the tokenizer directly - it passes model_type to bert_score.BERTScorer which handles tokenization internally.

    It looks like, the change has to be done on underlying bert_score library!

    What do you think?

  3. ShamikOfficial commented on Sep 2, 2026

    @ShamikOfficial

    Hi! Opening a PR for this.

    Ran into the same OverflowError with DeBERTa-style tokenizers on transformers 5. This forwards max_length and clamps the placeholder model_max_length before bert-score tokenizes.

    #self-assign

  4. fitzyracing1 commented on Oct 9, 2026

    @fitzyracing1

    As noted above, the fix belongs in bert-score, which hasn't had a release since 2023 (Tiiiger/bert_score#205, unmerged PR #206). Until it has one: fitzyracing-bert-score 0.3.14 on PyPI is a drop-in fork of bert-score 0.3.13 with only this fix. The import name is still bert_score, so evaluate.load("bertscore") picks it up with no code changes. When the tokenizer reports the 1e30 placeholder, it encodes without truncation, as the slow tokenizers did, so scores match bert-score on transformers 4. Your microsoft/deberta-xlarge-mnli example works on transformers 5 with it:

    pip uninstall -y bert-score
    pip install fitzyracing-bert-score
    

    Details: https://github.com/fitzyracing1/bert_score

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions