Skip to content

Import: check for a broken openai install on first use instead of at import unsloth - #15702

Merged
danielhanchen merged 5 commits into
mainfrom
perf/lazy-openai-check
Oct 28, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
perf/lazy-openai-check

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

import unsloth imports the whole openai SDK (522 modules, plus httpx2 / anyio / sniffio) whenever openai is installed, which it is on Colab and in Studio venvs. Nothing uses it: unsloth/models/_utils.py ran from openai import OpenAI only so that a broken openai install could flip transformers.utils.is_openai_available to False. This PR does the same check on the first call to transformers.utils.is_openai_available instead of at import.

Why the check exists

It came in with #1182 ("Weird Databricks errors"). transformers decides openai is available from the package metadata alone, so a broken install (one that raises on import) still reports True. trl 0.18 to 0.25 then crash: trl/trainer/judges.py runs if is_openai_available(): from openai import OpenAI at module level, and import unsloth imports that module while it patches the online DPO / XPO / Nash-MD trainers. trl 0.26 and later only import openai inside OpenAIPairwiseJudge.__init__, after the same check, and trl 1.15 / 1.16 have no judges at all.

What changes

  • transformers.utils.is_openai_available is wrapped once (a marker attribute keeps a second run from wrapping it again). The wrapper calls the original. If that says True, the first call tries from openai import OpenAI, and if the import fails it prints the same Unsloth: OpenAI failed to import - ignoring for now. once and returns False from then on. Otherwise it returns the original's value. The first import attempt runs under a lock, so first calls from several threads at once make one attempt and print the message once, as the import-time check did.
  • Bindings made before unsloth patches are unchanged, as before: transformers.utils.import_utils.is_openai_available, BACKENDS_MAPPING / requires_backends, is_serve_available (5.x) and commands/serving.py (4.57.6) never saw the old patch either. Modules that read transformers.utils.is_openai_available after unsloth (trl judges, trl env, transformers.testing_utils) get the wrapper, as they got the old replacement.
  • except Exception instead of a bare except:, so a Ctrl-C during a later first call is not swallowed.

Results

Fresh process per run, CUDA spoof (tests/_zoo_aggressive_cuda_spoof.py), openai 3.40.0 installed, arms interleaved:

main this PR
modules in sys.modules after import unsloth, transformers 4.57.6 + trl 1.15.0 6047 (522 openai) 5489 (0 openai)
same, transformers 5.21.0 + trl 1.15.0 6222 (522 openai) 5698 (0 openai)
same, transformers 4.57.6 + trl 0.25.1 6092 (522 openai) 6092 (522 openai, imported by trl's judges)
deferred work: first is_openai_available() after import unsloth (7 runs) 0 ms 371 ms CPU (321 to 437)
import unsloth main-thread CPU, 4.57.6 + trl 1.15.0 (9 pairs) 13.67 s (13.12 to 15.04) 13.07 s (12.15 to 13.61)

The paired difference is -0.56 s (median of 9) for 4.57.6 + trl 1.15.0 and -0.53 s for 5.21.0 + trl 1.15.0. The host was heavily loaded, and the gap between consecutive main runs (A/A) was 0.5 to 1.0 s, so the module counts and the deferred-cost row are the solid numbers. With trl 0.25 or older (Studio pins 0.23.1), trl's judges import openai during import unsloth anyway, so nothing changes there.

Behaviour

For healthy, missing and broken openai (a fake package with metadata whose __init__ raises), on main and on this branch, with transformers 4.57.6 + trl 0.25.1, 4.57.6 + trl 1.15.0, 5.17.0 + trl 1.15.0 and 5.21.0 + trl 1.15.0, these are identical between the arms: transformers.utils.is_openai_available() (first and second call, value and type), import_utils.is_openai_available(), requires_backends(obj, ["openai"]), is_serve_available(), trl judges import and OpenAIPairwiseJudge(...) (constructed when openai is healthy, ValueError: OpenAI client is not installed when it is missing or broken), and the warning count (one per process).

The only difference is when the broken-install warning prints: at the first is_openai_available() call instead of during import unsloth. With trl 0.25 or older that call still happens during import unsloth (judges), so the warning appears at the same point. With newer trl it appears just before something that needs openai, or not at all if nothing does. A broken install can only be detected by running the import, so a cheap eager check (for example find_spec) cannot keep the old timing.

Tests

  • New tests/test_lazy_openai_import_check.py:
    • with a fake healthy openai, import unsloth must not import it from _utils.py module level (fails on main);
    • with a fake broken openai, is_openai_available() returns False twice, the transformers original still returns True, the warning prints once, and the patch wraps exactly one layer;
    • four threads making the first call at once on a broken openai that takes 1 s to fail get False and print the warning once (without the lock it printed 4 times).
  • All three pass on this branch and fail on main under transformers 4.57.6 + trl 0.23.1 (the Studio pin), 4.57.6 + trl 0.25.1, 4.57.6 + trl 1.16.0 and 5.21.0 + trl 1.16.0. The first two also passed on the earlier revision under 5.17.0 + trl 1.15.0.
  • py_compile on Python 3.9 and 3.10.

Not covered

  • Run on Linux x86_64 with a spoofed CUDA device only. The change is pure Python with no OS or accelerator branches. On a host with no GPU, import unsloth takes the CPU init path, which does not import _utils.py and never imported openai on main either.
  • Not tested on Databricks itself; the broken install was simulated with a package that raises TypeError on import.

@danielhanchen
danielhanchen marked this pull request as ready for review October 28, 2026 13:25
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-28T13:27:22.100838Z 8f1be5d Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 8f1be5d8d6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 549b140 into main Oct 28, 2026
3 of 34 checks passed
@danielhanchen
danielhanchen deleted the perf/lazy-openai-check branch October 28, 2026 14:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant