Skip to content

Fix broken wandb import crashing unsloth startup#4147

Merged
danielhanchen merged 2 commits into
mainfrom
fix-broken-wandb-import
Mar 3, 2026
Merged

Fix broken wandb import crashing unsloth startup#4147
danielhanchen merged 2 commits into
mainfrom
fix-broken-wandb-import

Conversation

@danielhanchen
Copy link
Copy Markdown
Member

Summary

  • Adds disable_broken_wandb() to import_fixes.py that proactively tests if wandb can import, and if not, patches is_wandb_available() to return False and sets WANDB_DISABLED=true
  • Wires it up in __init__.py alongside the existing disable_torchcodec_if_broken() call
  • Follows the same pattern already used for torchcodec, causal_conv1d, and vllm broken-package handling

Problem

When wandb is installed but broken (old wandb < 0.19.11 with protobuf >= 6.0), from unsloth import FastLanguageModel crashes with:

ImportError: cannot import name 'Imports' from 'wandb.proto.wandb_telemetry_pb2'

The import chain is:

  1. unsloth/__init__.py -> tokenizer_utils.py -> import trl.trainer.sft_trainer
  2. trl imports from transformers import is_wandb_available
  3. is_wandb_available() does import wandb with no try/except
  4. wandb's internal protobuf dispatch fails because old wandb has no v6/ proto stubs for protobuf 6.x

This commonly happens on Colab where wandb comes pre-installed but may be an older version, while protobuf gets pulled to v6+ by vllm/grpcio dependencies.

wandb is entirely optional -- notebooks use report_to="none" -- but a broken install kills the entire import.

Root cause

wandb/proto/wandb_telemetry_pb2.py dispatches to v3/, v4/, v5/, or v6/ proto files based on google.protobuf.__version__[0]. wandb versions before 0.19.11 do not ship a v6/ directory, so with protobuf >= 6.0 the dispatch falls through all branches and the Imports class is never defined. This was fixed upstream in wandb/wandb#9612.

Test plan

  • Healthy wandb: from unsloth import FastLanguageModel works, is_wandb_available() returns True
  • Broken wandb proto: fix triggers, logs upgrade message, unsloth imports successfully
  • No wandb installed: early return, no effect

danielhanchen and others added 2 commits March 3, 2026 15:05
When wandb is installed but broken (e.g., wandb < 0.19.11 with
protobuf >= 6.0), the import chain unsloth -> trl -> transformers ->
is_wandb_available() -> import wandb crashes with:

  ImportError: cannot import name 'Imports' from
  'wandb.proto.wandb_telemetry_pb2'

This happens because transformers' is_wandb_available() has no
try/except around `import wandb`. The error propagates up and kills
`from unsloth import FastLanguageModel` even though wandb is optional.

Add disable_broken_wandb() following the same pattern as
disable_torchcodec_if_broken(). It proactively tries importing wandb
during early init, and if the import fails, patches
is_wandb_available() to return False and sets WANDB_DISABLED=true.
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a robust mechanism to prevent unsloth from crashing due to problematic wandb installations. It detects if wandb is installed but fails to import, typically because of a protobuf version mismatch, and then gracefully disables wandb functionality within transformers and sets an environment variable. This ensures unsloth can initialize successfully even when wandb is in a broken state, improving overall stability and user experience, especially in environments like Colab.

Highlights

  • Wandb Import Fix: Implemented a new function disable_broken_wandb() to proactively check for and handle broken wandb installations, particularly those caused by protobuf version mismatches.
  • Integration: Integrated disable_broken_wandb() into the unsloth initialization process, mirroring existing patterns for handling other broken packages like torchcodec.
  • Problem Resolution: Prevents unsloth startup crashes that occur when wandb is installed but fails to import, ensuring the library remains functional even with problematic wandb setups.
