langdetect-rs
A Rust port of langdetect that returns the same
results, down to the last bit of every probability. On WiLI-2018 and papluca test samples
it detects languages 40–52 times faster on one core and 245–315 times faster with all
eight cores of an M1 Pro
(benchmarks).
langdetect is pure Python and has not had a release since 2021, yet it is downloaded about
ten million times a month: document pipelines such as unstructured and the IFEval checks of
lm-eval, lighteval and inspect-evals depend on it. Its detection is a random walk over
character n-grams, so other detectors give different answers. langdetect-rs reproduces the
walk itself: the same Mersenne Twister, the same Gaussian smoothing, the same floating-point
operations in the same order. With a seed, every probability equals the original's; without
one, results vary from run to run as they do in langdetect.
The Python package is langdetect-rs; the Rust crate is langdetect. This is an independent
project, not affiliated with the authors of langdetect or language-detection.
Python
pip install langdetect-rs
Change the import and keep the rest of your code:
from langdetect_rs import DetectorFactory, detect, detect_langs # was: from langdetect import ...
DetectorFactory.seed = 0
detect("Ein, zwei, drei, vier")
# 'de'
detect_langs("Война и мир — роман Льва Толстого.")
# [ru:0.5714279654116534, bg:0.42857201882885804]
Detector (with append, set_alpha, set_prior_map, set_max_text_length),
DetectorFactory (with load_profile, load_json_profile, create, set_seed),
LangDetectException, ErrorCode, Language and utils.lang_profile.LangProfile work as in
langdetect 1.0.9, with the same results, exceptions and messages, also in its corner cases
(profiles loaded into a non-empty factory, priors of the wrong length, failed detections).
To detect many texts, use the batch functions. They run on all CPU cores and release the GIL;
a text without features gives None instead of raising:
from langdetect_rs import detect_batch
detect_batch(["Guten Morgen, wie geht es dir heute?", "Bonjour tout le monde", "1234"])
# ['de', 'fr', None]
Set RAYON_NUM_THREADS to limit the number of threads.
Code you do not control
When a library imports langdetect itself, call install() before importing that library:
import langdetect_rs
langdetect_rs.install() # `import langdetect` now returns langdetect_rs
from unstructured.partition.auto import partition
install() covers langdetect and its modules detector, detector_factory,
lang_detect_exception, language, utils.lang_profile and utils.ngram. Modules that
imported langdetect before the call keep the original.
Rust
[dependencies]
langdetect = "0.1"
use langdetect::{DetectorFactory, Seed};
assert_eq!(langdetect::detect("Ein, zwei, drei, vier")?, "de");
let mut factory = DetectorFactory::builtin().clone();
factory.seed = Seed::Int(0);
for lang in factory.detect_langs("Ceci est une phrase en français.")? {
println!("{lang}");
}
DetectorFactory is Send + Sync. Compat selects which Python behaviour to reproduce
(see below); the default is CPython 3.14.
Compatibility
The tests compare langdetect-rs with langdetect 1.0.9 and fail on any difference in any
probability: sentences and synthetic texts for all 55 languages, random Unicode including
lone surrogates, property-based tests, every detector option, custom and JSON profiles, prior
maps and error cases, on CPython 3.9 to 3.15. A separate script compares all 127,500 texts of
the WiLI-2018 and papluca test sets.
langdetect's own results depend on the Python it runs on, and langdetect-rs follows:
- From Python 3.12,
sum()uses compensated summation.langdetectnormalises probabilities withsum(), so its probabilities on WiLI-2018 and papluca differ between Python 3.11 and 3.12 for 38% of texts (in the last bits; the detected language did not change). str.isupper()follows the Unicode version of the Python, and decides when a run of capital letters is skipped. Tables for Unicode 13.0 to 17.0 (Python 3.9 to 3.15) are built in.langdetectloads its profiles inos.listdirorder, which depends on the file system and changes the last bits of a probability in about one text in 100,000. Iflangdetectis installed,langdetect-rsuses its order; otherwise, alphabetical order.
Factories and detectors can be pickled, also across processes, and batches stay usable in processes forked after a batch call.
Known differences:
- Without a seed,
langdetectseeds each detection with 2.5 KB fromos.urandom;langdetect-rstakes the seed from a per-thread generator that is seeded by the operating system and reseeded afterfork. Results are random either way. DetectorFactory.word_lang_prob_mapandDetector.randomdo not exist, andDetector.set_verbose()is accepted but prints nothing.- Lone surrogates are handled as the private-use characters U+10F800 to U+10FFFF. This only matters for custom profiles that contain one of these and texts that contain the other.
- Profile names must be strings, and
add_profileneeds a non-negativeindex. LangDetectExceptionis a separate class fromlangdetect's (afterinstall()they are the same).
Development
cargo test -p langdetect
cd bindings/python
uv sync --group dev
uv run pytest
cd benchmarks && uv run --group bench python parity.py # all 127,500 texts
License
Apache-2.0, the same licence as langdetect and language-detection, whose detection logic,
language profiles and normalisation tables this project ports. See NOTICE.
Metadata
Release files for langdetect-rs 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| langdetect_rs-0.1.0.tar.gz | 1.1 MB | Details |
Built distributions (wheels)
| File | Reset | |||
|---|---|---|---|---|
| langdetect_rs-0.1.0-cp39-abi3-win_amd64.whl | CPython 3.9 | abi3 | Windows x86-64 | Details |
| langdetect_rs-0.1.0-cp39-abi3-musllinux_1_2_x86_64.whl | CPython 3.9 | abi3 | Linux musl 1.2+ x86-64 | Details |
| langdetect_rs-0.1.0-cp39-abi3-musllinux_1_2_aarch64.whl | CPython 3.9 | abi3 | Linux musl 1.2+ ARM64 | Details |
| langdetect_rs-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl | CPython 3.9 | abi3 | Linux glibc 2.17+ x86-64 | Details |
| langdetect_rs-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl | CPython 3.9 | abi3 | Linux glibc 2.17+ ARM64 | Details |
| langdetect_rs-0.1.0-cp39-abi3-macosx_11_0_arm64.whl | CPython 3.9 | abi3 | macOS 11.0+ ARM64 | Details |
| langdetect_rs-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl | CPython 3.9 | abi3 | macOS 10.12+ x86-64 | Details |
Total release size: 13.4 MB
Release files / langdetect_rs-0.1.0.tar.gz
| Download URL | langdetect_rs-0.1.0.tar.gz |
|---|---|
| Size | 1.1 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
965c9a18c58868f7947e1a670753f8fcd7f78f665c7d01f15f607b98fbb7d208
|
|
BLAKE2b-256 checksum How to use checksums |
25114291720d53c301379dcc4d8befc85bde3c7fa5a3e3400d1b837b1de3ac80
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 11, 2026.
Transparency logRelease files / langdetect_rs-0.1.0-cp39-abi3-win_amd64.whl
| Download URL | langdetect_rs-0.1.0-cp39-abi3-win_amd64.whl |
|---|---|
| Size | 1.6 MB |
| Tags | CPython 3.9 Windows x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
aaec3223ae1aadd498eb1cad988f86916928b6b3b13740a0e843e3c8216f091f
|
|
BLAKE2b-256 checksum How to use checksums |
4894fc91c8b9dad525132510394b9e945f641c499e7f26533f696dd1ba5e6bb0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 11, 2026.
Transparency logRelease files / langdetect_rs-0.1.0-cp39-abi3-musllinux_1_2_x86_64.whl
| Download URL | langdetect_rs-0.1.0-cp39-abi3-musllinux_1_2_x86_64.whl |
|---|---|
| Size | 2.0 MB |
| Tags | CPython 3.9 Linux musl 1.2+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
25f9a29a49b4411b5325df22b3774063d7aa9ee16ea80ace68184f01d997585a
|
|
BLAKE2b-256 checksum How to use checksums |
fb7dace5fc32c8815db090dc1a6441bb5002e1f3724f2900b74ae3e5dc6d775f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 11, 2026.
Transparency logRelease files / langdetect_rs-0.1.0-cp39-abi3-musllinux_1_2_aarch64.whl
| Download URL | langdetect_rs-0.1.0-cp39-abi3-musllinux_1_2_aarch64.whl |
|---|---|
| Size | 1.9 MB |
| Tags | CPython 3.9 Linux musl 1.2+ ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
c7ceaa90f0c6b195b7344cbc1e56d9f08c39f2c5aa96eccab88a7b81fa11fa0c
|
|
BLAKE2b-256 checksum How to use checksums |
68593ab707864fbe081c373b236e272ecd580a169104bbca9d076207a4df6a11
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 11, 2026.
Transparency logRelease files / langdetect_rs-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | langdetect_rs-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 1.8 MB |
| Tags | CPython 3.9 Linux glibc 2.17+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
4b76f6b176ef5e6b8d2fd8a82d2b420c619ee76121200ce52a6edf2485cedfc8
|
|
BLAKE2b-256 checksum How to use checksums |
d075bced3d52dca8e8e57f5f2605095ce97683837bb8773d863bf4f4f7c9eaf7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 11, 2026.
Transparency logRelease files / langdetect_rs-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
| Download URL | langdetect_rs-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl |
|---|---|
| Size | 1.7 MB |
| Tags | CPython 3.9 Linux glibc 2.17+ ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
ffcd5443886e925537fee48518807a2f95b361ad3423995e309f381c423903ee
|
|
BLAKE2b-256 checksum How to use checksums |
8b0c077e9237c746284eeaeff43e29efa6a8320158da730b4bad0c4b248c1137
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 11, 2026.
Transparency logRelease files / langdetect_rs-0.1.0-cp39-abi3-macosx_11_0_arm64.whl
| Download URL | langdetect_rs-0.1.0-cp39-abi3-macosx_11_0_arm64.whl |
|---|---|
| Size | 1.7 MB |
| Tags | CPython 3.9 abi3 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
500d73771e3e5c16d80953940f3fed4ec79a81bc958440a33f7f03e83f50511b
|
|
BLAKE2b-256 checksum How to use checksums |
d882b333976c1e42861e38c0fdc1b8f042c931a25ebf061bda327d2c82ff0c4a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 11, 2026.
Transparency logRelease files / langdetect_rs-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl
| Download URL | langdetect_rs-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl |
|---|---|
| Size | 1.7 MB |
| Tags | CPython 3.9 abi3 macOS 10.12+ x86-64 |
|
SHA-256 checksum How to use checksums |
9c4ccdac6d4fe65f0a2aff7c361d7c8687ac3019719a4949b1329d00dbdb8ae2
|
|
BLAKE2b-256 checksum How to use checksums |
f7415351883ff1055ec8bd89214684dca36799e304d74acc0a7059344fe9ece8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 11, 2026.
Transparency log