How to Turn a Sentence into an Embedding with a Hugging Face Feature-Extraction Pipeline
An embedding is a list of numbers that stands for the meaning of a piece of text: texts that mean similar things get similar numbers. That makes embeddings the base of semantic search, clustering and finding duplicates. A Hugging Face feature-extraction pipeline gives you one vector per token, and one line of PyTorch averages them into one vector for the whole sentence. This guide does it with Transformers 5.26.0 on Python 3.14.
Syntax
from transformers import pipeline
extractor = pipeline("feature-extraction", model="<model>", revision="<commit>")
tokens = extractor("<text>", return_tensors=True)
sentence = tokens.mean(dim=1)
| Part | What it means |
|---|---|
"feature-extraction" | The task: return the model's internal numbers for the text, instead of a label |
model="<model>" | A model on the Hugging Face Hub, as <owner>/<name>; models trained for embeddings give the best results |
revision="<commit>" | The exact version of the model, so it can't change under you |
return_tensors=True | Return a PyTorch tensor instead of nested Python lists |
tokens | A tensor of shape [1, number of tokens, vector size]: one vector for each token |
tokens.mean(dim=1) | Averages the token vectors into one vector for the sentence, of shape [1, vector size] |
The pipeline's documentation says it "extracts the hidden states from the base transformer, which can be used as features in downstream tasks". A tensor is PyTorch's multi-dimensional array of numbers.
The Example
The sentence is "The cat sat on the mat." The model is sentence-transformers/all-MiniLM-L6-v2, a small model trained to produce embeddings. Its card says it "maps sentences & paragraphs to a 384 dimensional dense vector space". It's Apache-2.0 licensed.
Prerequisites
- uv. This guide uses uv 0.13.10. The project uses Python 3.14, which uv downloads if needed.
- About 91 MB for the model, downloaded on the first run.
Step 1: Create the Project
uv init --python 3.14 embeddings-demo
cd embeddings-demo
uv add transformers torch
That's a project with Transformers 5.26.0 and PyTorch 2.15.0.
Step 2: Write the Script
from transformers import pipeline
extractor = pipeline(
"feature-extraction",
model="sentence-transformers/all-MiniLM-L6-v2",
revision="1110a243fdf4706b3f48f1d95db1a4f5529b4d41",
)
tokens = extractor("The cat sat on the mat.", return_tensors=True)
print(tokens.shape)
sentence = tokens.mean(dim=1)
print(sentence.shape)

The script prints shapes rather than the numbers themselves: 384 numbers per token are too many to read, and the shape says what you got.
Step 3: Run It
uv run python embed.py
Warning: You are sending unauthenticated requests to the HF Hub. Please set a HF_TOKEN to enable higher rate limits and faster downloads.
config.json: 100%|█████████████████████████████████████████████████| 612/612 [00:00<00:00, 4.86MB/s]
model.safetensors: downloading bytes: ██████████████████████████████████████████| 85.0MB, 5.39MB/s
model.safetensors: reconstructing file: 100%|██████████████████████████| 90.9MB / 90.9MB, 7.40MB/s
Loading weights: 100%|█████████████████████████████████████████| 103/103 [00:00<00:00, 13227.19it/s]
tokenizer_config.json: 100%|███████████████████████████████████████| 350/350 [00:00<00:00, 1.67MB/s]
vocab.txt: 100%|█████████████████████████████████████████████████| 232k/232k [00:00<00:00, 3.19MB/s]
tokenizer.json: 100%|████████████████████████████████████████████| 466k/466k [00:00<00:00, 6.04MB/s]
special_tokens_map.json: 100%|██████████████████████████████████████| 112/112 [00:00<00:00, 557kB/s]
torch.Size([1, 9, 384])
torch.Size([1, 384])
![The terminal after uv run python embed.py: the model's files downloading, then torch.Size([1, 9, 384]) for the token vectors and torch.Size([1, 384]) for the sentence vector.](https://strdewiridedevblogsprod.blob.core.windows.net/images/blog/ai/hugging-face/transformers/2026/tf-embed-02-run.webp)
The first run downloaded the model's files. Then the two shapes:
[1, 9, 384]: one text, 9 tokens, 384 numbers per token. The sentence has 7 tokens of its own, "the", "cat", "sat", "on", "the", "mat" and ".", plus the two special tokens the model adds at the start and the end,[CLS]and[SEP].[1, 384]: after averaging over the tokens, one vector of 384 numbers for the whole sentence. That's its embedding.
dim=1 is the token dimension, the second one: the average runs over the 9 tokens and keeps the 384 positions apart. How to Average a Tensor Along a Dimension in PyTorch with mean() explains dim with a small tensor. The command works unchanged in PowerShell.
Why Average the Tokens
Each token's vector describes that token in its context, so a sentence of 9 tokens has 9 vectors. Most uses need one vector per text, whatever its length. Averaging them, called mean pooling, is the way this model was trained to be used: its card averages the token vectors too, leaving out padding tokens. With one sentence at a time there's no padding, so a plain mean gives the same result.
To use the embedding, compare it with other sentences' embeddings: How to Measure How Similar Two Sentences Are with Hugging Face Embeddings does that with cosine similarity.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Leaving out return_tensors=True | You get nested Python lists, which have no mean or shape | Pass return_tensors=True |
Averaging with mean(dim=2) | You get one number per token instead of one vector per sentence | Average over the tokens: dim=1 |
| Using any model's features as embeddings | Models not trained for embeddings can give similar vectors to sentences that mean different things | Choose a model made for sentence embeddings |
FAQ
What's the vector size of other models?
It depends on the model: 384 for this one, 768 for BERT-base. Most models list it in their config.json as hidden_size.
Can I store embeddings and compare them later?
Yes. Embeddings are plain numbers: store them in a file or a vector database, and compare new texts against them later, as long as every embedding comes from the same model.
Do I need the sentence-transformers library?
Not for this. The sentence-transformers library wraps the same steps, tokenizing, running the model and pooling, in one call. Transformers alone does it with the pipeline and one mean.
Conclusion
A feature-extraction pipeline with return_tensors=True turned "The cat sat on the mat." into a tensor of shape [1, 9, 384], one vector per token, and mean(dim=1) averaged it into a single 384-number embedding of shape [1, 384].
Additional Resources
- FeatureExtractionPipeline — the pipeline and its options.
- sentence-transformers/all-MiniLM-L6-v2 — the model's card, with its pooling code.
