huggingface/smolagents · error · ImportError

You must install `wikipedia-api` to run this tool: for insta

Error message

You must install `wikipedia-api` to run this tool: for instance run `pip install wikipedia-api`

What it means

Raised by WikipediaSearchTool.__init__ when the optional `wikipedia-api` package is not installed. smolagents keeps it optional, and the tool fails at construction with an ImportError chained from the ModuleNotFoundError.

Source

Thrown at src/smolagents/default_tools.py:598

        "query": {
            "type": "string",
            "description": "The topic to search on Wikipedia.",
        }
    }
    output_type = "string"

    def __init__(
        self,
        user_agent: str = "Smolagents (myemail@example.com)",
        language: str = "en",
        content_type: str = "text",
        extract_format: str = "WIKI",
    ):
        super().__init__()
        try:
            import wikipediaapi
        except ImportError as e:
            raise ImportError(
                "You must install `wikipedia-api` to run this tool: for instance run `pip install wikipedia-api`"
            ) from e
        if not user_agent:
            raise ValueError("User-agent is required. Provide a meaningful identifier for your project.")

        self.user_agent = user_agent
        self.language = language
        self.content_type = content_type

        # Map string format to wikipediaapi.ExtractFormat
        extract_format_map = {
            "WIKI": wikipediaapi.ExtractFormat.WIKI,
            "HTML": wikipediaapi.ExtractFormat.HTML,
        }

        if extract_format not in extract_format_map:
            raise ValueError("Invalid extract_format. Choose between 'WIKI' or 'HTML'.")

View on GitHub (pinned to 30bb116109)

Solutions

  1. pip install wikipedia-api.
  2. Add it to requirements/pyproject so environments are reproducible.
  3. Confirm the package name: it is `wikipedia-api` on PyPI (imports as `wikipediaapi`), not `wikipedia`.

Example fix

# before
tool = WikipediaSearchTool()  # ImportError
# after
# pip install wikipedia-api
tool = WikipediaSearchTool()
Defensive patterns

Strategy: validation

Validate before calling

try:
    import wikipediaapi  # noqa
except ImportError:
    raise SystemExit('pip install wikipedia-api')

Prevention

When it happens

Trigger: Instantiating WikipediaSearchTool without `pip install wikipedia-api`. Any constructor call (default or custom args) hits the import check first.

Common situations: Fresh installs without extras, notebooks where smolagents was installed long before the tool was first used, CI environments with minimal dependency sets.

Understand the failure class

Background: "X is not installed. Please install it with pip install Y": missing optional dependency errors — ImportError/ValueError raised when a library's optional extra was never installed — this error's family across 22 libraries.

Related errors


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