Skip to content

Fix crash when trl.experimental.openenv is unavailable#3787

Merged
danielhanchen merged 3 commits intounslothai:mainfrom
Fizza-Mukhtar:fix-trl-openenv-import
Dec 29, 2025
Merged

Fix crash when trl.experimental.openenv is unavailable#3787
danielhanchen merged 3 commits intounslothai:mainfrom
Fizza-Mukhtar:fix-trl-openenv-import

Conversation

@Fizza-Mukhtar
Copy link
Copy Markdown
Contributor

What this PR does

Makes usage of trl.experimental.openenv fully optional by guarding imports and downstream logic.

Why

Some TRL versions do not provide trl.experimental.openenv, causing DDP fine-tuning to fail at runtime.
This ensures Unsloth skips RL openenv patches safely when unavailable.

Result

  • Prevents crashes
  • Improves TRL version compatibility
  • Clear logging for users

Fixes #3722

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @Fizza-Mukhtar, 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 addresses a critical compatibility issue where Unsloth's DDP fine-tuning could crash if the trl.experimental.openenv module was not present in the installed trl library version. By introducing robust conditional checks and early exit mechanisms, the change ensures that Unsloth gracefully handles the absence of this module, preventing runtime failures and significantly improving the overall stability and compatibility with diverse trl environments.

Highlights

  • Optional trl.experimental.openenv usage: The pull request makes the usage of trl.experimental.openenv fully optional by guarding its imports and subsequent logic, preventing crashes when this module is unavailable.
  • Improved TRL version compatibility: This change enhances compatibility with various TRL versions, especially older ones that may not include trl.experimental.openenv, ensuring Unsloth functions correctly across different environments.
  • Clear logging for users: A new informative log message is added to explicitly notify users when trl.experimental.openenv is not available and its associated RL openenv patches are being skipped.

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

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 crash that occurs when trl.experimental.openenv is unavailable by making its usage optional. The changes guard the import and downstream logic, preventing runtime failures and improving compatibility with different TRL versions. I have one suggestion to simplify the implementation by removing an unnecessary flag, which will make the code more concise and Pythonic.

Comment on lines +953 to +965
openenv_available = False
try:
import trl.experimental.openenv.utils as openenv_utils
import trl.experimental.openenv as openenv

openenv_available = True
except ImportError as e:
logger.info(f"Unsloth: Failed to import trl openenv: {e}")

if not openenv_available:
logger.info(
"Unsloth: trl.experimental.openenv not available — skipping RL openenv patches."
)
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

The use of the openenv_available flag makes the code more verbose than necessary. You can simplify this logic by moving the second log message and the return statement into the except block. This avoids the need for the flag and the subsequent if check, making the code cleaner and more direct.

Suggested change
openenv_available = False
try:
import trl.experimental.openenv.utils as openenv_utils
import trl.experimental.openenv as openenv
openenv_available = True
except ImportError as e:
logger.info(f"Unsloth: Failed to import trl openenv: {e}")
if not openenv_available:
logger.info(
"Unsloth: trl.experimental.openenv not available — skipping RL openenv patches."
)
try:
import trl.experimental.openenv.utils as openenv_utils
import trl.experimental.openenv as openenv
except ImportError as e:
logger.info(f"Unsloth: Failed to import trl openenv: {e}")
logger.info(
"Unsloth: trl.experimental.openenv not available — skipping RL openenv patches."
)
return

@danielhanchen
Copy link
Copy Markdown
Contributor

Thanks @Fizza-Mukhtar !

@danielhanchen danielhanchen merged commit 87c60c4 into unslothai:main Dec 29, 2025
1 check passed
@Fizza-Mukhtar Fizza-Mukhtar deleted the fix-trl-openenv-import branch December 29, 2025 13:24
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.

[Bug] [MultiGPU/DDP] TypeError: expected str, bytes or os.PathLike object, not NoneType

2 participants