fastText is an open-source library for learning word representations and building text classifiers. It was developed by Facebook AI Research, now part of Meta AI, and is designed to train efficiently on ordinary hardware. Its two main uses are creating word embeddings from unlabeled text and training supervised classifiers from labeled text.

This tutorial explains how fastText works, how to install its Python package, how to train word vectors and classifiers, and when its subword approach is useful.

What fastText is used for

fastText supports two core NLP tasks:

  • Word representation learning: train dense vectors with the skip-gram or CBOW model from an unlabeled UTF-8 text corpus.
  • Text classification: assign one or more labels to documents such as messages, reviews, support tickets, or language samples.

A defining feature of fastText word embeddings is the use of character n-grams. Instead of treating every word as a completely independent token, fastText represents a word using the word itself and pieces of its spelling. This can produce vectors for rare or previously unseen words when their character patterns are known.

How fastText differs from Word2Vec

AspectfastTextWord2Vec
Word representationUses a word and its character n-gramsUsually learns one vector per vocabulary word
Out-of-vocabulary wordsCan construct a vector from known subwordsUsually has no vector for an unseen word
Morphologically rich languagesOften useful because related word forms share subwordsRelated forms are normally separate tokens
Model size and training costMay use more features because of subwordsCan be simpler when only whole-word vectors are required

fastText is not automatically better for every dataset. Whole-word models may be sufficient when the vocabulary is stable, the corpus is large, and unknown words are uncommon. fastText is especially practical when spelling patterns, inflections, misspellings, or rare words matter.

Install fastText for Python

The official Python package can be installed from PyPI. Because the package includes native C++ code, a compatible compiler and build tools may be required on some systems.

</>
Copy
python -m pip install fasttext

Verify the installation by importing the module:

</>
Copy
import fasttext

print(fasttext.__file__)

On systems where installation fails, first install the platform’s C++ compiler toolchain, then retry the pip command inside a clean virtual environment.

Train fastText word embeddings in Python

For unsupervised training, prepare a UTF-8 text file in which sentences or documents are written as normal text. Tokenization and text normalization should be chosen consistently for the language and use case.

</>
Copy
fastText learns vectors from words and character subwords.
Subword information helps represent uncommon word forms.
Text normalization should match the intended application.

Train a skip-gram model:

</>
Copy
import fasttext

model = fasttext.train_unsupervised(
    "corpus.txt",
    model="skipgram",
    dim=100,
    epoch=5,
    minn=3,
    maxn=6
)

model.save_model("fasttext_skipgram.bin")

Use model="cbow" to train a CBOW model instead. Important parameters include vector dimension, training epochs, learning rate, minimum word count, and the minimum and maximum character n-gram lengths.

Inspect vectors and nearest words

</>
Copy
vector = model.get_word_vector("classification")
neighbors = model.get_nearest_neighbors("classification", k=5)

print(vector.shape)
for similarity, word in neighbors:
    print(similarity, word)

The binary model file preserves the information needed for subword-based inference. A plain text vector export contains rows of words and numeric vector values, but it does not provide every capability of the binary model.

Train a fastText text classification model

Supervised fastText training expects one training example per line. Labels normally use the __label__ prefix. The remaining text on the line is the document to classify.

</>
Copy
__label__positive the delivery was quick and the item works well
__label__negative the package arrived damaged
__label__positive setup was simple and the instructions were clear
__label__negative the application crashes during startup

Train and save a classifier:

</>
Copy
import fasttext

classifier = fasttext.train_supervised(
    input="reviews.train.txt",
    epoch=25,
    lr=0.5,
    wordNgrams=2,
    dim=100
)

classifier.save_model("reviews_classifier.bin")

Evaluate the fastText classifier

</>
Copy
number_of_examples, precision, recall = classifier.test("reviews.test.txt")

print("Examples:", number_of_examples)
print("Precision:", precision)
print("Recall:", recall)

The test file must use the same label format and preprocessing rules as the training file. Keep a separate validation or test split so that evaluation is performed on examples not used to fit the model.

Predict labels for new text

