unslothai/unsloth · error · ValueError

Keep KV on idle unload must be true or false.

Error message

Keep KV on idle unload must be true or false.

What it means

Raised by set_openai_auto_switch in openai_auto_switch_settings.py:315-317 when keep_kv is provided and _coerce_bool returns None. Accepted: real bools and strings 1/true/yes/on, 0/false/no/off (case-insensitive). Integers 1/0 and any other token ("keep", "y", "checked") are rejected.

Source

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

            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 "
                f"{MIN_AUTO_UNLOAD_IDLE_SECONDS}."
            )
    parsed_keep_kv = None
    if keep_kv is not None:
        parsed_keep_kv = _coerce_bool(keep_kv)
        if parsed_keep_kv is None:
            raise ValueError("Keep KV on idle unload must be true or false.")
    parsed_auto_download = None
    if auto_download is not None:
        parsed_auto_download = _coerce_bool(auto_download)
        if parsed_auto_download is None:
            raise ValueError("Auto-download missing models must be true or false.")
    parsed_api_only = None
    if api_only is not None:
        parsed_api_only = _coerce_bool(api_only)
        if parsed_api_only is None:
            raise ValueError("Auto-unload API-loaded only must be true or false.")
    from storage.studio_db import upsert_app_settings

    updates: dict[str, Any] = {OPENAI_AUTO_SWITCH_SETTING_KEY: parsed_enabled}
    if parsed_idle is not None:
        updates[AUTO_UNLOAD_IDLE_SETTING_KEY] = parsed_idle
    if parsed_media_idle is not None:
        updates[MEDIA_AUTO_UNLOAD_IDLE_SETTING_KEY] = parsed_media_idle
    if parsed_keep_kv is not None:

View on GitHub (pinned to 203007d190)

Solutions

  1. Pass a real bool (keep_kv=True)
  2. Normalize checkbox ints to bool: bool(int(raw)) before calling
  3. Whitelist the coercion tokens in your validation layer
  4. Omit the argument to leave the stored setting unchanged

Example fix

# before
set_openai_auto_switch(enabled=True, keep_kv=1)

# after
set_openai_auto_switch(enabled=True, keep_kv=True)
Defensive patterns

Strategy: validation

Validate before calling

def to_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 keep_kv is not None and to_bool(keep_kv) is None:
    raise HTTPException(400, "keep_kv must be a boolean")

Type guard

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

Try / catch

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

Prevention

When it happens

Trigger: set_openai_auto_switch(enabled=True, keep_kv=1) from a checkbox that yields ints; keep_kv="keep"; keep_kv=[True]. Python None is allowed (leave stored value).

Common situations: HTML checkbox handlers sending "1"/"0" as ints after JSON parsing; API clients normalizing booleans to ints; truthy strings from environment variables like "yes please".

Related errors


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