Skip to main content

langdetect-rs

CI PyPI Crates.io License

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.

Large corpora and dataframes

Pass a large corpus in chunks, so that only one chunk of texts and results is in memory at a time. With a seed, the results do not depend on the chunk size:

from itertools import islice

from langdetect_rs import DetectorFactory, detect_batch, detect_langs_batch

DetectorFactory.seed = 0


def in_chunks(batch, texts, size=10_000):
    texts = iter(texts)
    while chunk := list(islice(texts, size)):
        yield from batch(chunk)


df["lang"] = list(in_chunks(detect_batch, df["text"].fillna("")))
langs = list(in_chunks(detect_langs_batch, df["text"].fillna("")))
# e.g. [[de:0.9999959448286744], None, [fr:0.9999975839780009], ...]

Each text gets None where detect or detect_langs would raise LangDetectException for a text without features: empty, or only digits, punctuation, URLs and e-mail addresses. Other errors are raised for the whole chunk. The batch functions take only str, so missing values (None, NaN) have to be replaced first; "" gives None.

unstructured

Since 0.27.28, unstructured uses langdetect-rs when it is installed and UNSTRUCTURED_USE_LANGDETECT_RS=true is set before unstructured is imported:

pip install "unstructured>=0.27.28" langdetect-rs
export UNSTRUCTURED_USE_LANGDETECT_RS=true

Detected languages stay the same. If the variable is set but langdetect-rs is missing, unstructured logs a warning and uses langdetect.

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

import lm_eval

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 and free-threaded 3.14. A separate script compares all 127,500 texts of the WiLI-2018 and papluca test sets. Before a release, the built wheel and sdist are installed in clean environments without langdetect and compared with the results langdetect gives for 300 texts on the same machine.

langdetect's own results depend on the Python and the platform it runs on, and langdetect-rs follows:

  • From Python 3.12, sum() uses compensated summation. langdetect normalises probabilities with sum(), 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. On a Python with another Unicode version, langdetect-rs uses the Unicode 17.0 tables and issues a RuntimeWarning on import, as its results can then differ from langdetect's.
  • langdetect loads its profiles in os.listdir order, which depends on the file system and changes the last bits of a probability in about one text in 100,000. If langdetect is installed, langdetect-rs uses its order; otherwise, alphabetical order.
  • The random walk draws from random.gauss, which calls the platform's log and cos, so langdetect's probabilities differ in the last bits between macOS, glibc, musl and Windows (in 4 of the 300 texts of the release check). langdetect-rs calls the same functions and is bit-identical to langdetect on the same platform.

Factories and detectors can be pickled, also across processes, and batches stay usable in processes forked after a batch call.

On free-threaded CPython, importing langdetect_rs enables the GIL (CPython reports this with a RuntimeWarning). There are no free-threaded wheels, so pip builds one from the sdist, which needs a Rust toolchain.

Known differences:

  • Without a seed, langdetect seeds each detection with 2.5 KB from os.urandom; langdetect-rs takes the seed from a per-thread generator that is seeded by the operating system and reseeded after fork. Results are random either way.
  • DetectorFactory.word_lang_prob_map and Detector.random do not exist, and Detector.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_profile needs a non-negative index.
  • LangDetectException is a separate class from langdetect's (after install() 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

To check a build without langdetect, run python tests/make_reference.py reference.json where langdetect is installed, then install the wheel from uv build --wheel in a clean environment with the same Python on the same machine and run python tests/check_standalone.py reference.json there.

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.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for langdetect-rs 0.1.1
File Size Uploaded Provenance Actions
langdetect_rs-0.1.1.tar.gz 1.1 MB With provenance Details for langdetect_rs-0.1.1.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for langdetect-rs 0.1.1
File Actions
langdetect_rs-0.1.1-cp39-abi3-win_amd64.whl CPython 3.9 abi3 Windows x86-64 Details for langdetect_rs-0.1.1-cp39-abi3-win_amd64.whl
langdetect_rs-0.1.1-cp39-abi3-musllinux_1_2_x86_64.whl CPython 3.9 abi3 Linux musl 1.2+ x86-64 Details for langdetect_rs-0.1.1-cp39-abi3-musllinux_1_2_x86_64.whl
langdetect_rs-0.1.1-cp39-abi3-musllinux_1_2_aarch64.whl CPython 3.9 abi3 Linux musl 1.2+ ARM64 Details for langdetect_rs-0.1.1-cp39-abi3-musllinux_1_2_aarch64.whl
langdetect_rs-0.1.1-cp39-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl CPython 3.9 abi3 Linux glibc 2.17+ x86-64 Details for langdetect_rs-0.1.1-cp39-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
langdetect_rs-0.1.1-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl CPython 3.9 abi3 Linux glibc 2.17+ ARM64 Details for langdetect_rs-0.1.1-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
langdetect_rs-0.1.1-cp39-abi3-macosx_11_0_arm64.whl CPython 3.9 abi3 macOS 11.0+ ARM64 Details for langdetect_rs-0.1.1-cp39-abi3-macosx_11_0_arm64.whl
langdetect_rs-0.1.1-cp39-abi3-macosx_10_12_x86_64.whl CPython 3.9 abi3 macOS 10.12+ x86-64 Details for langdetect_rs-0.1.1-cp39-abi3-macosx_10_12_x86_64.whl

