Registry / ai-ml / pycld2

pycld2

JSON →
library0.42pypypi✓ verified 25d ago

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 pycld2
INSTALL
IMPORT
SIG · PYCLD2
P
pycld2
ai-mlpythonv0.42
Install
2.0s avg
Import
Disk
31MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.42 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 34.6MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.0s · import 0.000s · 32MB
31MB installed
● package 31MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

detect
from pycld2 import detect
import pycld2 as cld2

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.

import pycld2 as cld2 # Example 1: Basic detection text_russian = "а неправильный формат идентификатора дн назад" isReliable, textBytesFound, details = cld2.detect(text_russian) print(f"Text: '{text_russian}'") print(f"Is reliable: {isReliable}") print(f"Detected language: {details[0][0]} ({details[0][1]})") print(f"Details: {details}") print('\n---\n') # Example 2: Detecting multiple languages and getting vectors text_mixed = """France is the largest country in Western Europe. A accès aux chiens et aux frontaux qui lui ont été il peut consulter. The quick brown fox jumped over the lazy dog.""" isReliable, textBytesFound, details, vectors = cld2.detect( text_mixed, returnVectors=True ) print(f"Text: '{text_mixed}'") print(f"Is reliable: {isReliable}") print(f"Detected language (summary): {details[0][0]} ({details[0][1]})") print(f"Segment language vectors: {vectors}")
Debug
Known issues
breakingInstallation commonly fails with 'Failed building wheel for pycld2' errors, particularly on non-standard architectures (e.g., ARM/aarch64) or Windows, due to missing C/C++ compilers or Python development headers.
fix
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.
affects: All versions
gotchaThe `detect()` function strictly requires UTF-8 encoded `bytes` or a `str` as input. Passing bytes encoded in other formats (e.g., Latin-1) will raise a `pycld2.error`.
fix
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')`).
affects: All versions
gotchaSetting the `debugScoreAsQuads` parameter to `True` in `detect()` can significantly impact performance, potentially causing a 2x performance hit.
fix
Only enable `debugScoreAsQuads=True` if you explicitly need this debugging feature, as it incurs a substantial CPU cost.
affects: All versions
deprecatedThe `hintEncoding` parameter in the `detect()` function is currently not working and provides no biasing hint to the detector.
fix
Avoid using `hintEncoding` as it has no effect. Focus on ensuring the input `utf8Bytes` is correctly encoded.
affects: All versions up to 0.42
Upgrade
Version history
0.42latest on PyPI · released Mar 28, 2025
Audit
Dependencies
C/C++ compiler (e.g., GCC, Clang, MSVC)requiredpycld2 compiles C++ code during installation. System build tools are required.
Python development headersrequiredRequired for compiling Python extensions. (e.g., python3-dev on Debian/Ubuntu)
Agent activity
17 hits · last 30 days
node
14
OpenAI (training)
1
Resources
pycld2 — pip install pycld2 · libregistry