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 showFix it fast
Most common cause, 1 of 2The 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.
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)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.
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.
python3 -c "import transformers; print(transformers.__version__)"
grep -n -E "from_pretrained\(|pipeline\(" app.pyThen find the line you see in this list and open its fix:
- 1The flag reached the model call but not the tokenizer, config or processor
The traceback ends in AutoTokenizer.from_pretrained, AutoConfig.from_pretrained or AutoProcessor.from_pretrained, while the model line already has trust_remote_code=True - 2The script runs unattended and the flag was left unset
Running 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.
grep -n "from_pretrained(" app.py | grep -v trust_remote_codeFix
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.
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.
python3 app.pyConfirmed by raw.githubusercontent.com
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.
grep -n "trust_remote_code" app.pyFix
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.
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.
python3 app.py < /dev/nullConfirmed by raw.githubusercontent.com
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.