{"record":{"id":"8867c2e7fb39101d","repo":"unslothai/unsloth","slug":"auto-unload-api-loaded-only-must-be-true-or-false","errorCode":null,"errorMessage":"Auto-unload API-loaded only must be true or false.","messagePattern":"Auto-unload API-loaded only must be true or false\\.","errorType":"validation","errorClass":"ValueError","httpStatus":400,"severity":"error","filePath":"studio/backend/utils/openai_auto_switch_settings.py","lineNumber":326,"sourceCode":"            raise ValueError(\n                f\"Media auto-unload idle seconds must be 0 (off) or at least \"\n                f\"{MIN_AUTO_UNLOAD_IDLE_SECONDS}.\"\n            )\n    parsed_keep_kv = None\n    if keep_kv is not None:\n        parsed_keep_kv = _coerce_bool(keep_kv)\n        if parsed_keep_kv is None:\n            raise ValueError(\"Keep KV on idle unload must be true or false.\")\n    parsed_auto_download = None\n    if auto_download is not None:\n        parsed_auto_download = _coerce_bool(auto_download)\n        if parsed_auto_download is None:\n            raise ValueError(\"Auto-download missing models must be true or false.\")\n    parsed_api_only = None\n    if api_only is not None:\n        parsed_api_only = _coerce_bool(api_only)\n        if parsed_api_only is None:\n            raise ValueError(\"Auto-unload API-loaded only must be true or false.\")\n    from storage.studio_db import upsert_app_settings\n\n    updates: dict[str, Any] = {OPENAI_AUTO_SWITCH_SETTING_KEY: parsed_enabled}\n    if parsed_idle is not None:\n        updates[AUTO_UNLOAD_IDLE_SETTING_KEY] = parsed_idle\n    if parsed_media_idle is not None:\n        updates[MEDIA_AUTO_UNLOAD_IDLE_SETTING_KEY] = parsed_media_idle\n    if parsed_keep_kv is not None:\n        updates[AUTO_UNLOAD_KEEP_KV_SETTING_KEY] = parsed_keep_kv\n    if parsed_auto_download is not None:\n        updates[OPENAI_AUTO_DOWNLOAD_SETTING_KEY] = parsed_auto_download\n    if parsed_api_only is not None:\n        updates[AUTO_UNLOAD_API_ONLY_SETTING_KEY] = parsed_api_only\n    upsert_app_settings(updates)\n    _invalidate(OPENAI_AUTO_SWITCH_SETTING_KEY)\n    if parsed_idle is not None:\n        _invalidate(AUTO_UNLOAD_IDLE_SETTING_KEY)\n    if parsed_media_idle is not None:","sourceCodeStart":308,"sourceCodeEnd":344,"githubUrl":"https://github.com/unslothai/unsloth/blob/203007d19051dcd2ae33876786d117c99f6b0368/studio/backend/utils/openai_auto_switch_settings.py#L308-L344","documentation":"Raised by set_openai_auto_switch in openai_auto_switch_settings.py:324-327 when api_only is provided and _coerce_bool fails. Accepted inputs are exactly True/False or the strings 1/true/yes/on and 0/false/no/off; ints, floats, and any other string raise this error. api_only restricts auto-unload to API-loaded models.","triggerScenarios":"set_openai_auto_switch(enabled=True, api_only=1) (int), api_only=\"only\", api_only=\"api\". Python None skips the field entirely.","commonSituations":"REST payloads where the toggle was serialized as an int; query parameters parsed to ints; scripts passing flags like \"yes!\".","solutions":["Convert to bool before calling: bool flag from your own checkbox handling","Use one of the accepted string tokens if strings are unavoidable","Send native JSON booleans from the API client","Leave the argument out to preserve the stored setting"],"exampleFix":"# before\nset_openai_auto_switch(enabled=True, api_only=1)\n\n# after\nset_openai_auto_switch(enabled=True, api_only=True)","handlingStrategy":"validation","validationCode":"def to_bool(v) -> bool | None:\n    if isinstance(v, bool):\n        return v\n    if isinstance(v, str):\n        n = v.strip().lower()\n        if n in {\"1\", \"true\", \"yes\", \"on\"}: return True\n        if n in {\"0\", \"false\", \"no\", \"off\"}: return False\n    return None\n\nif api_only is not None and to_bool(api_only) is None:\n    raise HTTPException(400, \"api_only must be a boolean\")","typeGuard":"def is_bool_like(v: object) -> bool:\n    return isinstance(v, bool) or (\n        isinstance(v, str) and v.strip().lower() in {\"1\",\"true\",\"yes\",\"on\",\"0\",\"false\",\"no\",\"off\"}\n    )","tryCatchPattern":"try:\n    set_openai_auto_switch(enabled=True, api_only=api_only)\nexcept ValueError as exc:\n    raise HTTPException(400, str(exc)) from exc","preventionTips":["Send native JSON booleans for every settings toggle","Centralize coercion so all six settings share one validator","Reject non-boolean types at the request schema layer"],"tags":["settings","validation","boolean-coercion"],"backgroundTag":null,"analyzedSha":"203007d19051dcd2ae33876786d117c99f6b0368","analyzedAt":"2026-08-15T02:48:39.846Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}