Skip to content

The Wikipedia tool accepts lowercase language codes (including simple, be-tarask, and zh-min-nan) and a non-empty page title. The configured endpoint must use HTTPS on the selected language's exact <lang>.wikipedia.org host, without credentials or a nonstandard port. Page titles are URL-encoded as a single path segment. HTTP redirects are not followed; a redirected page request returns an error and may need to be retried with its canonical title.

src.tools.implementations.wikipedia_tool.WikipediaTool

Bases: BaseRESTTool

Source code in src/tools/implementations/wikipedia_tool.py
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
class WikipediaTool(BaseRESTTool):
    name = "wikipedia"
    # Includes legacy Wikipedia codes such as simple, be-tarask and zh-min-nan.
    LANGUAGE_PATTERN = r"[a-z]{2,12}(?:-[a-z0-9]{1,8}){0,2}"

    def __init__(self, config: Optional[Dict] = None):
        """
        Initialize the WikipediaTool with configuration options.
        """
        super().__init__(config=config)
        self.description = "Fetch a summary of a Wikipedia page for a given query."
        self.strict = False

        self.parameters = {
            "type": "object",
            "properties": {
                "query": {
                    "type": "string",
                    "description": (
                        "The Wikipedia page title or search query "
                        "to retrieve the summary for."
                    ),
                },
                "lang": {
                    "type": "string",
                    "pattern": "^" + self.LANGUAGE_PATTERN + "$",
                    "description": (
                        "The language code for Wikipedia (e.g., 'en', 'es', 'fr'). "
                        "Defaults to 'en'."
                    ),
                },
            },
            "required": ["query"],
            "additionalProperties": False,
        }

        # Set request settings
        self.content_type = "application/json"
        self.rate_limit = 30
        self.default_timeout = 10
        self.max_retries = 3
        self.retry_delay = 1.0

    async def execute(
        self, context: Optional[StreamContext] = None, **kwargs
    ) -> ToolResponse:
        """
        Execute the Wikipedia API call.

        Args:
            context (Optional[ContextModel]): Additional context for the request.
            **kwargs: Should include:
                - query (str): The Wikipedia page title or search query.
                - lang (str, optional): The language code (default is "en").

        Returns:
            ToolResponse: The tool's response with the Wikipedia summary.
        """
        query = kwargs.get("query")
        if not isinstance(query, str) or not query.strip():
            raise ValueError("The 'query' parameter must be a non-empty string.")

        lang = kwargs.get("lang", "en")
        if not isinstance(lang, str) or not re.fullmatch(self.LANGUAGE_PATTERN, lang):
            raise ValueError("The 'lang' parameter must be a Wikipedia language code.")
        encoded_query = quote(query, safe="")
        endpoint_url = self.endpoint.format(
            **{"lang": lang, "encoded_query": encoded_query}
        )
        destination = urlsplit(endpoint_url)
        if (
            destination.scheme != "https"
            or destination.hostname != f"{lang}.wikipedia.org"
            or destination.port not in (None, 443)
            or destination.username is not None
            or destination.password is not None
        ):
            raise ValueError(
                "The Wikipedia endpoint must use HTTPS "
                "on the selected Wikipedia language host."
            )

        self.logger.info(
            f"Fetching Wikipedia summary for query: '{query}' in language: '{lang}'"
        )
        self.logger.debug(f"Endpoint URL: {endpoint_url}")
        self.logger.debug(f"Context: {context}")

        response = await self.make_request(
            method="GET",
            endpoint_url=endpoint_url,
            response_format=ResponseFormat.JSON,
            use_token=False,  # No token required for the Wikipedia API.
            # Redirect targets must not bypass destination validation.
            allow_redirects=False,
            additional_headers={
                "Accept": "application/json",
                "User-Agent": "WikipediaTool/1.0",
            },
        )

        tool_response = ToolResponse(
            result=self.parse_output(response),
            context=None,
        )
        return tool_response

    def parse_output(self, output: Any) -> str:
        """
        Parse and format the Wikipedia API response.

        Args:
            output (Any): Raw API response data.

        Returns:
            str: A formatted string containing the page title, summary, and a link to
                the full page.
        """
        try:
            if not isinstance(output, dict):
                return str(output)

            # Check if the API indicates a missing page or error
            if (
                output.get("type")
                == "https://mediawiki.org/wiki/HyperSwitch/errors/not_found"
            ):
                return "Error: The page for the given query was not found on Wikipedia."

            if "detail" in output:
                return f"Error: {output['detail']}"

            summary_data = {
                "title": output.get("title"),
                "summary": output.get("extract"),
                "page_url": output.get("content_urls", {})
                .get("desktop", {})
                .get("page"),
            }

            formatted_output = format_json_to_document(summary_data)
            formatted_output += self.get_tool_specific_instruction()
            return formatted_output

        except Exception as e:
            self.logger.error(f"Failed to parse Wikipedia data: {e}", exc_info=True)
            return "An error occurred while parsing the Wikipedia summary."

    def get_tool_specific_instruction(self) -> str:
        """
        Optional instructions to append to the output.

        Returns:
            str: Additional information.
        """
        return "\n\nFor more details, please visit the Wikipedia page."

__init__(config=None)

Initialize the WikipediaTool with configuration options.

