pycld2 provides Python bindings to Google Chromium's Compact Language Detection library (CLD2). It supports detection for over 165 languages and aims to consolidate the C++ library and its bindings into a single installable Python package. Version 0.42 was released in March 2025, with an irregular release cadence.
pip install pycld2Verified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `pycld2.detect()` for basic language identification and for obtaining detailed language vectors from mixed-language text. The `detect` function returns a tuple containing reliability, bytes found, a list of detected languages with confidence scores, and optionally segment-level language vectors.
Ensure that your system has the necessary C/C++ build tools (e.g., GCC, Clang, MSVC) and Python development headers installed before attempting `pip install pycld2`. For Debian/Ubuntu, `sudo apt-get install build-essential python3-dev` is often required. For macOS, `xcode-select --install`. For Windows, install Visual Studio Build Tools.
Always ensure your input text is either a standard Python `str` (which `pycld2` will encode to UTF-8 internally) or `bytes` that have been explicitly encoded using UTF-8 (e.g., `my_text.encode('utf-8')`).Only enable `debugScoreAsQuads=True` if you explicitly need this debugging feature, as it incurs a substantial CPU cost.
Avoid using `hintEncoding` as it has no effect. Focus on ensuring the input `utf8Bytes` is correctly encoded.