Total release size: 13.4 MB

Release files / langdetect_rs-0.1.1.tar.gz

Download URL langdetect_rs-0.1.1.tar.gz
Size 1.1 MB
Tags Source
SHA-256 checksum
How to use checksums
c12ab3e6dea24ef51b3167e711756b657b6af895fb0bd0ef4a602ea4d2fa7596
BLAKE2b-256 checksum
How to use checksums
e496fc64c83aeba939a5a9b7bb59408a353df08fa27e0746c19868eb4ed44bd4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 15, 2026.

Transparency log

Release files / langdetect_rs-0.1.1-cp39-abi3-win_amd64.whl

Download URL langdetect_rs-0.1.1-cp39-abi3-win_amd64.whl
Size 1.6 MB
Tags CPython 3.9 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
e5b5ab7ba9ae8cbda3aad0479c00979e12d9e3da57ab853c7f7f77c54384ee1a
BLAKE2b-256 checksum
How to use checksums
eb3d00adc9167b80301ff4410d5b5bcbd7f2df5cca3f5738ba1cde20e46fde01
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 15, 2026.

Transparency log

Release files / langdetect_rs-0.1.1-cp39-abi3-musllinux_1_2_x86_64.whl

Download URL langdetect_rs-0.1.1-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
8770871a102db68e156d5a870a06b9d3183fcaf5e8c03a80ead0510ee24a2251
BLAKE2b-256 checksum
How to use checksums
628de0b9bde05e8cfaec806d39dcce56efad0556a61c9267f1f3e788f9f0ae84
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 15, 2026.

Transparency log

Release files / langdetect_rs-0.1.1-cp39-abi3-musllinux_1_2_aarch64.whl

Download URL langdetect_rs-0.1.1-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
187275fc0538cf09be3247e09f3f4d5b43db12e632e2685743e67bc6a5e46f35
BLAKE2b-256 checksum
How to use checksums
07aa76aa4196113b2db329eeb5ce079de0e777a524b58925dc698aa65cc1ecfe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 15, 2026.

Transparency log

Release files / langdetect_rs-0.1.1-cp39-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL langdetect_rs-0.1.1-cp39-abi3-manylinux2014_x86_64.manylinux_2_17_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
66d81e855796b8aba9b20701c674aa672ceabac34f0f87dd010fcc31123eaa52
BLAKE2b-256 checksum
How to use checksums
8b2b60782be20b7906953b847bc372e16472df9c4e8e4d401931f70a811e24dd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 15, 2026.

Transparency log

Release files / langdetect_rs-0.1.1-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl

Download URL langdetect_rs-0.1.1-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
Size 1.7 MB
Tags CPython 3.9 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
ffa1c5cbd611c45a43a9ed8bf4176aa1e2f687d0b464e73346810820e789d64c
BLAKE2b-256 checksum
How to use checksums
fb9d07b0a488ed2ca0a9cc1024acf0237916d2f72cf5995217df6e895b933957
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 15, 2026.

Transparency log

Release files / langdetect_rs-0.1.1-cp39-abi3-macosx_11_0_arm64.whl

Download URL langdetect_rs-0.1.1-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
d2d8295a722757714a67d104855c598fa3084f385ea9fb98ed9fd200d4b41c73
BLAKE2b-256 checksum
How to use checksums
67b284b5151724e9c10341f29fe76fd858efd6d3f5e301faf31121e5b2799212
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 15, 2026.

Transparency log

Release files / langdetect_rs-0.1.1-cp39-abi3-macosx_10_12_x86_64.whl

Download URL langdetect_rs-0.1.1-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
479499c990ec605f6720977079b85eef5bb3268f7d52983ee652503c3bc6d2ad
BLAKE2b-256 checksum
How to use checksums
d5258e354c7dcf2aa1741773bd801e215719471589b82c971208c959d9fbacf9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 15, 2026.

Transparency log

Security for langdetect-rs 0.1.1

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.

Full provenance

All 8 files in this release have been uploaded with provenance metadata.

Select from 8 files

Attestation for langdetect_rs-0.1.1.tar.gz

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 Nov 15, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

8 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page