huggingface/transformers · error · TypeError

You can only update int, float, bool or string values in the

Error message

You can only update int, float, bool or string values in the config, got {v} for key {k}

What it means

TypeError from update_from_string when the existing attribute's type is not int, float, bool, or str (e.g. a list, dict, or None), so the parser cannot convert the string value. The update-string mechanism deliberately supports only scalar types.

Source

Thrown at src/transformers/configuration_utils.py:1202

        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:
            if isinstance(d["dtype"], dict):
                d["dtype"] = {k: str(v).split(".")[-1] for k, v in d["dtype"].items()}
            # models like Emu3 can have "dtype" as token in config's vocabulary map,
            # so we also exclude int type here to avoid error in this special case.
            elif not isinstance(d["dtype"], (str, int)):
                d["dtype"] = str(d["dtype"]).split(".")[1]

View on GitHub (pinned to a597f97485)

Solutions

  1. Set non-scalar attributes directly in Python: config.layer_types = ["dense", "dense"].
  2. If the attribute is None because it is optional, first assign a typed default, then use update_from_string only for scalars.
  3. Restrict update_from_string usage to int/float/bool/str keys.

Example fix

// before
config.update_from_string("layer_types=dense,dense")  # TypeError

// after
config.layer_types = ["dense", "dense"]
Defensive patterns

Strategy: type-guard

Validate before calling

for k, v in updates.items():
    old = getattr(config, k, None)
    if not isinstance(old, (bool, int, float, str)) or old is None:
        raise TypeError(f"key {k} has non-scalar type {type(old).__name__}; set it directly")

Type guard

def is_scalar_config_value(old) -> bool:
    return isinstance(old, (bool, int, float, str))

Try / catch

try:
    config.update_from_string(s)
except TypeError as e:
    if "only update int, float, bool or string" in str(e):
        # apply via direct attribute assignment instead
        raise

Prevention

When it happens

Trigger: config.update_from_string("layer_types=[dense, dense]") where layer_types is a list, or updating a key whose current value is None (e.g. an unset optional field).

Common situations: Attempting to tweak list-typed config fields (layer_types, rope scaling dicts, activation lists) through the string API; configs where optional attributes default to None.

Related errors


AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14). Data as JSON: /api/errors/5d71fbd588e568ba. Report an issue: GitHub.