> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Using Wikipedia Tool

> Guide to using LangChain's Wikipedia integration, showing configuration, API wrapper and tool usage, parameters, examples, and recommendations for retrieval augmented generation workflows.

In this lesson we review the Wikipedia integration in LangChain and show how to use the Wikipedia tool programmatically. The Wikipedia tool is useful for retrieving topical summaries, facts, and reference text that you can pass into an LLM or a retrieval-augmented generation (RAG) pipeline.

LangChain provides many integrations under the "Integrations" section of the docs: [https://docs.langchain.com](https://docs.langchain.com). Common examples include:

* Shell execution (e.g., [Bash](https://www.gnu.org/software/bash/))
* Web search providers ([Bing](https://www.bing.com), custom search APIs)
* ChatGPT plugins
* Image generation ([DALL·E](https://openai.com/dall-e))
* Cloud storage and document sources ([Google Drive](https://drive.google.com))
* Notification services ([Twilio](https://www.twilio.com))
* Knowledge sources ([Wikipedia](https://www.wikipedia.org), [YouTube](https://www.youtube.com), [Yahoo Finance](https://finance.yahoo.com))
* Human-in-the-loop tools (allowing workflows that prompt a human to act)

Quick integrations reference:

| Integration category |                          Use case | Example                                  |
| -------------------- | --------------------------------: | ---------------------------------------- |
| Shell / CLI          | Execute shell commands or scripts | `bash`                                   |
| Web search           |        Find and fetch web content | `Bing`, custom search APIs               |
| Image generation     |        Create images from prompts | [DALL·E](https://openai.com/dall-e)      |
| Cloud storage        |  Read documents from cloud drives | [Google Drive](https://drive.google.com) |
| Notifications        |             Send SMS/email alerts | [Twilio](https://www.twilio.com)         |
| Knowledge sources    |          Retrieve factual content | [Wikipedia](https://www.wikipedia.org)   |

All of the above are available as Python modules you can import and use directly. Below we demonstrate the Wikipedia tool and how to call it.

## Wikipedia tool: wrapper vs tool

The Wikipedia integration in LangChain is exposed in two layers:

* A utility wrapper (e.g., `WikipediaAPIWrapper`) that handles fetching pages and returning text.
* A tool wrapper (e.g., `WikipediaQueryRun`) that exposes a `run` interface used by agents or programmatic calls.

The wrapper supports configuration parameters such as `top_k_results` (how many search results to fetch) and `doc_content_chars_max` (limits the number of characters returned for each page). These let you balance coverage versus token usage when passing content to an LLM.

Parameter quick reference:

| Parameter               | Type    | Description                                 | Example |
| ----------------------- | ------- | ------------------------------------------- | ------- |
| `top_k_results`         | integer | Maximum number of search results to fetch   | `1`     |
| `doc_content_chars_max` | integer | Maximum characters to return from each page | `1000`  |

## Minimal example: import, configure, inspect, and call

Here is a concise example that demonstrates how to import, configure, inspect metadata, and call the Wikipedia tool:

```python theme={null}
from langchain_community.tools import WikipediaQueryRun
from langchain_community.utilities import WikipediaAPIWrapper

# Configure the underlying Wikipedia API wrapper
api_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=1000)

# Create the tool that uses the wrapper
tool = WikipediaQueryRun(api_wrapper=api_wrapper)
```

You can inspect the tool's metadata (name, description, and the expected arguments):

```python theme={null}
print(tool.name)
print(tool.description)
print(tool.args)
```

Expected console output:

```text theme={null}
wikipedia
A wrapper around Wikipedia. Useful for when you need to answer general questions about people, places, companies, facts, historical events, or other subjects. Input should be a search query.
{'query': {'title': 'Query', 'type': 'string'}}
```

To run the tool, call its `run` method with a dictionary whose key is the argument name (`"query"`) and whose value is the search string. For example, to fetch the top summary for "Neural Network":

```python theme={null}
result = tool.run({"query": "Neural Network"})
print(result)
```

The returned `result` contains the text retrieved from Wikipedia (bounded by `doc_content_chars_max`).

<Callout icon="lightbulb" color="#1CB2FE">
  The Wikipedia tool returns external content suitable for RAG workflows. Tune `top_k_results` and `doc_content_chars_max` to control coverage and token consumption. Use the retrieved text as context to an LLM or to populate a retrieval index.
</Callout>

<Callout icon="warning" color="#FF6B6B">
  The tool only retrieves content from Wikipedia; it does not call an LLM. Always validate returned facts and be mindful of freshness, attribution, and rate limits when using third-party content.
</Callout>

## Practical notes and recommended patterns

* Retrieval-only: The Wikipedia tool fetches content only. To generate answers or reason over content, pass the retrieved text into an LLM chain, prompt, or an agent that calls the LLM.
* RAG integration: Combine multiple sources (Wikipedia + other knowledge sources) and index them in a vector store for more robust retrieval.
* Tool composition: Wrap the Wikipedia tool invocation in functions or chaining mechanisms (e.g., LCEL or LangChain chains) and combine it with other tools (search, calculators, or user prompts) for multi-step agents.
* Rate limits & caching: Respect Wikipedia API rate limits and consider caching frequently fetched pages to reduce network load and latency.

## Links and references

* LangChain Integrations: [https://docs.langchain.com](https://docs.langchain.com)
* Wikipedia: [https://www.wikipedia.org](https://www.wikipedia.org)
* DALL·E: [https://openai.com/dall-e](https://openai.com/dall-e)
* Bash: [https://www.gnu.org/software/bash/](https://www.gnu.org/software/bash/)
* Twilio: [https://www.twilio.com](https://www.twilio.com)

This demonstrates the basic usage of the Wikipedia tool. The next section will cover combining multiple tools into an agent for more advanced workflows.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/langchain/module/06905b96-585d-4c9e-835a-d8fcaca76e2a/lesson/dd8b1038-f63d-4623-9cac-2378b7808082" />
</CardGroup>
