Skip to content

Fix RuntimeError not caught when torchcodec fails to load#3987

Merged
danielhanchen merged 1 commit into
mainfrom
fix-torchcodec-runtime-error
Feb 5, 2026
Merged

Fix RuntimeError not caught when torchcodec fails to load#3987
danielhanchen merged 1 commit into
mainfrom
fix-torchcodec-runtime-error

Conversation

@danielhanchen
Copy link
Copy Markdown
Member

@danielhanchen danielhanchen commented Feb 5, 2026

Summary

  • Catch RuntimeError in patch_torchcodec_audio_decoder() exception handler
  • Add disable_torchcodec_if_broken() to disable torchcodec in transformers when FFmpeg is missing

Problem

When torchcodec is installed but FFmpeg shared libraries (libavutil.so) are not available:

  1. Import-time crash: patch_torchcodec_audio_decoder() tries to import from datasets which imports torchcodec, raising RuntimeError

  2. Runtime crash: transformers' is_torchcodec_available() returns True (package exists via find_spec), so load_audio() tries to use torchcodec which fails:

RuntimeError: Could not load libtorchcodec. Likely causes:
  1. FFmpeg is not properly installed in your environment...

Solution

  1. Add RuntimeError to the exception tuple in patch_torchcodec_audio_decoder()

  2. Add disable_torchcodec_if_broken() which:

    • Tests if torchcodec can actually load its native libraries
    • If not, patches transformers.utils.import_utils._torchcodec_available = False
    • This makes transformers fall back to librosa for audio loading

Related

Testing

Verified Unsloth imports and audio loading works on systems without FFmpeg installed.

When datasets library has torchcodec installed but FFmpeg libraries
are missing, torchcodec raises a RuntimeError during import. The
exception handler only caught ImportError and AttributeError, causing
the error to propagate and crash Unsloth imports in environments
like Colab where FFmpeg may not be installed.
@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 improves the stability of the system by preventing crashes caused by torchcodec failing to load its native libraries due to missing FFmpeg dependencies. Previously, such failures would propagate as a RuntimeError, but now the system gracefully handles this specific exception, ensuring smoother operation, especially in environments where FFmpeg might not be pre-installed.

Highlights

  • Error Handling Enhancement: The pull request extends the exception handling in patch_torchcodec_audio_decoder() to catch RuntimeError in addition to ImportError and AttributeError.
  • Robustness for torchcodec: This change provides a defensive mechanism for scenarios where torchcodec raises a RuntimeError due to missing FFmpeg dependencies, preventing application crashes.

🧠 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.

Changelog
  • unsloth/import_fixes.py
    • Modified the except clause in patch_torchcodec_audio_decoder to include RuntimeError alongside ImportError and AttributeError.
Activity
  • The pull request was initiated by danielhanchen to address a RuntimeError issue.
  • The author identified that torchcodec could raise a RuntimeError when FFmpeg shared libraries are unavailable, which was not being caught.
  • The proposed solution was to add RuntimeError to the exception handling tuple.
  • The author confirmed testing on systems without FFmpeg, verifying successful imports.
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.

@danielhanchen danielhanchen merged commit 5f9dfad into main Feb 5, 2026
3 of 4 checks passed
@danielhanchen danielhanchen deleted the fix-torchcodec-runtime-error branch February 5, 2026 14:35
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 correctly addresses a RuntimeError that occurs when torchcodec fails to load by adding it to the exceptions caught. The change is simple and effective. I have one suggestion to improve code clarity and maintainability.

Comment thread unsloth/import_fixes.py

_patch()
except (ImportError, AttributeError):
except (ImportError, AttributeError, RuntimeError):
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

For improved clarity and consistency with other exception handlers in this file (e.g., line 1045), consider adding a brief comment to the pass statement on the next line. This would explain why these exceptions are being caught and ignored, which is helpful for future maintainers. For example:

pass  # unsloth_zoo not installed or torchcodec failed to load

abiswas-realadvice pushed a commit to abiswas-realadvice/unsloth that referenced this pull request May 14, 2026
…3987)

When datasets library has torchcodec installed but FFmpeg libraries
are missing, torchcodec raises a RuntimeError during import. The
exception handler only caught ImportError and AttributeError, causing
the error to propagate and crash Unsloth imports in environments
like Colab where FFmpeg may not be installed.

Co-authored-by: Daniel Han <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