Repository navigation
Import: check for a broken openai install on first use instead of at import unsloth - #15702
Merged
Merged
Conversation
…mporter [skip ci]
danielhanchen
marked this pull request as ready for review
October 28, 2026 13:25
Member
Author
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. More of your lovely PRs please. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
import unslothimports 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.pyranfrom openai import OpenAIonly so that a broken openai install could fliptransformers.utils.is_openai_availableto False. This PR does the same check on the first call totransformers.utils.is_openai_availableinstead 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.pyrunsif is_openai_available(): from openai import OpenAIat module level, andimport unslothimports that module while it patches the online DPO / XPO / Nash-MD trainers. trl 0.26 and later only import openai insideOpenAIPairwiseJudge.__init__, after the same check, and trl 1.15 / 1.16 have no judges at all.What changes
transformers.utils.is_openai_availableis 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 triesfrom openai import OpenAI, and if the import fails it prints the sameUnsloth: 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.transformers.utils.import_utils.is_openai_available,BACKENDS_MAPPING/requires_backends,is_serve_available(5.x) andcommands/serving.py(4.57.6) never saw the old patch either. Modules that readtransformers.utils.is_openai_availableafter unsloth (trl judges,trl env,transformers.testing_utils) get the wrapper, as they got the old replacement.except Exceptioninstead of a bareexcept:, 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:sys.modulesafterimport unsloth, transformers 4.57.6 + trl 1.15.0is_openai_available()afterimport unsloth(7 runs)import unslothmain-thread CPU, 4.57.6 + trl 1.15.0 (9 pairs)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 unslothanyway, 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 andOpenAIPairwiseJudge(...)(constructed when openai is healthy,ValueError: OpenAI client is not installedwhen 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 duringimport unsloth. With trl 0.25 or older that call still happens duringimport 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 examplefind_spec) cannot keep the old timing.Tests
tests/test_lazy_openai_import_check.py:import unslothmust not import it from_utils.pymodule level (fails on main);is_openai_available()returns False twice, the transformers original still returns True, the warning prints once, and the patch wraps exactly one layer;py_compileon Python 3.9 and 3.10.Not covered
import unslothtakes the CPU init path, which does not import_utils.pyand never imported openai on main either.TypeErroron import.