Changelog
  • unsloth/init.py
    • Imported and invoked the new disable_broken_wandb function.
    • Cleaned up the disable_broken_wandb function from the module's namespace after execution.
  • unsloth/import_fixes.py
    • Added the disable_broken_wandb function to detect and mitigate wandb import failures by patching transformers.is_wandb_available() and setting WANDB_DISABLED.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a fix to prevent a startup crash caused by a broken wandb installation. The approach involves proactively checking if wandb can be imported and gracefully disabling it if the import fails. This is a solid defensive programming practice that improves the robustness of the library. The implementation is clean and consistent with existing patterns for handling similar issues with other optional dependencies. I have one suggestion to make the exception handling more specific, which will further improve the code's robustness.

Comment thread unsloth/import_fixes.py

try:
import wandb
except Exception:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using a broad except Exception: can catch unintended errors like KeyboardInterrupt and hide bugs. It's better to catch more specific exceptions that you expect to occur during a failed import of a library, especially one with native extensions. For consistency with other similar functions in this file, like disable_torchcodec_if_broken, consider catching (ImportError, RuntimeError, OSError).

Suggested change
except Exception:
except (ImportError, RuntimeError, OSError):

@danielhanchen danielhanchen merged commit 7be99ff into main Mar 3, 2026
4 checks passed
@danielhanchen danielhanchen deleted the fix-broken-wandb-import branch March 3, 2026 15:08
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 54f5bdc40e

ℹ️ 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".

Comment thread unsloth/__init__.py
patch_vllm_for_notebooks()
patch_torchcodec_audio_decoder()
disable_torchcodec_if_broken()
disable_broken_wandb()
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Invoke wandb guard before importing transformers

disable_broken_wandb() is executed after patch_enable_input_require_grads(), but that earlier patch unconditionally does from transformers import PreTrainedModel (unsloth/import_fixes.py), so on environments where importing transformers already touches wandb (the failure mode this commit targets), startup can still crash before the new guard runs. Moving the guard earlier in the init sequence is necessary to make the workaround effective for those installs.

Useful? React with 👍 / 👎.

danielhanchen added a commit that referenced this pull request Mar 3, 2026
)

trl/trainer/callbacks.py imports is_wandb_available from
accelerate.utils, not from transformers. The original fix in #4147
only patched the transformers version, so `from trl import GRPOTrainer`
still crashed via the callbacks.py -> accelerate -> wandb path.

Must patch both the source module (accelerate.utils.imports) AND the
re-export namespace (accelerate.utils) since Python's
`from accelerate.utils import X` reads from the latter, which holds
its own cached reference.
abiswas-realadvice pushed a commit to abiswas-realadvice/unsloth that referenced this pull request May 14, 2026
* Fix broken wandb import crashing unsloth startup

When wandb is installed but broken (e.g., wandb < 0.19.11 with
protobuf >= 6.0), the import chain unsloth -> trl -> transformers ->
is_wandb_available() -> import wandb crashes with:

  ImportError: cannot import name 'Imports' from
  'wandb.proto.wandb_telemetry_pb2'

This happens because transformers' is_wandb_available() has no
try/except around `import wandb`. The error propagates up and kills
`from unsloth import FastLanguageModel` even though wandb is optional.

Add disable_broken_wandb() following the same pattern as
disable_torchcodec_if_broken(). It proactively tries importing wandb
during early init, and if the import fails, patches
is_wandb_available() to return False and sets WANDB_DISABLED=true.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
abiswas-realadvice pushed a commit to abiswas-realadvice/unsloth that referenced this pull request May 14, 2026
…slothai#4148)

trl/trainer/callbacks.py imports is_wandb_available from
accelerate.utils, not from transformers. The original fix in unslothai#4147
only patched the transformers version, so `from trl import GRPOTrainer`
still crashed via the callbacks.py -> accelerate -> wandb path.

Must patch both the source module (accelerate.utils.imports) AND the
re-export namespace (accelerate.utils) since Python's
`from accelerate.utils import X` reads from the latter, which holds
its own cached reference.
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