huggingface/smolagents · error · ValueError

Invalid extract_format. Choose between 'WIKI' or 'HTML'.

Error message

Invalid extract_format. Choose between 'WIKI' or 'HTML'.

What it means

Raised by WikipediaSearchTool.__init__ when extract_format is not 'WIKI' or 'HTML'. These two strings map to wikipediaapi.ExtractFormat members; any other value (including lowercase variants) is rejected before the Wikipedia client is built.

Source

Thrown at src/smolagents/default_tools.py:615

        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'.")

        self.extract_format = extract_format_map[extract_format]

        self.wiki = wikipediaapi.Wikipedia(
            user_agent=self.user_agent, language=self.language, extract_format=self.extract_format
        )

    def forward(self, query: str) -> str:
        try:
            page = self.wiki.page(query)

            if not page.exists():
                return f"No Wikipedia page found for '{query}'. Try a different query."

            title = page.title
            url = page.fullurl

            if self.content_type == "summary":

View on GitHub (pinned to 30bb116109)

Solutions

  1. Use exactly 'WIKI' or 'HTML' (uppercase).
  2. Validate/normalize user-supplied format strings (e.g. .upper()) before constructing the tool.

Example fix

# before
tool = WikipediaSearchTool(user_agent='x', extract_format='wiki')
# after
tool = WikipediaSearchTool(user_agent='x', extract_format='WIKI')
Defensive patterns

Strategy: type-guard

Validate before calling

fmt = (extract_format or 'WIKI').upper()
assert fmt in {'WIKI','HTML'}, "extract_format must be 'WIKI' or 'HTML'"

Type guard

def is_valid_extract_format(v: str) -> bool:
    return isinstance(v, str) and v.upper() in {'WIKI', 'HTML'}

Prevention

When it happens

Trigger: Passing extract_format='wiki' (lowercase), 'markdown', 'TEXT', or any other string to the constructor.

Common situations: Assuming case-insensitive values or other formats like markdown/plain text; passing through unvalidated user config.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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