Safeguard
AI Security•

What does trust_remote_code=True do when loading a model?

trust_remote_code=True lets a Transformers model run Python code from the model repository. Learn the risk, how to pin a revision, and when to say no.

Safeguard Security Team
3 min read

Setting trust_remote_code=True lets the Transformers library run Python code that ships inside a model repository, instead of only its own built-in model code. That is needed for custom architectures, but it means loading the model also runs the author's code on your machine.

Why a custom model needs it

The Transformers documentation explains that a custom model builds on the library's configuration and modeling classes, but the modeling code is not from Transformers. Because it is not, you must pass trust_remote_code=True to load it. Without the flag, the library will not run that code.

The documentation says to take extra precaution with custom models. It notes that the Hub scans every repository for malware, and still advises care to avoid inadvertently executing malicious code. A scan lowers risk but is not a guarantee.

Weights and code are different risks

RiskWhat it isMitigation
Weight file formatOlder PyTorch weights use pickle, which is documented as insecurePrefer safetensors files
Repository codePython in the repo runs when you set the flagRead it, pin the revision

The loading guide says the library loads safetensors weights when they are available, and calls them more secure and faster than pickle based weights. That helps with the first row only. The flag is about the second.

Pin a revision

The documentation gives a second layer of safety: load the custom model from a specific revision, so you do not pick up model code that has changed since you reviewed it. You copy the commit hash from the model's commit history and pass it as the revision.

model = AutoModelForImageClassification.from_pretrained(
    "org/model-name", trust_remote_code=True, revision="<full commit hash>"
)

Related reading: pin a Hugging Face model revision to a commit hash.

What to look for when you read the code

Open the modeling and configuration files and search for network calls, subprocess use, file writes outside the model folder, reads of environment variables, and any import you do not recognise. Check that the repository's requirements do not pull in unusual packages. A custom architecture should need numerical libraries, not shell access.

A decision guide

  1. Does the architecture exist in the library already? Then you do not need the flag, so leave it off.
  2. If you do need it, read the modeling and configuration files before the first load.
  3. Pin the commit you read.
  4. Run first loads in a container with no credentials and limited network.
  5. Re-read the diff before moving to a newer revision.

Quick check

Does a clean Hub scan mean the model code is safe?

No. The documentation still says to take care to avoid executing malicious code.

Does safetensors remove the need for care with remote code?

No. It addresses the weight file format. Repository code is a separate risk.

Safeguard's MCP Guardrail and policies are aimed at agent tool use, and its SAST code scanning can look at Python you vendor in. A free scan is open to try.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.