huggingface/transformers · error · ValueError
Framework '{return_tensors}' not recognized!
Error message
Framework '{return_tensors}' not recognized! What it means
Raised by DataCollatorMixin.__call__ (data_collator.py:46). The collator dispatches on the requested tensor framework: 'pt' routes to torch_call, 'np' routes to numpy_call; anything else (including 'tf', 'jax', or a typo like 'torch'/'numpy') is not recognized and raises. Note this dispatch only supports torch and numpy — the TensorFlow/JAX paths were removed from this code path.
Source
Thrown at src/transformers/data/data_collator.py:46
InputDataClass = Any
"""
A DataCollator is a function that takes a list of samples from a Dataset and collate them into a batch, as a dictionary
of PyTorch tensors or NumPy arrays.
"""
DataCollator = Callable[[list[InputDataClass]], dict[str, Any]]
class DataCollatorMixin:
def __call__(self, features, return_tensors: str | None = None):
if return_tensors is None:
return_tensors = self.return_tensors
if return_tensors == "pt":
return self.torch_call(features)
elif return_tensors == "np":
return self.numpy_call(features)
else:
raise ValueError(f"Framework '{return_tensors}' not recognized!")
def pad_without_fast_tokenizer_warning(tokenizer, *pad_args, **pad_kwargs):
"""
Pads without triggering the warning about how using the pad function is sub-optimal when using a fast tokenizer.
"""
# To avoid errors when using Feature extractors
if not hasattr(tokenizer, "deprecation_warnings"):
return tokenizer.pad(*pad_args, **pad_kwargs)
# Save the state of the warning, then disable it
warning_state = tokenizer.deprecation_warnings.get("Asking-to-pad-a-fast-tokenizer", False)
tokenizer.deprecation_warnings["Asking-to-pad-a-fast-tokenizer"] = True
try:
padded = tokenizer.pad(*pad_args, **pad_kwargs)
finally:View on GitHub (pinned to a597f97485)
Solutions
- Use 'pt' for PyTorch or 'np' for NumPy.
- If you passed 'torch'/'numpy' out of habit, switch to the exact short codes 'pt'/'np'.
- If you were relying on 'tf'/'jax' output, produce numpy output ('np') and convert with tf.convert_to_tensor / jnp.array in your training loop.
Example fix
# before
batch = collator(features, return_tensors='tf')
# after
import numpy as np, tensorflow as tf
batch_np = collator(features, return_tensors='np')
batch = {k: tf.convert_to_tensor(v) for k, v in batch_np.items()} Defensive patterns
Strategy: validation
Validate before calling
ALLOWED = {'pt', 'np'}
assert return_tensors in ALLOWED, f"return_tensors must be one of {ALLOWED}, got {return_tensors!r}"
batch = collator(features, return_tensors=return_tensors) Type guard
def is_supported_return_tensors(rt: str | None) -> bool:
return rt in ('pt', 'np', None) Prevention
- Use only 'pt' or 'np' with collators; convert to tf/jax tensors downstream yourself.
- Centralize the return_tensors choice in one config constant so typos surface once.
- When porting older scripts, grep for return_tensors='tf'/'jax' — they are no longer accepted here.
When it happens
Trigger: Calling collator(features, return_tensors='tf'), return_tensors='jax', return_tensors='torch' (instead of 'pt'), or constructing a dataclass collator with return_tensors='tf' as a field default; the dispatch in __call__ then hits the else branch.
Common situations: Porting old training scripts (written when 'tf' was accepted) to current transformers; passing the string 'torch'/'numpy' because other APIs (e.g. tokenizer __call__/pad use return_tensors='pt'|'np') are remembered differently; stale tutorials.
Related errors
- return_tensors must be one of ("pt", "np"), {return_tensors=
- Cannot assign to field {name}, you should create a new insta
- You are attempting to pad samples but the tokenizer you are
- This tokenizer does not have a mask token which is necessary
- mlm_probability should be between 0 and 1.
AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14).
Data as JSON: /api/errors/9d93c58827dc7602.
Report an issue: GitHub.