Skip to content

feat: opt-in langdetect-rs backend for partition language detection - #4547

Merged
cragwolfe merged 3 commits into
Unstructured-IO:mainfrom
kholdrex:kholdrex/langdetect-rs-backend
Oct 15, 2026
Merged

cragwolfe merged 3 commits into
Unstructured-IO:mainfrom
kholdrex:kholdrex/langdetect-rs-backend

Conversation

@kholdrex

@kholdrex kholdrex commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Partition language detection can now use langdetect-rs instead of langdetect. This is opt-in: it only happens when UNSTRUCTURED_USE_LANGDETECT_RS=true is set before unstructured is imported and langdetect-rs is installed. Without the variable, langdetect is used as before, whether or not langdetect-rs is installed. langdetect-rs is a Rust port of langdetect 1.0.9 with the same API. It reproduces the random walk exactly: the same Mersenne Twister, the same floating-point operations in the same order. With DetectorFactory.seed = 0, as set in detect_languages, it returned the same probabilities as langdetect on every input tested below.

Disclosure: I wrote langdetect-rs. It is Apache-2.0, has no Python dependencies, and ships abi3 wheels for Linux (glibc and musl, x86_64 and aarch64), macOS and Windows.

Changes:

  • UNSTRUCTURED_USE_LANGDETECT_RS (default off) in ENVConfig. partition/common/lang.py reads it on first import. If it is set and langdetect-rs is missing, a warning is logged and langdetect is used; any other import error is raised. langdetect stays the required dependency.
  • cleaners/translate.py is unchanged.
  • langdetect-rs==0.1.0 is added to the test group. Default CI jobs still run langdetect. test_unit on Python 3.12 gets one extra step, make test-langdetect-rs, which asserts the Rust backend is selected and reruns the language-metadata tests (lang, text, email, html, xml) with the variable set.
  • Tests: a parity test comparing languages and probabilities on three example documents, subprocess tests for backend selection (unset, enabled, enabled but missing), and a config parsing test.
  • CHANGELOG entry and version bump to 0.27.27.

Why

Timed on example-docs: best of 3 runs of the partitioner, macOS arm64, CPython 3.12, default arguments.

Document Elements detect_language_per_element langdetect langdetect-rs
fake-email.eml 4 False 5 ms 2 ms
example-10k-1p.html 23 False 9 ms 4 ms
README.md 10 True 20 ms 2 ms
language-docs/eng_spa_mult.html 5 True 16 ms 2 ms
language-docs/UDHR_first_article_all.txt 525 False 3.88 s 2.37 s
example-10k.html 314 True 2.16 s 1.53 s
book-war-and-peace-1225p.txt 11,724 True 95.6 s 75.2 s

metadata.languages was identical for every document in both modes. On large documents without per-element detection, the gain is small because detection is a small part of the total work.

There are also process-level savings:

  • First call: the first partition_text call in a fresh process takes 0.50–0.56 s with langdetect and 0.32–0.33 s with langdetect-rs, because langdetect parses 55 JSON profiles on first use.
  • Memory: after loading the profiles, RSS grows by about 67 MB with langdetect and about 19 MB with langdetect-rs. This is the memory that mem: load only 15 common langdetect profiles to reduce memory #4297 tried to reduce by loading fewer profiles. That PR was closed because it changed results; this change keeps all 55 profiles and the same results.

How identity is checked in langdetect-rs: its test suite compares full probability vectors against langdetect on CPython 3.9 to 3.15. Its CI runs all 127,500 texts of the WiLI-2018 and papluca test sets on Linux, macOS and Windows with no differences.

Testing

uv sync --group test
uv run pytest test_unstructured/partition/common/test_lang.py test_unstructured/partition/utils/test_config.py
make test-langdetect-rs

To compare the two backends directly:

import langdetect, langdetect_rs
for m in (langdetect, langdetect_rs):
    m.DetectorFactory.seed = 0
