fastText is an open-source library for efficient text classification and word representation learning, released by Facebook AI Research in 2016, that models each word as a bag of character n-grams and trains shallow linear models at very high speed. In speech & language AI, it is a standard baseline for language identification, topic labeling and similar tasks where throughput matters more than deep contextual understanding.
The library combines two ideas: subword embeddings, which extend the word2vec skip-gram and CBOW models, and a linear classifier trained on averaged embeddings. The result trains on billions of words in minutes on an ordinary multicore CPU and handles rare or unseen words better than word-level embedding methods.
How subword embeddings represent words
fastText represents each word as the sum of vectors for its character n-grams plus a vector for the whole word. The word is wrapped in boundary symbols, so “where” becomes <where> and yields trigrams such as <wh, whe, her, ere and re>. By default, n-grams of 3 to 6 characters are extracted.
Because n-grams are shared across words, related forms such as “connect”, “connected” and “connection” receive similar vectors without explicit stemming. The approach is especially effective for morphologically rich languages such as Finnish, Turkish or Czech, where a single lemma can appear in dozens of inflected forms.
To limit memory, n-grams are mapped into a fixed number of buckets with a hashing trick, typically 2 million buckets. Hash collisions are tolerated because they rarely harm accuracy in practice. The table below contrasts this design with word-level embeddings.
| Property | fastText | word2vec | GloVe |
|---|---|---|---|
| Basic unit | Character n-grams plus word | Whole word | Whole word |
| Out-of-vocabulary words | Vector built from n-grams | No vector | No vector |
| Training objective | Skip-gram or CBOW | Skip-gram or CBOW | Co-occurrence matrix factorization |
| Morphology awareness | Strong | None | None |
| Model size | Larger due to n-gram buckets | Moderate | Moderate |
How the supervised classifier works
The supervised mode of fastText is a shallow neural network with one hidden layer and no nonlinearity. Word and optional word n-gram embeddings are averaged into a document vector, which a linear layer maps to class scores. Despite its simplicity, this architecture matched many deep models on sentiment and topic benchmarks when it was published.
Speed comes from two techniques. Hierarchical softmax based on a Huffman tree reduces the cost of the output layer from linear to logarithmic in the number of classes, and word bigrams add local word order at little cost. Training labels use the prefix __label__, and a single example may carry several labels for multi-label classification.
The following table lists the hyperparameters that most influence quality and speed.
| Parameter | Common range | Main effect |
|---|---|---|
| dim | 10 to 300 | Embedding size; larger captures more nuance |
| wordNgrams | 1 to 3 | Adds local word order; 2 is a frequent choice |
| minn and maxn | 2 to 6 | Character n-gram length range |
| epoch | 5 to 50 | More passes for small datasets |
| lr | 0.1 to 1.0 | Learning rate; high values speed convergence |
| loss | softmax, hs, ns, ova | Output layer type and label handling |
The library also offers automatic hyperparameter tuning on a validation file and model quantization, which can shrink a classifier from hundreds of megabytes to a few megabytes with a small accuracy loss, making deployment on mobile devices practical.
Pretrained models and typical applications
The project published pretrained word vectors for 157 languages, trained on Common Crawl and Wikipedia, and a language identification model covering 176 languages. These resources made fastText a common entry point for multilingual work and for low-resource languages where large neural models are unavailable.
- Language identification in web crawling and data cleaning pipelines.
- Spam and toxicity filtering where millions of messages must be scored quickly.
- Intent recognition baselines for chatbots and customer support routing.
- Quality filtering of training data for large language models.
- Initial embeddings for downstream models in morphologically complex languages.
Choosing fastText versus transformer models
The decision between fastText and a transformer depends on latency, cost and the need for context. fastText ignores long-range word order, so it cannot resolve negation scope or sarcasm reliably, but it runs on a CPU in microseconds per document. The decision table summarizes common situations.
| Situation | Recommended choice | Reason |
|---|---|---|
| Billions of short texts to filter | fastText | Throughput and minimal hardware cost |
| Language identification | fastText | Accurate pretrained model, tiny footprint |
| Nuanced sentiment or negation | Transformer | Needs contextual understanding |
| Few labeled examples | Transformer with fine-tuning | Pretrained context transfers better |
| Edge device with strict memory | Quantized fastText | Model of a few megabytes |
A strong practice is to train fastText first as a baseline: if a transformer improves accuracy by only one or two points, the operational savings of the simpler model often outweigh the gain.
Limitations and common mistakes
The main limitation is that fastText produces static embeddings: a word such as “bank” receives one vector regardless of its meaning in context. Models such as ELMo and transformers address this with contextual representations.
- Skipping text normalization, such as lowercasing and punctuation separation, which fragments the vocabulary.
- Evaluating only precision at one on multi-label data, which hides poor recall.
- Using default epochs on tiny datasets, which leads to underfitting.
- Ignoring class imbalance, which biases the classifier toward frequent labels.
- Treating language identification scores as certain on very short strings of one or two words.
The simple explanation
fastText is a tool that helps computers sort text quickly. It looks at words and also at small pieces of words. So it can guess the meaning of a word it has never seen. For example, it can link “unhappiness” to “happy” because they share letters.
Think of a mail sorter who reads only a few key clues on each envelope. The sorter is not perfect, but very fast and cheap. That is the main trade-off of fastText: speed and low cost instead of deep understanding.
How it is used in practice
A data team preparing a multilingual web corpus for training a language model downloads several billion web pages. Before any other processing, they run the pretrained fastText language identification model on every paragraph. On a cluster of ordinary CPU servers, the model labels the text in a few hours and discards paragraphs scoring below 0.65 confidence.
Next, the team trains a supervised fastText classifier on about 100,000 paragraphs labeled as high or low quality. The classifier uses word bigrams and 100-dimensional vectors and trains in under ten minutes. Applied to the full corpus, it removes boilerplate and spam, and the cleaned dataset improves downstream model accuracy at a fraction of the cost of transformer-based filtering.