Skip to content

Return an empty list from batch_decode for an empty batch - #50873

Merged
ArthurZucker merged 3 commits into
huggingface:mainfrom
zaidwhy:fix/batch-decode-empty
Nov 19, 2026
Merged

ArthurZucker merged 3 commits into
huggingface:mainfrom
zaidwhy:fix/batch-decode-empty

Conversation

@zaidwhy

@zaidwhy zaidwhy commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

🤗 Transformers CI

batch_decode() returns [''] instead of [] when given an empty batch. I reproduced this with a list, NumPy array and PyTorch tensor.

This is a regression from v4.57. I checked the old implementation, where batch_decode() used a list comprehension and correctly returned an empty list. In v5, it passes the whole input to decode(), which causes the issue.

This can cause prediction and reference counts to mismatch, which can lead to incorrect evaluation results. It also affects multimodal processors that use the tokenizer's batch_decode().

Minimal reproduction

from transformers import AutoTokenizer
tok = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2")
print(tok.batch_decode([]))   # [''] on main, [] with this fix

Reproduction

I also created a small evaluation example using hf-internal-testing/tiny-random-gpt2. It generates sequences and keeps only rows ending with EOS. When no rows match, the resulting tensor has shape (0, 6).

  • Main: preds: [''], len(preds)=1, len(refs)=0
  • With this fix: preds: [], counts match.

This is a reproduction I put together to show how the bug can happen in an evaluation loop, not a reported user issue.

Fix

Added an early check in tokenization_utils_base.py to return [] when the batch is empty.

Other behaviour stays the same:

  • decode([]) still returns ''.
  • [[], []] (shape (2, 0)) still returns ['', ''].

Tests

Added two tests in tests/tokenization/test_tokenization_utils.py. Both fail on main and pass with this change.

Full module: 31 passed, 1 skipped.

I also read the Mistral-common and Marian implementations. Both call super().batch_decode(), so they get the fix too.

cc @ArthurZucker @itazap (tokenizers)

@github-actions

Copy link
Copy Markdown
Contributor

Thank you for your contribution 🤗!

CI Security Gate — waiting for maintainer approval

CI is approved automatically only for authors who already have a contribution merged in this repository. A maintainer will review this PR and approve CI to run on it.

@github-actions

Copy link
Copy Markdown
Contributor

This PR might need docs.

Beyond the empty-batch fix, this PR makes batch_decode() emit a deprecation warning when it receives a single sequence (e.g. batch_decode([1, 2, 3])), telling users to call decode instead — code that previously ran silently now warns, so users need to know about the upcoming removal. The empty-batch behavior (batch_decode([]) now returns [] instead of ['']) is also worth a one-line note since it changes returned list lengths.

Pages that might need an update:

  • docs/source/en/main_classes/tokenizer.md

Posted automatically after approval. Docs live in docs/source/en/, and updating them turns the docs-check check green. The check doesn't block merging.

@HuggingFaceDocBuilderDev

HuggingFaceDocBuilderDev commented Nov 18, 2026 •

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Pages changed in this PR:

@zaidwhy

zaidwhy commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review and the cleanup commit. I updated the Mistral test_batch_decode test that was failing (it still expected [''] for an empty batch) and added a short note to the tokenizer docs. Could someone approve the CI run when you get a chance?

@ArthurZucker
ArthurZucker added this pull request to the merge queue Nov 19, 2026
Merged via the queue into huggingface:main with commit c6f989d Nov 19, 2026
127 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants