xai-org/x-algorithm · error · ValueError

Expected bool [True, true, False, false], got {val!r}

Error message

Expected bool [True, true, False, false], got {val!r}

What it means

cast_type converts string values (usually from CLI overrides) into typed config fields. When the target type is bool, only the strings 'True','true','False','false' are accepted; anything else raises this ValueError.

Source

Thrown at phoenix/python/common/xai-configlib/src/xai_configlib/__init__.py:407

                new_dcls = dataclasses.replace(new_dcls, **{name: new_dict})

        if path[-len(key_path) :] == key_path:
            if value == "None" and is_optional(ty_hints[name]):
                new_dcls = dataclasses.replace(dcls, **{name: None})
            else:
                ty = ty_hints[name]
                new_dcls = dataclasses.replace(new_dcls, **{name: cast_type(ty, value)})
            used.append(path)
    return new_dcls


def cast_type(ty: Type[Any], val: str):
    if ty is bool:
        if val in ("False", "false"):
            return False
        elif val in ("True", "true"):
            return True
        raise ValueError(f"Expected bool [True, true, False, false], got {val!r}")
    elif is_optional(ty) and val == "None":
        return None
    elif is_union(ty):
        subtys = [subty for subty in get_args(ty) if subty is not type(None)]
        for subty in subtys:
            try:
                return cast_type(subty, val)
            except ValueError:
                if subty is subtys[-1]:
                    raise
        raise TypeError(f"Got {val} for union type {ty}")
    elif is_tuple(ty):
        if not val:
            return ()
        vals = val.split(",")
        tys = get_args(ty)
        if len(vals) != len(tys):
            raise TypeError(f"Tuple {ty} has different number of arguments from given value {vals}")

View on GitHub (pinned to 24c60942c5)

Solutions

  1. Use 'True'/'False' (case-insensitive first letter) e.g. 'use_amp=False'
  2. For numeric truthiness, change the config field to int or convert before passing
  3. Quote the value in shell so it arrives intact

Example fix

# before
replace_cli_subs(cfg, ["use_amp=1"])
# after
replace_cli_subs(cfg, ["use_amp=True"])
Defensive patterns

Strategy: type-guard

Validate before calling

BOOL_STRINGS = {"True", "true", "False", "false"}
def is_bool_castable(s: str) -> bool:
    return s in BOOL_STRINGS

Type guard

def is_bool_override(field_type, value: str) -> bool:
    return field_type is not bool or value in ("True","true","False","false")

Try / catch

try:
    cast_type(bool, val)
except ValueError as e:
    raise SystemExit(f"Bad bool override: {val!r}; use True/False") from e

Prevention

When it happens

Trigger: Passing a bool override like 'use_amp=1', 'use_amp=yes', or 'use_amp=None' where the config field is annotated bool; cast_type(ty=bool, val='1').

Common situations: Using 0/1 or y/n conventions from shell scripts, passing empty strings, or overriding a bool field with a value meant for a different field.

Related errors


AI-assisted analysis of xai-org/x-algorithm@24c60942c5 (2026-08-28). Data as JSON: /api/errors/a20e1f5045f2a060. Report an issue: GitHub.