Skip to content

[deprecation] Warn when loading local custom code without trust_remote_code#3807

Merged
tomaarsen merged 2 commits into
huggingface:mainfrom
tomaarsen:fix/require_trust_remote_code_warning
Jun 12, 2026
Merged

[deprecation] Warn when loading local custom code without trust_remote_code#3807
tomaarsen merged 2 commits into
huggingface:mainfrom
tomaarsen:fix/require_trust_remote_code_warning

Conversation

@tomaarsen

@tomaarsen tomaarsen commented Jun 9, 2026

Copy link
Copy Markdown
Member

Resolves #3801

Hello!

Pull Request overview

  • Emit a FutureWarning when a local model loads custom code without trust_remote_code=True
  • Note in the docstring that v6.0 will require trust_remote_code=True for local custom code, matching transformers

Details

Sentence Transformers currently treats any local model directory as implicitly trusted. import_module_class resolves repository-local custom classes through get_class_from_dynamic_module even when trust_remote_code=False, because of the or os.path.exists(model_name_or_path) short-circuit. That was a deliberate choice on my part (once the files are on disk, the code isn't really "remote" anymore), but it diverges from transformers, which requires trust_remote_code=True for local custom code as well, and it can surprise "download, then load" pipelines: snapshot_download doesn't execute any code, so loading the result with trust_remote_code=False looks safe but isn't. Reported in #3801.

For now this PR only adds a FutureWarning, emitted when custom code is actually loaded from a local path without trust_remote_code=True. It fires on the success path only, so it won't false-alarm when dynamic loading fails and we fall back to import_from_string (e.g. a class that resolves from an installed package). Loading by Hub id and built-in sentence_transformers.* modules are unaffected.

The behavior change itself is breaking (local custom-code models would suddenly require trust_remote_code=True), so it's deferred to v6.0, where the os.path.exists short-circuit will be removed to match transformers. There's a TODO(v6.0) in the code marking exactly that.

  • Tom Aarsen

Copilot AI 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.

Pull request overview

Adds a forward-looking deprecation warning to tighten the trust_remote_code security contract when loading repository-local custom code from disk, aligning Sentence Transformers with transformers behavior planned for v6.0.

Changes:

  • Emit a FutureWarning when dynamic custom-module loading succeeds from a local path while trust_remote_code=False (with a TODO marker for removing the local-path short-circuit in v6.0).
  • Document the deprecation directly in import_module_class’s docstring via a Sphinx .. deprecated:: 5.6 directive.
  • Add tests asserting the warning fires only for the local-path + trust_remote_code=False success path, and does not fire when trust_remote_code=True.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
sentence_transformers/util/misc.py Adds a FutureWarning on successful local dynamic-module loads without explicit trust; documents upcoming v6.0 behavior change.
tests/util/test_misc.py Adds regression tests to ensure the warning behavior is correct for trusted vs untrusted local loads.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@tomaarsen tomaarsen merged commit ae1acc3 into huggingface:main Jun 12, 2026
18 checks passed
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.

Remote Code Execution via trust_remote_code=False Bypass (Code Injection Vulnerability)

2 participants