</>
Copy
labels, probabilities = classifier.predict(
    "the product is easy to configure",
    k=2
)

for label, probability in zip(labels, probabilities):
    print(label, probability)

The result contains labels and confidence scores. Confidence values are useful for ranking predictions, but they should not be treated as perfectly calibrated probabilities without separate validation.

Use a saved fastText model

</>
Copy
import fasttext

model = fasttext.load_model("reviews_classifier.bin")
print(model.predict("support resolved the issue quickly"))

Loading a saved binary model avoids retraining and is the normal approach for batch jobs, APIs, and local applications.

fastText command-line examples

The original project also provides a command-line interface when fastText is built from source. The following commands show the general workflow.

</>
Copy
./fasttext skipgram -input corpus.txt -output vectors
./fasttext supervised -input reviews.train.txt -output reviews_model
./fasttext test reviews_model.bin reviews.test.txt
./fasttext predict-prob reviews_model.bin reviews.test.txt 2

The first command creates word representations. The remaining commands train, evaluate, and use a supervised classifier.

Pre-trained fastText vectors and language identification

The fastText website provides pre-trained word vectors for many languages and publishes supervised language-identification models. Check the model page for the training data, license, supported labels, and expected input before using a downloaded model in production.

fastText preprocessing and training practices

  • Use UTF-8 files: confirm that training and test files use consistent encoding.
  • Keep preprocessing consistent: apply the same normalization, tokenization, and casing rules during training and prediction.
  • Inspect label balance: heavily imbalanced classes can make aggregate metrics misleading.
  • Use held-out evaluation data: do not report training-set performance as test performance.
  • Tune on validation data: compare settings such as epochs, learning rate, dimensions, word n-grams, and character n-grams.
  • Check domain coverage: a model trained on one type of text may perform poorly on another.
  • Measure resource use: test model size, loading time, and prediction latency on the deployment hardware.

Is fastText deprecated?

The original facebookresearch/fastText GitHub repository was archived on March 19, 2024 and is read-only. Archiving means that the repository is no longer accepting normal changes there; it does not by itself make existing models or installations stop working. The official website and Python package remain available, but new projects should evaluate maintenance status, operating-system compatibility, and long-term support requirements before adopting the library.

When fastText is a suitable NLP model

fastText remains useful for compact word embeddings, CPU-friendly text classification, language identification, and baselines that can be trained without a GPU. It is a practical choice when inference speed, small deployment environments, or subword handling matter more than deep contextual understanding.

It is less suitable when a task requires long-context reasoning, token-level generation, rich contextual embeddings, or state-of-the-art performance from transformer-based architectures. Model selection should be based on measured accuracy, latency, memory use, maintenance needs, and the available training data.

FastText FAQs

What is fastText used for?

fastText is used to train word embeddings and supervised text classifiers. Common applications include document categorization, sentiment classification, language identification, and representing rare or unseen words through character subwords.

Is fastText a language model?

fastText can learn word representations and includes CBOW and skip-gram training objectives, but it is not a modern generative large language model. Its supervised component predicts labels rather than generating long-form text.

Can fastText create a vector for an unknown word?

Yes, when the model was trained with character n-grams, it can construct a vector from the unknown word’s subwords. The quality depends on whether those character patterns were learned from the training corpus.

Is fastText better than Word2Vec?

Neither model is universally better. fastText is often preferable when rare words, spelling variants, or morphology matter. Word2Vec may be adequate when the vocabulary is stable and whole-word vectors meet the task requirements.

Does fastText require a GPU?

No. fastText is designed to train and run efficiently on standard CPU-based hardware. Actual training time depends on corpus size, parameters, and processor resources.

FastText tutorial QA checklist

  • Confirm every supervised training row begins with the intended __label__ value.
  • Verify training, validation, test, and prediction text use the same preprocessing rules.
  • Check that Python examples use the installed fasttext module and valid file paths.
  • Report precision and recall from held-out data, not from the training file.
  • State that the original GitHub repository is archived rather than claiming that all fastText usage is discontinued.
  • Review downloaded pre-trained model licenses and supported languages before redistribution or deployment.