PaddlePaddle/PaddleOCR · error · ValueError
{value} is not a valid {cls.__name__}, please select one of
Error message
{value} is not a valid {cls.__name__}, please select one of {list(cls._value2member_map_.keys())} What it means
ExplicitEnum is the base class (copied from HuggingFace transformers) for tokenizer strategy enums such as TruncationStrategy. Python calls _missing_ when a value cannot be matched to a member; this override replaces the default cryptic message with an explicit list of valid values.
Source
Thrown at ppocr/data/imaug/label_ops.py:1898
for idx, seq in enumerate(process_seq):
l = len(seq)
labels[idx][:l] = seq
topk[k] = labels
return (
np.array(topk["input_ids"]).astype(np.int64),
np.array(topk["attention_mask"]).astype(np.int64),
max_length,
)
class ExplicitEnum(str, Enum):
"""
Enum with more explicit error message for missing values.
"""
@classmethod
def _missing_(cls, value):
raise ValueError(
f"{value} is not a valid {cls.__name__}, please select one of {list(cls._value2member_map_.keys())}"
)
class TruncationStrategy(ExplicitEnum):
"""
Possible values for the `truncation` argument in [`PreTrainedTokenizerBase.__call__`]. Useful for tab-completion in
an IDE.
"""
ONLY_FIRST = "only_first"
ONLY_SECOND = "only_second"
LONGEST_FIRST = "longest_first"
DO_NOT_TRUNCATE = "do_not_truncate"
class PaddingStrategy(ExplicitEnum):
"""View on GitHub (pinned to 2661c7c0ef)
Solutions
- Read the valid values from the error message itself and use one of them exactly, e.g. truncation: only_first or longest_only equivalent ('longest' for padding)
- If the setting comes from YAML, quote the string and avoid boolean keywords: truncation: 'only_first', not truncation: yes
- Check for boolean leakage: use str(value).lower() comparisons or normalize config values before passing them to the tokenizer
- Align the transformers version with the one PaddleOCR pins if you also import HF tokenizers
Example fix
# before tokenizer(text, truncation='True') # after from ppocr.data.imaug.label_ops import TruncationStrategy tokenizer(text, truncation=TruncationStrategy.ONLY_FIRST)
Defensive patterns
Strategy: validation
Validate before calling
from ppocr.data.imaug.label_ops import TruncationStrategy
if isinstance(truncation, str):
if truncation not in TruncationStrategy._value2member_map_:
raise SystemExit(f'truncation must be one of {list(TruncationStrategy._value2member_map_)}')
truncation = TruncationStrategy(truncation) Type guard
def is_valid_truncation(v) -> bool:
from ppocr.data.imaug.label_ops import TruncationStrategy
return v is None or v in TruncationStrategy._value2member_map_ or isinstance(v, TruncationStrategy) Try / catch
try:
tokenizer(texts, truncation=truncation)
except ValueError as e:
if 'is not a valid' in str(e):
# fall back to a safe default instead of crashing the loader
tokenizer(texts, truncation='only_first')
else:
raise Prevention
- Normalize config strings: reject YAML booleans for enum-typed settings
- Pass enum members (TruncationStrategy.ONLY_FIRST) instead of raw strings from code
- Centralize tokenizer kwarg construction so validation happens in one place
When it happens
Trigger: Passing a 'truncation' (or padding) argument to the tokenizer used by the recognition label pipeline whose value is not one of the enum members, e.g. truncation='True' (string), truncation='both', or a boolean True.
Common situations: Reading truncation/padding settings from a yml config where YAML renders booleans as strings; passing Python True instead of 'longest'/'only_first'; version drift between transformers and the vendored tokenizer code in label_ops.py.
Related errors
- Found a {token.__class__} in the saved `added_tokens_decoder
- RecResizeImg.image_shape is required in rec inference.yml
- Unexpected recognition channels: ${String(channels)}
- OCR pipeline config text must decode to an object.
- OCR pipeline config must be an object or YAML text.
AI-assisted analysis of PaddlePaddle/PaddleOCR@2661c7c0ef (2026-08-14).
Data as JSON: /api/errors/2304e340714e6801.
Report an issue: GitHub.