Source code in src/tools/implementations/wikipedia_tool.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def __init__(self, config: Optional[Dict] = None):
    """
    Initialize the WikipediaTool with configuration options.
    """
    super().__init__(config=config)
    self.description = "Fetch a summary of a Wikipedia page for a given query."
    self.strict = False

    self.parameters = {
        "type": "object",
        "properties": {
            "query": {
                "type": "string",
                "description": (
                    "The Wikipedia page title or search query "
                    "to retrieve the summary for."
                ),
            },
            "lang": {
                "type": "string",
                "pattern": "^" + self.LANGUAGE_PATTERN + "$",
                "description": (
                    "The language code for Wikipedia (e.g., 'en', 'es', 'fr'). "
                    "Defaults to 'en'."
                ),
            },
        },
        "required": ["query"],
        "additionalProperties": False,
    }

    # Set request settings
    self.content_type = "application/json"
    self.rate_limit = 30
    self.default_timeout = 10
    self.max_retries = 3
    self.retry_delay = 1.0

execute(context=None, **kwargs) async

Execute the Wikipedia API call.

Parameters:

Name Type Description Default
context Optional[ContextModel]

Additional context for the request.

None
**kwargs

Should include: - query (str): The Wikipedia page title or search query. - lang (str, optional): The language code (default is "en").

{}

Returns:

Name Type Description
ToolResponse ToolResponse

The tool's response with the Wikipedia summary.

Source code in src/tools/implementations/wikipedia_tool.py
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
async def execute(
    self, context: Optional[StreamContext] = None, **kwargs
) -> ToolResponse:
    """
    Execute the Wikipedia API call.

    Args:
        context (Optional[ContextModel]): Additional context for the request.
        **kwargs: Should include:
            - query (str): The Wikipedia page title or search query.
            - lang (str, optional): The language code (default is "en").

    Returns:
        ToolResponse: The tool's response with the Wikipedia summary.
    """
    query = kwargs.get("query")
    if not isinstance(query, str) or not query.strip():
        raise ValueError("The 'query' parameter must be a non-empty string.")

    lang = kwargs.get("lang", "en")
    if not isinstance(lang, str) or not re.fullmatch(self.LANGUAGE_PATTERN, lang):
        raise ValueError("The 'lang' parameter must be a Wikipedia language code.")
    encoded_query = quote(query, safe="")
    endpoint_url = self.endpoint.format(
        **{"lang": lang, "encoded_query": encoded_query}
    )
    destination = urlsplit(endpoint_url)
    if (
        destination.scheme != "https"
        or destination.hostname != f"{lang}.wikipedia.org"
        or destination.port not in (None, 443)
        or destination.username is not None
        or destination.password is not None
    ):
        raise ValueError(
            "The Wikipedia endpoint must use HTTPS "
            "on the selected Wikipedia language host."
        )

    self.logger.info(
        f"Fetching Wikipedia summary for query: '{query}' in language: '{lang}'"
    )
    self.logger.debug(f"Endpoint URL: {endpoint_url}")
    self.logger.debug(f"Context: {context}")

    response = await self.make_request(
        method="GET",
        endpoint_url=endpoint_url,
        response_format=ResponseFormat.JSON,
        use_token=False,  # No token required for the Wikipedia API.
        # Redirect targets must not bypass destination validation.
        allow_redirects=False,
        additional_headers={
            "Accept": "application/json",
            "User-Agent": "WikipediaTool/1.0",
        },
    )

    tool_response = ToolResponse(
        result=self.parse_output(response),
        context=None,
    )
    return tool_response

get_tool_specific_instruction()

Optional instructions to append to the output.

Returns:

Name Type Description
str str

Additional information.

Source code in src/tools/implementations/wikipedia_tool.py
161
162
163
164
165
166
167
168
def get_tool_specific_instruction(self) -> str:
    """
    Optional instructions to append to the output.

    Returns:
        str: Additional information.
    """
    return "\n\nFor more details, please visit the Wikipedia page."

parse_output(output)

Parse and format the Wikipedia API response.

Parameters:

Name Type Description Default
output Any

Raw API response data.

required

Returns:

Name Type Description
str str

A formatted string containing the page title, summary, and a link to the full page.

Source code in src/tools/implementations/wikipedia_tool.py
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
def parse_output(self, output: Any) -> str:
    """
    Parse and format the Wikipedia API response.

    Args:
        output (Any): Raw API response data.

    Returns:
        str: A formatted string containing the page title, summary, and a link to
            the full page.
    """
    try:
        if not isinstance(output, dict):
            return str(output)

        # Check if the API indicates a missing page or error
        if (
            output.get("type")
            == "https://mediawiki.org/wiki/HyperSwitch/errors/not_found"
        ):
            return "Error: The page for the given query was not found on Wikipedia."

        if "detail" in output:
            return f"Error: {output['detail']}"

        summary_data = {
            "title": output.get("title"),
            "summary": output.get("extract"),
            "page_url": output.get("content_urls", {})
            .get("desktop", {})
            .get("page"),
        }

        formatted_output = format_json_to_document(summary_data)
        formatted_output += self.get_tool_specific_instruction()
        return formatted_output

    except Exception as e:
        self.logger.error(f"Failed to parse Wikipedia data: {e}", exc_info=True)
        return "An error occurred while parsing the Wikipedia summary."