Registry / ai-ml / fast-langdetect

fast-langdetect

JSON →
library1.0.1pypypi✓ verified 24d ago

fast-langdetect is an ultra-fast and highly accurate language detection library based on FastText, a library developed by Facebook. It offers 80x faster performance and up to 95% accuracy compared to conventional methods. The library supports Python versions 3.9 to 3.13 and works offline with a lightweight model, with continuous active development.

pip install fast-langdetect
INSTALL
IMPORT
SIG · FAST-LANGDETECT
F
fast-langdetect
ai-mlpythonv1.0.1
Install
2.5s avg
Import
554ms
Disk
23MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.0.1 · 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.580s · 27.1MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.5s · import 0.528s · 24MB
23MB installed
● package 23MB
Code
Verified usage

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

detect_language
from fast_langdetect import detect_language
Primary function for quick, direct language detection.
LangDetector
from fast_langdetect import LangDetector
Used to create custom detector instances for advanced configuration.
LangDetectConfig
from fast_langdetect import LangDetectConfig
Configuration class for setting parameters like cache directory or input length.

This quickstart demonstrates how to use `fast-langdetect` to detect the language of various text inputs. It shows usage of the default 'lite' model, explicitly selecting the 'full' model for higher accuracy, and using the 'auto' model with fallback behavior.

from fast_langdetect import detect_language text1 = "Hello, how are you?" text2 = "Bonjour, comment allez-vous?" text3 = "Este es un texto muy largo en español, con muchas palabras y frases para probar la detección de idioma." # Detect language with default settings (lite model) result1 = detect_language(text1) print(f"'{text1}' detected as: {result1.lang} (confidence: {result1.score:.2f})") # Detect language using the 'full' model for potentially higher accuracy result2 = detect_language(text2, model='full') print(f"'{text2}' detected as: {result2.lang} (confidence: {result2.score:.2f})") # Detect language with 'auto' model, which falls back to lite on MemoryError # Also request top 2 languages result3 = detect_language(text3, model='auto', k=2) print(f"'{text3}' detected top 2 as: {result3}")
Debug
Known issues
breakingThe configuration system was overhauled in v0.3.0, replacing environment variables (e.g., `FTLANG_CACHE`) with a dedicated `LangDetectConfig` class for explicit management. Existing code relying on environment variables for configuration will break.
fix
Migrate to using `LangDetectConfig` objects. For example, pass `config=LangDetectConfig(cache_dir='your/path')` to `LangDetector` or use `FTLANG_CACHE` environment variable before importing and initializing any components if not explicitly setting `cache_dir`.
affects: >=0.3.0
gotchaDetection accuracy can be reduced for text samples that are significantly shorter or longer than approximately 80 characters. Inputs are truncated to 80 characters by default.
fix
For longer texts, disable truncation via `LangDetectConfig(max_input_length=None)` if you understand the potential performance or accuracy implications. For short texts, be aware of inherent accuracy limitations.
affects: All
gotchaDifferent models have different memory footprints and accuracy. The 'lite' model is memory-friendly (~45-60 MB RSS) and works offline, while the 'full' model (~170-210 MB RSS) offers higher accuracy but consumes more memory. The `model='auto'` setting only falls back to the 'lite' model if a `MemoryError` occurs.
fix
Explicitly choose `model='lite'` for memory-constrained environments, `model='full'` for highest accuracy, or `model='auto'` with awareness of its specific fallback condition.
affects: All
gotchaThe `model='auto'` fallback mechanism is specific to `MemoryError` only. Other issues like `FileNotFoundError`, `PermissionError`, or network-related errors during model loading will raise standard Python exceptions and are not silently handled or fallen back.
fix
Implement standard Python error handling (try-except blocks) for potential I/O or network issues when using the library.
affects: All
gotchaAs of v0.4.0, newline characters in input text are always replaced with spaces internally to prevent errors with the underlying FastText model. This transformation is logged at a DEBUG level and happens silently by default.
fix
Be aware that newline characters are processed this way. If exact text formatting is critical for other parts of your pipeline, perform necessary pre-processing before passing text to `fast-langdetect`.
affects: >=0.4.0
gotchaThe pre-trained FastText language identification models bundled or downloaded by `fast-langdetect` are licensed under the Creative Commons Attribution-ShareAlike 3.0 (CC BY-SA 3.0) license. This is separate from the MIT license for the `fast-langdetect` code itself.
fix
If redistributing or modifying the model files, ensure compliance with the CC BY-SA 3.0 license terms, including attribution and share-alike conditions.
affects: All
Errors
Common errors & fixes
ImportError: cannot import name 'detect'
This error occurs when the 'detect' function cannot be imported, often due to naming conflicts, such as naming your script 'langdetect.py', which interferes with the module import.
fix
Rename your script to avoid conflicts, e.g., 'language_detection.py', and ensure the 'langdetect' module is installed and correctly imported.
LangDetectException: No features in text
This error arises when the input text lacks sufficient features for language detection, such as being too short or containing only non-alphabetic characters.
fix
Ensure the input text contains enough alphabetic characters to allow for accurate language detection.
ModuleNotFoundError: No module named 'langdetect'
This error indicates that the 'langdetect' module is not installed in your Python environment.
fix
Install the 'langdetect' module using pip: 'pip install langdetect'.
AttributeError: module 'fast_langdetect' has no attribute 'ft_detect'
This error occurs when trying to call a function or attribute named 'ft_detect' which does not exist in the current version of the `fast_langdetect` library, possibly due to an outdated code snippet or an attempt to use a non-existent internal function.
fix
Use the public `detect` function directly, which is the primary entry point for language detection. For example: `from fast_langdetect import detect; detect('your text')`.
ModuleNotFoundError: No module named 'fast_langdetect'
This error indicates that the `fast-langdetect` library is not installed in your Python environment or the Python interpreter cannot find it.
fix
Install the library using pip: `pip install fast-langdetect`.
Upgrade
Version history
1.0.1latest on PyPI · released May 6, 2026
Audit
Dependencies
robust-downloaderrequiredHandles model downloads and caching.
requestsrequiredUsed for HTTP requests, likely by robust-downloader.
fasttext-predictrequiredThe underlying FastText model inference engine.
Agent activity
20 hits · last 30 days
node
18
OpenAI (training)
1
Resources