unslothai/unsloth · error · ValueError

OpenAI auto-switch must be true or false.

Error message

OpenAI auto-switch must be true or false.

What it means

Raised by set_openai_auto_switch in openai_auto_switch_settings.py:291 when the enabled argument cannot be coerced to bool. _coerce_bool accepts real bools plus the strings "1/true/yes/on" and "0/false/no/off" (case-insensitive, trimmed). Notably it does NOT accept integers: passing 1 or 0 raises this error because isinstance(1, bool) is False.

Source

Thrown at studio/backend/utils/openai_auto_switch_settings.py:291

def get_auto_unload_api_only() -> bool:
    """Whether the idle unload spares models a user loaded from the UI."""
    parsed = _coerce_bool(_cached_setting(AUTO_UNLOAD_API_ONLY_SETTING_KEY, None))
    return parsed if parsed is not None else DEFAULT_AUTO_UNLOAD_API_ONLY


def set_openai_auto_switch(
    enabled: Any,
    idle_seconds: Any,
    keep_kv: Any = None,
    auto_download: Any = None,
    api_only: Any = None,
    media_idle_seconds: Any = None,
) -> tuple[bool, int, bool, bool, bool, int]:
    """One-transaction write; ``None`` leaves a stored value untouched."""
    parsed_enabled = _coerce_bool(enabled)
    if parsed_enabled is None:
        raise ValueError("OpenAI auto-switch must be true or false.")
    parsed_idle = None
    if idle_seconds is not None:
        parsed_idle = _coerce_int(idle_seconds)
        if parsed_idle is None:
            raise ValueError("Auto-unload idle seconds must be a non-negative integer.")
        if 0 < parsed_idle < MIN_AUTO_UNLOAD_IDLE_SECONDS:
            raise ValueError(
                f"Auto-unload idle seconds must be 0 (off) or at least "
                f"{MIN_AUTO_UNLOAD_IDLE_SECONDS}."
            )
    parsed_media_idle = None
    if media_idle_seconds is not None:
        parsed_media_idle = _coerce_int(media_idle_seconds)
        if parsed_media_idle is None:
            raise ValueError("Media auto-unload idle seconds must be a non-negative integer.")
        if 0 < parsed_media_idle < MIN_AUTO_UNLOAD_IDLE_SECONDS:
            raise ValueError(
                f"Media auto-unload idle seconds must be 0 (off) or at least "

View on GitHub (pinned to 203007d190)

Solutions

  1. Pass an actual bool: enabled=True / enabled=False
  2. If the value comes from user input, normalize with the same tokens: strip+lowercase and accept 1/true/yes/on vs 0/false/no/off
  3. Change the client to send JSON booleans, not 0/1
  4. Validate with _coerce_bool before calling and map failures to a user-facing form error

Example fix

# before
set_openai_auto_switch(enabled=1, idle_seconds=120)  # int -> ValueError

# after
set_openai_auto_switch(enabled=True, idle_seconds=120)
Defensive patterns

Strategy: validation

Validate before calling

def coerce_setting_bool(v) -> bool | None:
    if isinstance(v, bool):
        return v
    if isinstance(v, str):
        n = v.strip().lower()
        if n in {"1", "true", "yes", "on"}: return True
        if n in {"0", "false", "no", "off", ""}: return False
    return None

if coerce_setting_bool(enabled) is None:
    raise HTTPException(400, "enabled must be a boolean")

Type guard

def is_settable_bool(v: object) -> bool:
    if isinstance(v, bool):
        return True
    return isinstance(v, str) and v.strip().lower() in {"1","true","yes","on","0","false","no","off",""}

Try / catch

try:
    set_openai_auto_switch(enabled=raw_enabled)
except ValueError as exc:
    raise HTTPException(400, str(exc)) from exc

Prevention

When it happens

Trigger: Calling set_openai_auto_switch(enabled=1), set_openai_auto_switch(enabled="enable"), or passing None. Also any int/float value, or unrecognized strings like "y"/"enabled".

Common situations: Frontend or API client sends JSON true/false that gets coerced to 1/0 by a middleware; scripts pass Python ints; query-string parsing yields unhandled tokens like "enabled".

Related errors


AI-assisted analysis of unslothai/unsloth@203007d190 (2026-08-15). Data as JSON: /api/errors/f4557d50edcc3793. Report an issue: GitHub.