Skip to content

Handle Transformers 5 vLLM import errors#3908

Merged
danielhanchen merged 3 commits into
mainfrom
fix-transformers5-hybridcache-vllm-error
Jan 20, 2026
Merged

Handle Transformers 5 vLLM import errors#3908
danielhanchen merged 3 commits into
mainfrom
fix-transformers5-hybridcache-vllm-error

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

  • guard vllm guided decoding patch to emit a clear error when Transformers 5 breaks vllm import
  • drop unused HybridCache imports in vision to avoid Transformers 5 ImportError

Testing

  • PYTHONPATH=. python - <<'PY'
    from unsloth import version
    print("unsloth", version)
    PY

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @danielhanchen, 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 significantly improves the compatibility and user experience of the unsloth library when interacting with vLLM and Transformers versions. It introduces specific error handling to gracefully manage ImportError exceptions that arise when vLLM encounters Transformers 5.x, guiding users to the correct transformers version. Additionally, it prunes unnecessary HybridCache imports to prevent similar import failures, ensuring smoother operation and clearer diagnostics for dependency conflicts.

Highlights

  • Enhanced vLLM Import Error Handling: Implemented robust try-except blocks around vLLM imports to catch ImportError exceptions specifically caused by Transformers 5.x incompatibility. This provides users with a clear RuntimeError message, guiding them to downgrade transformers to version 4.57.3.
  • Removed Unused HybridCache Imports: Cleaned up unsloth/models/vision.py by removing HybridCache from transformers import statements. This resolves ImportError issues that arose when Transformers 5.x was installed, as HybridCache is no longer available or compatible in newer versions.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

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.

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

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.

Code Review

This pull request addresses compatibility issues with Transformers v5 by adding error handling for vLLM imports and cleaning up unused imports.

In unsloth/import_fixes.py, you've added try-except blocks to catch ImportError when importing vllm, providing a clearer error message to users when an incompatible version of transformers is detected. This is a great improvement for user experience. However, there is significant code duplication in the error handling logic. I've suggested refactoring this into a helper function to improve maintainability.

The changes in unsloth/models/vision.py to remove the unused HybridCache import are clean and help prevent potential import errors with newer transformers versions.

Overall, good work on improving compatibility and error handling.

Comment thread unsloth/import_fixes.py Outdated
Comment on lines 256 to 302
try:
import vllm
except ImportError as e:
error_text = str(e)
if (
"ALLOWED_LAYER_TYPES" in error_text
or "transformers.configuration_utils" in error_text
):
try:
vllm_version = importlib_version("vllm")
except Exception:
vllm_version = "unknown"
raise RuntimeError(
"Unsloth: vLLM with version "
f"{vllm_version} does not yet support transformers>=5.0.0. "
"Please downgrade to transformers==4.57.3 via "
'pip install --force-reinstall "transformers==4.57.3". '
f"Original error: {e}"
) from e
raise

try:
from vllm.sampling_params import GuidedDecodingParams
except ImportError:
except ImportError as e:
error_text = str(e)
if (
"ALLOWED_LAYER_TYPES" in error_text
or "transformers.configuration_utils" in error_text
):
try:
vllm_version = importlib_version("vllm")
except Exception:
vllm_version = "unknown"
raise RuntimeError(
"Unsloth: vLLM with version "
f"{vllm_version} does not yet support transformers>=5.0.0. "
"Please downgrade to transformers==4.57.3 via "
'pip install --force-reinstall "transformers==4.57.3". '
f"Original error: {e}"
) from e
if not hasattr(vllm, "sampling_params") or not hasattr(
vllm.sampling_params, "StructuredOutputsParams"
):
raise
vllm.sampling_params.GuidedDecodingParams = (
vllm.sampling_params.StructuredOutputsParams
)

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

There's significant code duplication in the except blocks for handling ImportError. This duplicated logic can be extracted into a helper function to improve code clarity and maintainability. Defining a local helper function within fix_vllm_guided_decoding_params would be a good approach to keep the scope limited.

    def _handle_vllm_transformers_import_error(e):
        error_text = str(e)
        if (
            "ALLOWED_LAYER_TYPES" in error_text
            or "transformers.configuration_utils" in error_text
        ):
            try:
                vllm_version = importlib_version("vllm")
            except Exception:
                vllm_version = "unknown"
            raise RuntimeError(
                "Unsloth: vLLM with version "
                f"{vllm_version} does not yet support transformers>=5.0.0. "
                "Please downgrade to transformers==4.57.3 via "
                'pip install --force-reinstall "transformers==4.57.3". '
                f"Original error: {e}"
            ) from e

    try:
        import vllm
    except ImportError as e:
        _handle_vllm_transformers_import_error(e)
        raise

    try:
        from vllm.sampling_params import GuidedDecodingParams
    except ImportError as e:
        _handle_vllm_transformers_import_error(e)
        if not hasattr(vllm, "sampling_params") or not hasattr(
            vllm.sampling_params, "StructuredOutputsParams"
        ):
            raise
        vllm.sampling_params.GuidedDecodingParams = (
            vllm.sampling_params.StructuredOutputsParams
        )

@danielhanchen danielhanchen merged commit d59ee86 into main Jan 20, 2026
4 checks passed
@danielhanchen danielhanchen deleted the fix-transformers5-hybridcache-vllm-error branch January 20, 2026 09:02
abiswas-realadvice pushed a commit to abiswas-realadvice/unsloth that referenced this pull request May 14, 2026
* Handle Transformers 5 vLLM import errors

* Deduplicate vLLM transformers mismatch handling

---------

Co-authored-by: danielhanchen <danielhanchen@users.noreply.github.com>
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