text = open("example-docs/language-docs/eng_spa_mult.txt").read()
assert [(x.lang, x.prob) for x in langdetect.detect_langs(text)] == \
       [(x.lang, x.prob) for x in langdetect_rs.detect_langs(text)]

View guided diff

langdetect-rs is a Rust port of langdetect with the same API and
bit-identical results, including the seeded detection behind
metadata.languages. When it is installed, partitioning uses it for
language detection; otherwise langdetect is used as before.

@chrikrah chrikrah left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@kholdrex I'd hold this until the Rust path is opt-in. The speedup is worth having, but as written it reaches users who never asked for it.

blocking: lang.py and translate.py import any installed langdetect_rs at runtime, with no version check and no way to turn it off. The >=0.1.0, <1.0.0 bound covers the test group only, and 0.1.0 is the sole release, published the day this PR opened. If a later 0.x drifts from langdetect, metadata.languages changes with nothing in the user's dependencies to explain it. That is the silent-misclassification risk #4297 was closed over, @KRRT7. I'd gate it behind an environment variable, since a version check would still switch anyone who gets the package transitively.

non-blocking: you put langdetect-rs in test so CI covers the Rust path. The cost is that nothing but the three-file parity test covers the langdetect path that pip install unstructured runs. ci.yml:131 and :169 sync --group test, and test_unit goes through base-cache/action.yml:21 with --all-groups, so every job gets the port. A separate group alone won't fix it. It needs --no-group in base-cache plus one job that installs it.

# not run: the suite with langdetect-rs installed. I didn't execute the wheel; I only listed its files. The CI claim rests on reading ci.yml and base-cache/action.yml at 89563ac.

Would an opt-in variable work for you here?

Picking up langdetect-rs just because it is installed means
metadata.languages could change without anything in the user's own
dependencies explaining it, for example after a transitive install or a
future langdetect-rs release. Partition language detection now uses
langdetect unless UNSTRUCTURED_USE_LANGDETECT_RS is set; if it is set
and the package is missing, a warning is logged and langdetect is used.

translate.py goes back to plain langdetect: its detect() call is
unseeded, so the parity tested for metadata.languages does not cover it.

With the gate, the default CI jobs exercise langdetect even though
langdetect-rs stays in the test group. A make test-langdetect-rs step on
Python 3.12 asserts the Rust backend is selected and reruns the
language-metadata tests with the variable set. The test dependency is
pinned to the tested 0.1.0 release.
@kholdrex kholdrex changed the title feat: use langdetect-rs for language detection when installed feat: opt-in langdetect-rs backend for partition language detection Oct 13, 2026
@kholdrex

Copy link
Copy Markdown
Contributor Author

@chrikrah Yes, an opt-in variable works, thanks. Pushed:

  • langdetect-rs is used only when UNSTRUCTURED_USE_LANGDETECT_RS=true is set before unstructured is imported. Without it, lang.py uses langdetect as before, even if langdetect-rs is installed. If the variable is set but the package is missing, it logs a warning and uses langdetect. Any other import error is raised.
  • Scope is now partition language detection only. translate.py is back to the original import langdetect.
  • On CI: with the gate, keeping langdetect-rs in test no longer switches the default jobs, so every existing unit job runs plain langdetect. test_unit on 3.12 gets one extra step, make test-langdetect-rs, which asserts the Rust backend is actually selected and then reruns the language-metadata tests (lang, text, email, html, xml) with the variable set. So I left base-cache and the groups as they are.
  • Pinned the test dependency to langdetect-rs==0.1.0 and reworded the CHANGELOG to say what was tested.
  • Added subprocess tests for the three selection cases (unset, enabled, enabled but missing), plus a config parsing test.

@socket-security

socket-security Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Addedpypi/​langdetect-rs@​0.1.0100100100100100

View full report

