Repository navigation
feat: opt-in langdetect-rs backend for partition language detection - #4547
Conversation
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
left a comment
There was a problem hiding this comment.
@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.
|
@chrikrah Yes, an opt-in variable works, thanks. Pushed:
|
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
chrikrah
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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)
|
@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 On CI: agreed, I'll keep the dedicated Rust step on 3.12 only. The parity tests in |
Summary
Partition language detection can now use
langdetect-rsinstead oflangdetect. This is opt-in: it only happens whenUNSTRUCTURED_USE_LANGDETECT_RS=trueis set beforeunstructuredis imported andlangdetect-rsis installed. Without the variable,langdetectis used as before, whether or notlangdetect-rsis installed.langdetect-rsis a Rust port oflangdetect1.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. WithDetectorFactory.seed = 0, as set indetect_languages, it returned the same probabilities aslangdetecton 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) inENVConfig.partition/common/lang.pyreads it on first import. If it is set andlangdetect-rsis missing, a warning is logged andlangdetectis used; any other import error is raised.langdetectstays the required dependency.cleaners/translate.pyis unchanged.langdetect-rs==0.1.0is added to thetestgroup. Default CI jobs still runlangdetect.test_uniton 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.Why
Timed on
example-docs: best of 3 runs of the partitioner, macOS arm64, CPython 3.12, default arguments.detect_language_per_elementfake-email.emlexample-10k-1p.htmlREADME.mdlanguage-docs/eng_spa_mult.htmllanguage-docs/UDHR_first_article_all.txtexample-10k.htmlbook-war-and-peace-1225p.txtmetadata.languageswas 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:
partition_textcall in a fresh process takes 0.50–0.56 s withlangdetectand 0.32–0.33 s withlangdetect-rs, becauselangdetectparses 55 JSON profiles on first use.langdetectand about 19 MB withlangdetect-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 againstlangdetecton 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-rsTo compare the two backends directly: