huggingface/transformers · error · ValueError
can't derive true or false from {v} (key {k})
Error message
can't derive true or false from {v} (key {k}) What it means
ValueError from update_from_string when the target attribute is a bool but the provided value string is not one of the accepted truthy/falsy tokens (true/1/y/yes or false/0/n/no, case-insensitive). The parser refuses to guess a boolean from arbitrary text.
Source
Thrown at src/transformers/configuration_utils.py:1196
Args:
update_str (`str`): String with attributes that should be updated for this class.
"""
d = dict(x.split("=") for x in update_str.split(","))
for k, v in d.items():
if not hasattr(self, k):
raise ValueError(f"key {k} isn't in the original config dict")
old_v = getattr(self, k)
if isinstance(old_v, bool):
if v.lower() in ["true", "1", "y", "yes"]:
v = True
elif v.lower() in ["false", "0", "n", "no"]:
v = False
else:
raise ValueError(f"can't derive true or false from {v} (key {k})")
elif isinstance(old_v, int):
v = int(v)
elif isinstance(old_v, float):
v = float(v)
elif not isinstance(old_v, str):
raise TypeError(
f"You can only update int, float, bool or string values in the config, got {v} for key {k}"
)
setattr(self, k, v)
def dict_dtype_to_str(self, d: dict[str, Any]) -> None:
"""
Checks whether the passed dictionary and its nested dicts have a *dtype* key and if it's not None,
converts torch.dtype to a string of just the type. For example, `torch.float32` get converted into *"float32"*
string, which can then be stored in the json format.
"""
if d.get("dtype") is not None:View on GitHub (pinned to a597f97485)
Solutions
- Use one of the accepted tokens: true/false, 1/0, y/n, yes/no (any casing).
- Strip whitespace/quotes from each value before building the update string.
- Set booleans directly: config.scale_attn_weights = True.
Example fix
// before
config.update_from_string("scale_attn_weights=on") # ValueError
// after
config.update_from_string("scale_attn_weights=true") Defensive patterns
Strategy: validation
Validate before calling
BOOL_TOKENS = {"true", "1", "y", "yes", "false", "0", "n", "no"}
for k, v in parsed_pairs.items():
if isinstance(getattr(config, k, None), bool) and str(v).strip().lower() not in BOOL_TOKENS:
raise ValueError(f"{k}={v!r} is not a recognized boolean token") Type guard
def is_bool_token(v: str) -> bool:
return v.strip().strip('"\'').lower() in {"true", "1", "y", "yes", "false", "0", "n", "no"} Prevention
- Normalize boolean inputs to true/false before composing update strings.
- Strip whitespace and quotes from shell-provided values.
When it happens
Trigger: config.update_from_string("scale_attn_weights=maybe"), or passing 'True ' with whitespace, or 'on/off', or 't/f' which are not in the accepted token list.
Common situations: User-supplied CLI flags flowed into the update string with unexpected spellings; values produced by shell substitution that include quotes or spaces.
Related errors
- key {k} isn't in the original config dict
- You can only update int, float, bool or string values in the
- File not found: {audio}
- out_indices must be a list, got {type(self._out_indices)}
- out_indices must be valid indices for stage_names {self.stag
AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14).
Data as JSON: /api/errors/274ac13ca568ed5a.
Report an issue: GitHub.