Skip to content

Replace requests with urllib.request to reduce core dependencies - #1211

Merged
bact merged 6 commits into
devfrom
copilot/reduce-dependencies-in-pythainlp
Jan 17, 2026
Merged

Replace requests with urllib.request to reduce core dependencies#1211
bact merged 6 commits into
devfrom
copilot/reduce-dependencies-in-pythainlp

Conversation

Copilot AI commented Jan 16, 2026

Copy link
Copy Markdown
Contributor

What does this changes

Replaces the requests library with Python's standard library urllib.request for HTTP operations, reducing core dependencies from 2 to 1 (only tzdata for Windows remains).

What was wrong

PyThaiNLP had requests>=2.31 as a core dependency solely for corpus download functionality (get_corpus_db() and _download() in pythainlp/corpus/core.py). This contributed to:

  • Long dependency installation times (>1 hour per platform)
  • Increased risk of subdependency version conflicts
  • Larger installation footprint

How this fixes it

  • pythainlp/corpus/core.py: Replaced requests.get() with urllib.request.urlopen()
    • Created _ResponseWrapper class for API compatibility with existing .json() method calls
    • Maintained chunked streaming downloads using response.read(CHUNK_SIZE)
    • Updated exception handling from requests.exceptions.* to urllib.error.*
    • Added library-appropriate User-Agent string that identifies PyThaiNLP client with version and platform info (e.g., PyThaiNLP/5.2.0 (Python/3.12; linux))
    • Defined _USER_AGENT module-level constant used consistently across HTTP requests
  • pyproject.toml: Removed requests>=2.31 from core dependencies
  • CHANGELOG.md: Documented the change

No breaking changes—API surface remains identical.

Your checklist for this pull request

  • Passed code styles and structures
  • Passed code linting checks and unit test
Original prompt

This section details on the original issue you should resolve

<issue_title>Consider reduce dependencies</issue_title>
<issue_description>## Background

  • It is becoming more difficult to build the entire PyThaiNLP package since it has a long list of dependencies, and each of them may require conflicting versions of subdependencies.
  • Dependency installation alone can take longer than one hour for each platform, way unhealthy to get responses for development
  • Some dependencies also only support older/end-of-life versions of Python, making the situation more complicated. Some CI environments may not support these old Python versions. It is of security concerns too. See Python end of life information here https://devguide.python.org/versions/

Proposal

  • Use the standard library when it possible. Often times there are functionalities that good enough for the job.
  • For the features PyThaiNLP develops on our own, try to stick with a single library/framework for one particular area of purposes (for example, use either Tensorflow or PyTorch)
  • Before adding a new dependency, assess first what are the transitive dependencies
  • Lazy import when possible. (This does not help shortening the test time though)
  • Consider putting the dependencies as a separate section in the optional dependencies in pyproject.toml. Keep the dependencies for the standard installation minimal. (This does not help shortening the test time though)

See also

Notes

<agent_instructions>Try to reduce the number of dependencies gradually.

Note that the unittest.yml workflow only test core and compact sets of features.
They are not the full features available in the package. You need to do additional testing for those features that are not covered in unittest.yml when you adjust the depedencies.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@wannaphong - Unnecessary base dependencies? #1184 - Fix base dependencies #1185

@bact bact added the dependencies Pull requests that update a dependency file label Jan 16, 2026
@github-actions

Copy link
Copy Markdown

Hello @Copilot, thank you for submitting a PR! We will respond as soon as possible.

สวัสดี @Copilot ขอบคุณที่ส่ง PR เข้ามา เราจะตอบกลับให้เร็วที่สุดเท่าที่จะทำได้

Copilot AI and others added 2 commits January 16, 2026 17:59
Co-authored-by: bact <128572+bact@users.noreply.github.com>
…dling

Co-authored-by: bact <128572+bact@users.noreply.github.com>
Copilot AI changed the title [WIP] Reduce dependencies in PyThaiNLP package Replace requests with urllib.request to reduce core dependencies Jan 16, 2026
Copilot AI requested a review from bact January 16, 2026 18:04
Comment thread pythainlp/corpus/core.py Outdated
Co-authored-by: bact <128572+bact@users.noreply.github.com>
Copilot AI requested a review from bact January 16, 2026 18:09
@bact

bact commented Jan 16, 2026

Copy link
Copy Markdown
Member

@copilot considering our use case, what is the best user-agent string?

Co-authored-by: bact <128572+bact@users.noreply.github.com>

Copilot AI commented Jan 16, 2026

Copy link
Copy Markdown
Contributor Author

@copilot considering our use case, what is the best user-agent string?

For this use case (a Python library downloading corpus data from its own server), the best User-Agent identifies the actual client rather than masquerading as a browser. Updated in commit 9507e0d to use: PyThaiNLP/{version} (Python/{py_version}; {platform})

This approach provides:

  • Transparency in server logs
  • Better debugging and analytics
  • Follows conventions used by other Python libraries (pip, requests, etc.)
  • Example output: PyThaiNLP/5.2.0 (Python/3.12; linux)

@bact
bact marked this pull request as ready for review January 16, 2026 18:13
@bact
bact requested a review from wannaphong January 16, 2026 18:14
Removed duplicate entry for KeyError fix and updated the requests library replacement note.
@sonarqubecloud

Copy link
Copy Markdown

@bact
bact merged commit 11c8a9e into dev Jan 17, 2026
27 of 28 checks passed
@bact
bact deleted the copilot/reduce-dependencies-in-pythainlp branch January 17, 2026 10:21
@bact bact mentioned this pull request Jan 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dependencies Pull requests that update a dependency file

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants