Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Import: check for a broken openai install on first use instead of at import unsloth (#15702)
Daniel Han549b140e08e2
diff --git a/tests/test_lazy_openai_import_check.py b/tests/test_lazy_openai_import_check.pynew file mode 100644index 00000000000..a4f667fa136--- /dev/null+++ b/tests/test_lazy_openai_import_check.py@@ -0,0 +1,119 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.++"""`import unsloth` must not import the openai SDK itself (about 0.6 s), while a broken openai+install still reads as unavailable through `transformers.utils.is_openai_available`."""++import os+import subprocess+import sys+from pathlib import Path++import pytest++pytest.importorskip("transformers")+pytest.importorskip("torch")++_TESTS = str(Path(__file__).resolve().parent)+_MSG = "Unsloth: OpenAI failed to import - ignoring for now."++# Records the direct importer, so trl<0.26 judges importing openai is not blamed on unsloth.+_HEALTHY = """+import sys+_importers = []+_f = sys._getframe(1)+while _f is not None and "importlib" in _f.f_code.co_filename:+ _f = _f.f_back+if _f is not None:+ _importers.append((_f.f_code.co_filename.replace("\\\\", "/"), _f.f_code.co_name))+class OpenAI:+ pass+"""++_BROKEN = 'raise TypeError("simulated broken openai install")\n'++_SLOW_BROKEN = 'import time\ntime.sleep(1.0)\nraise TypeError("simulated broken openai install")\n'++_CHECK = r"""+import os, sys+os.environ.setdefault("UNSLOTH_COMPILE_DISABLE", "1")+os.environ.setdefault("TORCHDYNAMO_DISABLE", "1")+sys.path.insert(0, sys.argv[1])+import _zoo_aggressive_cuda_spoof as _spoof+_spoof.apply()+import unsloth+import transformers.utils as tu+import transformers.utils.import_utils as iu+mode = sys.argv[2]+if mode == "threads":+ import threading+ print("CALLS", flush = True)+ from unsloth.models._utils import _lazy_openai_import_check+ check, answers = _lazy_openai_import_check(iu.is_openai_available), []+ workers = [threading.Thread(target = lambda: answers.append(check())) for _ in range(4)]+ for w in workers:+ w.start()+ for w in workers:+ w.join()+ assert answers == [False] * 4, answers+elif mode == "healthy":+ importers = getattr(sys.modules.get("openai"), "_importers", [])+ eager = [f for f in importers if f[0].endswith("unsloth/models/_utils.py") and f[1] == "<module>"]+ assert not eager, eager+ assert tu.is_openai_available() is True+ assert "openai" in sys.modules+else:+ print("CALLS", flush = True)+ assert tu.is_openai_available() is False+ assert tu.is_openai_available() is False+ assert iu.is_openai_available() is True+cells = [c.cell_contents for c in (tu.is_openai_available.__closure__ or ())]+assert getattr(tu.is_openai_available, "_unsloth_lazy_openai_check", False), tu.is_openai_available+assert any(getattr(c, "__module__", "") == iu.__name__ and c.__name__ == "is_openai_available" for c in cells), cells+print("OK", flush = True)+"""+++def _fake_openai(root, body):+ pkg = root / "openai"+ pkg.mkdir()+ (pkg / "__init__.py").write_text(body, encoding = "utf-8")+ dist = root / "openai-9.9.9.dist-info"+ dist.mkdir()+ (dist / "METADATA").write_text(+ "Metadata-Version: 2.1\nName: openai\nVersion: 9.9.9\n", encoding = "utf-8"+ )+ (dist / "top_level.txt").write_text("openai\n", encoding = "utf-8")+++def _run(tmp_path, mode, body):+ _fake_openai(tmp_path, body)+ env = dict(os.environ)+ env["PYTHONPATH"] = os.pathsep.join(p for p in (str(tmp_path), env.get("PYTHONPATH")) if p)+ # mlx hosts would otherwise skip unsloth/models/_utils.py.+ env["UNSLOTH_FORCE_GPU_PATH"] = "1"+ result = subprocess.run(+ [sys.executable, "-c", _CHECK, _TESTS, mode],+ capture_output = True,+ text = True,+ timeout = 900,+ env = env,+ )+ assert result.returncode == 0 and "OK" in result.stdout, (+ result.stdout[-2000:] + result.stderr[-4000:]+ )+ return result.stdout+++def test_import_unsloth_does_not_import_openai_itself(tmp_path):+ _run(tmp_path, "healthy", _HEALTHY)+++def test_broken_openai_still_reads_unavailable_and_warns_once(tmp_path):+ out = _run(tmp_path, "broken", _BROKEN)+ assert out.count(_MSG) == 1, out[-2000:]+++def test_concurrent_first_calls_on_a_broken_openai_warn_once(tmp_path):+ out = _run(tmp_path, "threads", _SLOW_BROKEN)+ assert out.split("CALLS", 1)[1].count(_MSG) == 1, out[-2000:]diff --git a/unsloth/models/_utils.py b/unsloth/models/_utils.pyindex d96cee0bc5c..4cbb2e22bc7 100644--- a/unsloth/models/_utils.py+++ b/unsloth/models/_utils.py@@ -4314,19 +4314,40 @@ else:torch_amp_custom_bwd = torch.amp.custom_bwd(device_type = DEVICE_TYPE_TORCH)-from transformers.utils import is_openai_available+import transformers.utils+++def _lazy_openai_import_check(is_available):+ # transformers reads only metadata, so a broken openai install (#1182, Databricks) reports True+ # and crashes importers like trl's judges: try the real import once, on first use, under a lock.+ import_ok = []+ lock = threading.RLock()++ @functools.wraps(is_available)+ def is_openai_available():+ available = is_available()+ if not available:+ return available+ if not import_ok:+ with lock:+ if not import_ok:+ try:+ from openai import OpenAI # noqa: F401+ except Exception:+ print("Unsloth: OpenAI failed to import - ignoring for now.")+ import_ok.append(False)+ else:+ import_ok.append(True)+ return available if import_ok[0] else False-if is_openai_available():- try:- from openai import OpenAI- except:- print("Unsloth: OpenAI failed to import - ignoring for now.")- import transformers.utils+ is_openai_available._unsloth_lazy_openai_check = True+ return is_openai_available- def _is_openai_available():- return False- transformers.utils.is_openai_available = _is_openai_available+if not getattr(transformers.utils.is_openai_available, "_unsloth_lazy_openai_check", False):+ transformers.utils.is_openai_available = _lazy_openai_import_check(+ transformers.utils.is_openai_available+ )from transformers import AutoTokenizerfrom transformers.utils.import_utils import _is_package_available