Skip to content

Fix regressions from security PRs #4042, #4044, and #4045 - #4062

Merged
danielhanchen merged 4 commits into
mainfrom
daniel/fix-security-regressions-4042-4045
Feb 16, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
daniel/fix-security-regressions-4042-4045

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Feb 15, 2026 •

Copy link
Copy Markdown
Member

Summary

This consolidates and corrects the security-related changes discussed in #4042, #4044, and #4045 while preserving runtime behavior.

What changed

  1. to_sharegpt formatter no longer uses exec and now uses a closure-based formatter.
  2. Optional [[...]] block gating now treats only None and empty string as missing, so values like 0 are preserved.
  3. to_sharegpt now validates batch length alignment between merged input and output columns before zipping.
  4. Conversation extension path no longer builds and executes dynamic code; it uses a direct Python combiner.
  5. GGUF equivalence subprocess now uses argument lists with shell=False and preserves apostrophes in prompts.
  6. fix_vllm_pdl_blackwell now skips workaround at vLLM >= 0.13.2.
  7. PDL workaround now also patches already-loaded vllm.lora.ops.triton_ops.* modules that expose supports_pdl.

Validation

Validated by executing both full notebooks that use to_sharegpt:

  • nb/Llama3_(8B)-Ollama.ipynb
  • nb/Kaggle-Llama3_(8B)-Ollama.ipynb

Both completed to_sharegpt conversion, training, inference, and GGUF export paths.

Notes

This PR is intended to supersede the behavior changes proposed in #4042, #4044, and #4045 with a single maintainable patch set.

@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 consolidates and corrects several security-related changes previously discussed in separate PRs. The primary goal is to enhance the security and stability of the chat template formatting and import fix mechanisms, ensuring runtime behavior is preserved while mitigating potential vulnerabilities. The changes include refactoring dynamic code execution to safer alternatives, improving data validation, and refining conditional workarounds for external libraries.

Highlights

  • Security Enhancements for Chat Template Formatting: The to_sharegpt formatter has been refactored to no longer use exec, instead employing a closure-based formatter for improved security and maintainability. Optional [[...]] blocks now correctly preserve 0 values, treating only None and empty strings as missing.
  • Robustness in Data Processing: Added validation to to_sharegpt to ensure batch length alignment between merged input and output columns, preventing potential data inconsistencies during conversion. Conversation extension logic now uses a direct Python combiner, eliminating dynamic code execution.
  • Improved Subprocess Execution: The GGUF equivalence subprocess now utilizes argument lists with shell=False for safer execution and correctly preserves apostrophes in prompts, addressing potential command injection vulnerabilities.
  • Refined vLLM PDL Workaround: The fix_vllm_pdl_blackwell workaround now correctly skips its application for vLLM versions 0.13.2 and above, and also patches already-loaded vllm.lora.ops.triton_ops.* modules that expose supports_pdl.
  • Comprehensive Regression Testing: New test files (test_chat_templates_security_regressions.py and test_import_fixes_security_regressions.py) have been added to cover the corrected behaviors and ensure no regressions are introduced.

🧠 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
  • tests/test_chat_templates_security_regressions.py
    • Added new test cases for optional prompt behavior, including preservation of zero values and validation of malformed blocks.
    • Included tests for the test_hf_gguf_equivalence subprocess invocation contract.
    • Added tests to verify the structure of to_sharegpt with conversation_extension=2.
  • tests/test_import_fixes_security_regressions.py
    • Added tests for patching known consumer modules.
    • Included tests for patching extra already-loaded consumer modules.
    • Added tests to validate the skip-path for fixed vLLM versions.
  • unsloth/chat_templates.py
    • Refactored _create_formatter to use a closure-based approach instead of exec for generating prompt formatters.
    • Modified optional [[...]] block handling to only consider None or empty strings as missing values, preserving 0.
    • Introduced validation to ensure optional [[...]] blocks contain at least one column reference.
    • Added batch length alignment validation in to_sharegpt for merged input and output columns.
    • Updated to_sharegpt's conversation extension logic to use a direct Python combiner, removing dynamic code generation.
    • Modified test_hf_gguf_equivalence to use subprocess.Popen with shell=False and an argument list, preserving apostrophes in prompts.
  • unsloth/import_fixes.py
    • Adjusted the fix_vllm_pdl_blackwell version check from > to >= to skip the workaround for vLLM versions 0.13.2 and above.
    • Introduced a _record_patch helper to prevent duplicate patching entries.
    • Implemented logic to patch already-loaded vllm.lora.ops.triton_ops.* modules that expose supports_pdl.
Activity
  • The pull request includes two new test files: tests/test_chat_templates_security_regressions.py and tests/test_import_fixes_security_regressions.py.
  • Validation was performed by executing these new test files, resulting in 7 passed tests.
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 provides a comprehensive set of fixes for regressions and security vulnerabilities. The removal of exec() and subprocess.Popen(shell=True) calls significantly improves security by eliminating potential code and command injection vectors. The new implementations using closures and argument lists are much safer and more readable. Additionally, the PR includes important correctness fixes, such as preserving 0 values in optional prompt blocks, validating batch lengths, and making the vLLM PDL workaround more robust. The new tests are thorough and provide excellent coverage for these changes. I have one minor suggestion for improving type checking style.

Comment thread unsloth/chat_templates.py
formatter_templates = []

for j, optional_prompt in enumerate(final_optional_prompts):
if type(optional_prompt) is str:

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

It's generally better to use isinstance() for type checking instead of type(). isinstance() is more robust as it correctly handles subclasses. This also aligns with Python's best practices (like PEP 8) and the style used in other parts of the codebase.

Suggested change
if type(optional_prompt) is str:
if isinstance(optional_prompt, str):

Comment thread unsloth/import_fixes.py Resolved
@danielhanchen
danielhanchen merged commit 5557283 into main Feb 16, 2026
2 checks passed
@danielhanchen
danielhanchen deleted the daniel/fix-security-regressions-4042-4045 branch February 16, 2026 07:16
This was referenced Feb 18, 2026
abiswas-realadvice pushed a commit to abiswas-realadvice/unsloth that referenced this pull request May 14, 2026
…nslothai#4045 (unslothai#4062)

* Fix security-regression fallout in chat templates and PDL patching

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

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

* Drop security regression test files from PR scope

* Apply suggestion from @danielhanchen

---------

Co-authored-by: Daniel Hanchen <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@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