ValueError
vllm.LLM.__init__.ValueError: trust_remote_code must be True for remote code models
Stack trace
Traceback (most recent call last):
File "app.py", line 10, in <module>
llm = LLM(model="huggyllama/llama-7b", trust_remote_code=False) # triggers error
File "/usr/local/lib/python3.9/site-packages/vllm/llm.py", line 45, in __init__
raise ValueError("trust_remote_code must be True for remote code models")
ValueError: trust_remote_code must be True for remote code models Why it happens
vLLM requires explicit confirmation to trust and execute remote code when loading certain models to prevent security risks. If trust_remote_code is not set to True for such models, it raises this error to avoid running untrusted code.
Detection
Check for ValueError exceptions during LLM initialization and verify if trust_remote_code is set to True when loading remote code models.
Causes & fixes
Loading a remote code model without setting trust_remote_code=True
Set trust_remote_code=True in the LLM constructor when initializing models that require remote code execution.
Using default trust_remote_code=False with a model that mandates remote code execution
Explicitly pass trust_remote_code=True to override the default and allow the model to load safely.
Code: broken vs fixed
from vllm import LLM
llm = LLM(model="huggyllama/llama-7b") # triggers ValueError about trust_remote_code from vllm import LLM
llm = LLM(model="huggyllama/llama-7b", trust_remote_code=True) # fixed by enabling trust_remote_code
print("Model loaded successfully") Workaround
Catch the ValueError and prompt the user or log a clear message instructing to set trust_remote_code=True before retrying model loading.
Prevention
Always review model documentation to know if trust_remote_code=True is required and set it proactively when initializing vLLM models that execute remote code.