Skip to content

fix: fallback to English for invalid Wikipedia language codes#474

Open
lennney wants to merge 2 commits into
deedy5:mainfrom
lennney:fix/wikipedia-invalid-lang-fallback
Open

fix: fallback to English for invalid Wikipedia language codes#474
lennney wants to merge 2 commits into
deedy5:mainfrom
lennney:fix/wikipedia-invalid-lang-fallback

Conversation

@lennney

@lennney lennney commented Jun 10, 2026

Copy link
Copy Markdown

Summary

Fixes #417

When region=wt-wt (DuckDuckGo worldwide sentinel), the Wikipedia engine extracts lang=wt which is not a valid ISO 639 language code. This causes a ConnectionError on every search because wt.wikipedia.org does not exist.

Changes

  • Added _INVALID_LANG_CODES frozenset containing wt
  • In build_payload(), when the extracted language code is in this set, fall back to en
  • Added debug logging for the fallback

Testing

Verified with unit tests and integration test:

  • wt-wt -> en (fallback works)
  • us-en -> en (normal region preserved)
  • cn-zh -> zh (non-English region preserved)
  • jp-ja -> ja (non-English region preserved)
  • DDGS().text(python, region=wt-wt) returns results without ConnectionError

lennney added 2 commits June 9, 2026 15:38
Adds `--json` flag to all search commands (text, images, videos, news,
books) and extract command. When used, results are output as formatted
JSON to stdout, making it easier for coding agents and scripts to parse.

Previously, the only way to get JSON output was via `-o` which writes
to a file. The new `--json` flag outputs directly to stdout without
requiring a file path.

Closes deedy5#432
When region='wt-wt' (DuckDuckGo's worldwide sentinel), the Wikipedia
engine extracted lang='wt' which is not a valid ISO 639 language code,
causing ConnectionError on https://wt.wikipedia.org.

Add _INVALID_LANG_CODES frozenset containing 'wt' and fall back to 'en'
when the extracted language code is in this set.

Fixes deedy5#417
Copilot AI review requested due to automatic review settings June 10, 2026 04:30

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR improves CLI output flexibility and hardens Wikipedia engine behavior for certain DuckDuckGo region codes.

Changes:

  • Add --json flag to multiple CLI commands to print results as JSON to stdout.
  • Add a Wikipedia language fallback to English when the derived language code is not valid (e.g., wt).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
ddgs/engines/wikipedia.py Adds a guard to map invalid DDG region-derived “lang” codes to en for Wikipedia API calls.
ddgs/cli.py Introduces a --json flag across several commands (and adjusts output branching) to emit JSON on stdout.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread ddgs/cli.py
Comment on lines +566 to +576
@click.option("-nc", "--no-color", is_flag=True, default=False, help="disable color output")
@click.option("--json", "as_json", is_flag=True, default=False, help="output results as JSON to stdout")
def extract(
url: str,
fmt: str,
output: str | None,
proxy: str | None,
*,
verify: bool,
no_color: bool,
as_json: bool,
Comment thread ddgs/cli.py
Comment on lines +264 to 267
if as_json:
click.echo(json.dumps(data, ensure_ascii=False, indent=2))
elif not output and not download:
_print_data(data, no_color=no_color)
Comment thread ddgs/cli.py
Comment on lines +377 to 380
if as_json:
click.echo(json.dumps(data, ensure_ascii=False, indent=2))
elif not output and not download:
_print_data(data, no_color=no_color)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wikipedia engine builds invalid URL for region="wt-wt" (wt.wikipedia.org does not exist)

2 participants