@chrikrah chrikrah left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The gate holds at c6f3a51: with UNSTRUCTURED_USE_LANGDETECT_RS unset, lang.detect_langs is langdetect.detect_langs, and a test now pins that default. @kholdrex, I'd approve.

Both findings from my review at 89563ac are resolved: lang.py imports langdetect_rs only behind the variable, translate.py is back on plain langdetect, and make test-langdetect-rs runs the Rust path as its own CI step instead of leaving it to the parity test.

Evidence at c6f3a51, Python 3.13.15, ruff 0.13.1:

$ PYTHONPATH=<tree> python -m pytest -q test_unstructured/partition/common/test_lang.py test_unstructured/partition/utils/test_config.py
74 passed in 4.68s
$ PYTHONPATH=<tree> python -m pytest -q <same two files>   # baseline origin/main e2f22b38
63 passed in 0.89s
$ # unstructured/partition/utils/config.py:358 default flipped False -> True, suite rerun
3 failed, 71 passed in 4.08s   # test_lang_uses_langdetect_by_default, test_use_langdetect_rs[None-False], test_use_langdetect_rs[-False]
$ UNSTRUCTURED_USE_LANGDETECT_RS=true python -c 'from unstructured.partition.common import lang; import langdetect_rs; print(lang.detect_langs.__module__, lang.detect_langs is langdetect_rs.detect_langs)'
langdetect_rs.detector_factory True
$ UNSTRUCTURED_USE_LANGDETECT_RS=true python -m pytest -q <test_lang, test_text, test_email, test_xml>
242 passed, 1 skipped in 15.46s
$ UNSTRUCTURED_USE_LANGDETECT_RS=true python -m pytest -q test_unstructured/partition/html/test_partition.py
108 passed in 6.71s
$ ruff check unstructured/partition/common/lang.py unstructured/partition/utils/config.py unstructured/cleaners/translate.py test_unstructured/partition/common/test_lang.py test_unstructured/partition/utils/test_config.py
All checks passed!
# not run: the full suite, and html/test_partition.py with the variable unset.

non-blocking: with the variable set, translate.py:41 still calls langdetect.detect, so translate_text keeps the Python backend while partition detection switches to Rust.

@kholdrex, I'd keep the Rust-path step on the 3.12 matrix entry only, since the wheel is abi3 and the backend does not vary by Python version, but say if you want it on every entry.

0.27.27 was released on main, so this change goes into the next dev version.

@cragwolfe cragwolfe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed c6f3a51 with GPT Pro. No actionable code defects found.

The opt-in gate preserves the Python backend by default. Missing-package fallback, propagation of other import errors, deterministic seeding, dependency declarations, and the checked-in tests for both backends are consistent with the intended behavior.

This approval covers the code reviewed. The PR still has merge conflicts. Exact-head CI, Security, CodeQL, Partition Benchmark, and Codeflash Optimization workflows show action_required with no jobs executed; only the two Socket checks passed. I did not run tests locally. Resolve the conflicts and obtain successful CI on the resulting head before merging.

(authored by codex)

@kholdrex

Copy link
Copy Markdown
Contributor Author

@cragwolfe thanks for the review. I merged main and moved the changelog entry to 0.27.28-dev0, since 0.27.27 has been released. The CI workflows will need approval to run on the new head.

@chrikrah thanks for checking it again.

On translate.py: keeping it on langdetect is intentional. I reverted that change to keep this PR scoped to partition language detection. translate_text calls detect() without setting a seed, and the seeded parity tests don't cover it, so an opt-in for translation should be a separate change with its own tests.

On CI: agreed, I'll keep the dedicated Rust step on 3.12 only. The parity tests in test_lang.py already run on every Python version in the regular matrix.

@kholdrex
kholdrex requested a review from cragwolfe October 15, 2026 02:34
@cragwolfe
cragwolfe merged commit 0308163 into Unstructured-IO:main Oct 15, 2026
53 checks passed
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.

3 participants