Skip to content

Intermittent ConnectTimeout when using wikipedia package through LangChain WikipediaLoader #353

Description

@abderrahimlaribi

I'm experiencing intermittent connection timeouts when using LangChain's WikipediaLoader, which internally uses the Python wikipedia package.

The same Wikimedia API endpoint works successfully when called directly with requests, but the request made by the wikipedia package sometimes times out.

Environment

OS: Windows 11
Python: 3.11.7
langchain: 1.0.3
langchain-community: 0.4.2
wikipedia: 1.4.0
requests: 2.34.2

Minimal reproduction

import wikipedia
wikipedia.set_lang("en")


results = wikipedia.search("Paestum", results=2)
print(results)


page = wikipedia.page("Capaccio Paestum")


print(page.title)
print(page.url)
print(page.summary[:500])

The search can succeed:

['Paestum', 'Capaccio Paestum']

and the page can also resolve:

Capaccio Paestum
https://en.wikipedia.org/wiki/Capaccio_Paestum

However, accessing page.summary can fail with:

ConnectTimeout: HTTPSConnectionPool(
    host='en.wikipedia.org',
    port=443
): Max retries exceeded with url:

/w/api.php?prop=extracts&explaintext=&exintro=&titles=Capaccio+Paestum&format=json&action=query

The same happens when using LangChain:

from langchain_community.document_loaders import WikipediaLoader


loader = WikipediaLoader(
    query="Paestum",
    load_max_docs=2
)


docs = loader.load()

The traceback shows that WikipediaLoader eventually calls:

langchain_community.utilities.wikipedia.WikipediaAPIWrapper
    -> wikipedia
    -> wikipedia.wikipedia._wiki_request()
    -> requests.get()
    -> en.wikipedia.org

Important observation

I can successfully call the Wikimedia API directly using requests.

For example:


import requests


url = "https://en.wikipedia.org/w/api.php"


params = {
    "action": "query",
    "format": "json",
    "prop": "extracts",
    "explaintext": True,
    "exintro": True,
    "titles": "Capaccio Paestum",
}


response = requests.get(url, params=params, timeout=10)


print(response.status_code)
print(response.text)

This returns:

200

and a valid JSON response containing the article extract.

Therefore, the problem does not appear to be that the Wikimedia API is completely unreachable from my machine.

Expected behavior

wikipedia.page(...).summary should either:

successfully retrieve the summary, or
raise a clear HTTP/network exception with a reasonable timeout.
Actual behavior

The request sometimes hangs until Windows reports:

TimeoutError: [WinError 10060]

which becomes:

requests.exceptions.ConnectTimeout

The request is being made to:

https://en.wikipedia.org/w/api.php

Additional observation

The problem is not limited to LangChain.

For example, this can fail independently of LangChain:

page = wikipedia.page("Capaccio Paestum")


print(page.title)
print(page.url)
print(page.summary)

The failure occurs when summary triggers another request to the MediaWiki API.

Question

Could this be related to how the wikipedia package performs its HTTP requests, redirects, headers, or connection handling?

Since the same API request succeeds with a direct requests.get() call, I'm trying to understand what differs between the two requests.

Is there a recommended way to configure the wikipedia package with:

a custom User-Agent
an explicit connection/read timeout
a custom requests.Session
or another HTTP configuration?

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions