Google USE (Universal Sentence Encoder) for spaCy





GitHub Stars



Last Commit

5mos ago









Spacy - Universal Sentence Encoder

Make use of Google's Universal Sentence Encoder directly within SpaCy. This library lets you embed Docs, Spans and Tokens from the Universal Sentence Encoder family available on TensorFlow Hub.

For using sentence-BERT in spaCy, see


There are many different reasons to not always use BERT. For example to have embeddings that are tuned specifically for another task (e.g. sentence similarity). See this very useful blog article:

The Universal Sentence Encoder is trained on different tasks which are more suited to identifying sentence similarity. Google AI blog paper

This library uses the user_hooks of spaCy to use an external model for the vectors, in this case a simple wrapper to the models available on TensorFlow Hub.


You can install this library from:

  • github: pip install git+
  • pyPI: pip install spacy-universal-sentence-encoder


  • python 3.6/3.7/3.8 (constraint from tensorflow)
  • tensorflow>=2.4.0,<3.0.0
  • spacy>=3.0.0,<4.0.0 (SpaCy v3 API changed a lot from v2)

To use the multilingual version of the models, you need to install the extra named multi with the command: pip install spacy-universal-sentence-encoder[multi]. This installs the dependency tensorflow-text that is required to run the multilingual models. Note that this library is still not available for Windows operating systems (

In alternative, you can install the following standalone pre-packaged models with pip. The same limitation for multilingual models applies (when trying to install a multilingual model on Windows, pip will say that no tensorflow-text is available). Each model can be installed independently:

model namesourcepip package
en_use_md install
en_use_lg install
xx_use_md install
xx_use_lg install

In addition, also CMLM models are now available:

model namesourcepip package
en_use_cmlm_md install
en_use_cmlm_lg install
xx_use_cmlm install
xx_use_cmlm_br install


Loading the model

If you installed the model standalone packages (see table above) you can use the usual spacy API to load this model:

import spacy
nlp = spacy.load('en_use_md')

Otherwise you need to load the model in the following way:

import spacy_universal_sentence_encoder
nlp = spacy_universal_sentence_encoder.load_model('xx_use_lg')

The third option is to load the model on your existing spaCy pipeline:

import spacy
# this is your nlp object that can be any spaCy model
nlp = spacy.load('en_core_web_sm')

# add the pipeline stage (will be mapped to the most adequate model from the table above, en_use_md)

In all of the three options, the first time that you load a certain Universal Sentence Encoder model, it will be downloaded from TF Hub (see section below to use an already downloaded model, or to change the location of the model files).

The last option (using nlp.add_pipe) can be customised with the following configurations:

  • use_model_url: allows to use a specific TFHub URL
  • preprocessor_url: for TFHub models that need specific preprocessing with another TFHub model (e.g., see documentation of CMLM models)
  • model_name: to load a specific model instead of mapping the current (language, size) to one of the options in the table above
  • enable_cache: default True, enables an internal cache to avoid embedding the same text (doc/span/token) twice. It makes the computation faster (when enough duplicates are embedded) but has a memory footprint because all the embeddings extracted are kept in the cache
  • debug: default False shows debugging information.

To use the configurations, when adding the pipe stage pass a dict as additional argument, for example:

nlp.add_pipe('universal_sentence_encoder', config={'enable_cache': False})

Use the embeddings

After adding to the pipeline, you can use the embedding models by using the various properties and methods of Docs, Spans and Tokens:

# load as before
import spacy
nlp = spacy.load('en_core_web_lg')

# get two documents
doc_1 = nlp('Hi there, how are you?')
doc_2 = nlp('Hello there, how are you doing today?')
# Inspect the shape of the Doc, Span and Token vectors
print(doc_1.vector.shape) # the full document representation
print(doc_1[3], doc_1[3].vector.shape) # the word "how"
print(doc_1[3:6], doc_1[3:6].vector.shape) # the span "how are you"

# or use the similarity method that is based on the vectors, on Doc, Span or Token

Common issues

Here you can find the most common issues with possible solutions.

Using a pre-downloaded model

If you want to use a model that you have already downloaded from TensorFlow Hub, belonging to the Universal Sentence Encoder family, you can use it by doing the following:

  • locate the full path of the folder where you have downloaded and extracted the model. Let's suppose the location is $HOME/tfhub_models
  • rename the folder of the extracted model (the one directly containing the folders variables and the file saved_model.pb) to the sha1 hash of the TFHub model source. The mapping URL / sha1 values is the following:
    • en_use_md: 063d866c06683311b44b4992fd46003be952409c
    • en_use_lg: c9fe785512ca4a1b179831acb18a0c6bfba603dd
    • xx_use_md: 26c892ffbc8d7b032f5a95f316e2841ed4f1608c
    • xx_use_lg: 97e68b633b7cf018904eb965602b92c9f3ad14c9
  • set the environment variable TFHUB_CACHE_DIR to the location containing the renamed folder, for example $HOME/tfhub_models (set it before trying to download the model: export TFHUB_CACHE_DIR=$HOME/tfhub_models)
  • Now load your model and it should see that it was already downloaded


To serialise and deserialise nlp objects, SpaCy does not restore user_hooks after deserialisation, so a call to from_bytes will result in not using the TensorFlow vectors, so the similarities won't be good. For this reason the suggested solution is:

  • serialise with bytes = doc.to_bytes() normally
  • deserialise with spacy_universal_sentence_encoder.doc_from_bytes(nlp, bytes) which will also restore the user hooks


This library, relying on TensorFlow, is not fork-safe. This means that if you are using this library inside multiple processes (e.g. with a multiprocessing.pool.Pool), your processes will deadlock. The solutions are:

  • use a thread-based environment (e.g. multiprocessing.pool.ThreadPool)
  • only use this library inside the created processes (first create the processes and then import and use the library)


To build and upload

# change version
# change version references everywhere
# update locally installed package
pip install -r requirements.txt
# build the standalone models (17)
# build the archive at dist/spacy_universal_sentence_encoder-${VERSION}.tar.gz
python sdist
# upload to pypi
twine upload dist/spacy_universal_sentence_encoder-${VERSION}.tar.gz
# upload language packages to github

Rate & Review

Great Documentation0
Easy to Use0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Unwelcoming Community0
No reviews found
Be the first to rate


No alternatives found


No tutorials found
Add a tutorial