Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
AI and LLM integration

Hugging Face Transformers still asks for trust_remote_code after you set it

Your error

The repository my-org/custom-model contains custom code which must be executed to correctly load the model. You can inspect the repository content at https://hf.co/my-org/custom-model .2 more lines it can show

Fix it fast

Most common cause, 1 of 2

The flag reached the model call but not the tokenizer, config or processor

Pass trust_remote_code=True to every loader that reads from that repository, or build a pipeline with the flag at the top level, which forwards it to the config, tokenizer and processor loaders.

Fix
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline

repo = "my-org/custom-model"
tok = AutoTokenizer.from_pretrained(repo, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(repo, trust_remote_code=True)

# or, in one call
gen = pipeline("text-generation", model=repo, trust_remote_code=True)
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

Transformers raises The repository contains custom code which must be executed when a loader that reads the repo's own Python code gets no trust_remote_code=True. Each from_pretrained call (config, tokenizer, processor, model) checks the flag separately, and unattended jobs cannot answer the 15-second prompt. Pass the flag to every loader or to pipeline(), and pin a revision. Applies to Transformers 5.x.

  • Applies to transformers 5.23.0, Python 3.11 to 3.14, Linux and Windows
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Read the traceback to see which Auto class raised the error, then list every from_pretrained and pipeline call in the script with its arguments.

Run this first
python3 -c "import transformers; print(transformers.__version__)"
grep -n -E "from_pretrained\(|pipeline\(" app.py

Then find the line you see in this list and open its fix:

  1. 1The flag reached the model call but not the tokenizer, config or processorThe traceback ends in AutoTokenizer.from_pretrained, AutoConfig.from_pretrained or AutoProcessor.from_pretrained, while the model line already has trust_remote_code=True
  2. 2The script runs unattended and the flag was left unsetRunning by hand stops at Do you wish to run the custom code? [y/N], while a job or container fails after about 15 seconds, or at once when it has no input

What does the error look like?

  • The repository my-org/custom-model contains custom code which must be executed to correctly load the model. You can inspect the repository content at https://hf.co/my-org/custom-model .
  • Please pass the argument `trust_remote_code=True` to allow custom code to be run.
  • Loading this model requires you to execute custom code contained in the model repository on your local machine. Please set the option `trust_remote_code=True` to permit loading of this model.

Cause 1 of 2

The flag reached the model call but not the tokenizer, config or processor

You seeThe traceback ends in AutoTokenizer.from_pretrained, AutoConfig.from_pretrained or AutoProcessor.from_pretrained, while the model line already has trust_remote_code=True

Check

Each Auto class reads trust_remote_code from its own keyword arguments and resolves it on its own; AutoTokenizer, for example, pops the value and passes it to resolve_trust_remote_code. Setting it on AutoModel does nothing for the tokenizer of a repository that ships its own tokenizer code.

Check
grep -n "from_pretrained(" app.py | grep -v trust_remote_code

Fix

Pass trust_remote_code=True to every loader that reads from that repository, or build a pipeline with the flag at the top level, which forwards it to the config, tokenizer and processor loaders.

Fix
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline

repo = "my-org/custom-model"
tok = AutoTokenizer.from_pretrained(repo, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(repo, trust_remote_code=True)

# or, in one call
gen = pipeline("text-generation", model=repo, trust_remote_code=True)

Verify

Every loader finishes without the custom code message, and a short generation runs.

Verify
python3 app.py

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 2

The script runs unattended and the flag was left unset

You seeRunning by hand stops at Do you wish to run the custom code? [y/N], while a job or container fails after about 15 seconds, or at once when it has no input

Check

When trust_remote_code is left as None for a repo with remote code, Transformers asks on standard input and sets a 15-second alarm. A job with nobody at the keyboard hits the timeout and gets the error. It fails straight away when standard input is closed, or on systems without SIGALRM, such as Windows.

Check
grep -n "trust_remote_code" app.py

Fix

Set the flag explicitly in code for repositories you have reviewed, and pin the commit you reviewed with revision so later pushes to the repo do not change what runs.

Fix
model = AutoModelForCausalLM.from_pretrained(
    "my-org/custom-model",
    trust_remote_code=True,
    revision="<commit-hash-you-reviewed>",
)

Verify

The job loads the model without waiting for input and without the error.

Verify
python3 app.py < /dev/null

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Wrap model loading in one helper that applies the same trust_remote_code and revision to config, tokenizer and model.
  • Read the repository's modeling and configuration files before enabling remote code, and record the reviewed commit hash next to the model name.
  • Run a smoke test with stdin closed in CI so any loader that would prompt fails there first.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: stegnor.com, Menulo, ShopGo.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.