huggingface/smolagents · error · ValueError

Loading a tool from Hub requires to acknowledge you trust it

Error message

Loading a tool from Hub requires to acknowledge you trust its code: to do so, pass `trust_remote_code=True`.

What it means

Tool.from_hub downloads and executes arbitrary Python code from a Hugging Face Hub repo. As a safety gate, it refuses to run unless the caller explicitly passes trust_remote_code=True.

Source

Thrown at src/smolagents/tools.py:550

        </Tip>

        Args:
            repo_id (`str`):
                The name of the Space repo on the Hub where your tool is defined.
            token (`str`, *optional*):
                The token to identify you on hf.co. If unset, will use the token generated when running
                `huggingface-cli login` (stored in `~/.huggingface`).
            trust_remote_code(`str`, *optional*, defaults to False):
                This flags marks that you understand the risk of running remote code and that you trust this tool.
                If not setting this to True, loading the tool from Hub will fail.
            kwargs (additional keyword arguments, *optional*):
                Additional keyword arguments that will be split in two: all arguments relevant to the Hub (such as
                `cache_dir`, `revision`, `subfolder`) will be used when downloading the files for your tool, and the
                others will be passed along to its init.
        """
        if not trust_remote_code:
            raise ValueError(
                "Loading a tool from Hub requires to acknowledge you trust its code: to do so, pass `trust_remote_code=True`."
            )

        # Get the tool's tool.py file.
        tool_file = hf_hub_download(
            repo_id,
            "tool.py",
            token=token,
            repo_type="space",
            cache_dir=kwargs.get("cache_dir"),
            force_download=kwargs.get("force_download"),
            proxies=kwargs.get("proxies"),
            revision=kwargs.get("revision"),
            subfolder=kwargs.get("subfolder"),
            local_files_only=kwargs.get("local_files_only"),
        )

        tool_code = Path(tool_file).read_text()

View on GitHub (pinned to 30bb116109)

Solutions

  1. Pass trust_remote_code=True to load_tool / from_hub after reviewing the repo's tool.py code
  2. Only do this for repos you or a trusted party control; inspect the code in the repo first
  3. Prefer vendoring the tool code locally if trust is a concern

Example fix

# before
tool = load_tool("huggingface-tools/text-to-image")
# after
tool = load_tool("huggingface-tools/text-to-image", trust_remote_code=True)
Defensive patterns

Strategy: try-catch

Try / catch

try:
    tool = load_tool(repo_id)
except ValueError as e:
    if "trust_remote_code" in str(e):
        # review the repo's tool.py, then:
        tool = load_tool(repo_id, trust_remote_code=True)
    else:
        raise

Prevention

When it happens

Trigger: Calling load_tool(repo_id) or Tool.from_hub(repo_id) without trust_remote_code=True.

Common situations: Following older smolagents/transformers examples that predate the flag; CI scripts using load_tool without the flag after upgrading; forgetting the flag when switching from local tools to Hub tools.

Related errors


AI-assisted analysis of huggingface/smolagents@30bb116109 (2026-08-28). Data as JSON: /api/errors/86f28b1169ac8944. Report an issue: GitHub.