# Sentence Transformers

> Compute, train, and rerank text embeddings in Python

- Category: Retrieval & Knowledge (RAG) / Embedding Models & Inference
- Language: Python
- Repository: https://github.com/huggingface/sentence-transformers
- Website: https://sbert.net
- Canonical page: https://ai-tldr.dev/tools/sentence-transformers/
- Updated: 2026-08-18

## In short

Sentence Transformers is a Python framework for computing text embeddings with sentence, reranker (cross-encoder), and sparse encoder models. You load a pretrained model, call encode() on your texts, and get back vectors you can compare with a similarity function. It sits in the Embedding Models & Inference category of the Retrieval & Knowledge (RAG) tools and is open-source, written in Python.

## Overview

Sentence Transformers is a Python framework for computing text embeddings with sentence, reranker (cross-encoder), and sparse encoder models. You load a pretrained model, call encode() on your texts, and get back vectors you can compare with a similarity function. It works with over 15,000 pretrained models on Hugging Face, including many from the MTEB leaderboard.

It is aimed at developers building semantic search, retrieval, and text-similarity features, as well as teams that want to train or finetune their own embedding and reranker models. The same library covers both the dense embeddings used to find candidate documents and the cross-encoder rerankers used to score those candidates more precisely.

Within the embeddings category, it is the standard high-level toolkit for turning text into vectors and reranking results. It also supports sparse encoder models (such as SPLADE) for keyword-style sparse representations, so you can mix dense, sparse, and reranking stages in one pipeline.

## What it does

- Compute dense text embeddings with a one-line model.encode() call that returns numpy arrays
- Score and rank query-passage pairs with CrossEncoder reranker models via predict() or rank()
- Generate sparse embeddings with SparseEncoder models like SPLADE, including sparsity stats
- Built-in similarity helpers via model.similarity() to compare embeddings
- Access to 15,000+ pretrained models on Hugging Face, covering 100+ languages
- Train or finetune your own embedding, reranker, and sparse encoder models

## Getting started

Install the package, then load a pretrained model and encode some text.

1. Install

   Install from PyPI. Python 3.10+, PyTorch 1.11.0+, and transformers v4.41.0+ are recommended.

```bash
pip install -U sentence-transformers
```

2. Compute embeddings

   Load a Sentence Transformer model and encode a list of sentences into vectors.

```python
from sentence_transformers import SentenceTransformer

model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")

sentences = [
    "The weather is lovely today.",
    "It's so sunny outside!",
    "He drove to the stadium.",
]
embeddings = model.encode(sentences)
print(embeddings.shape)
# => (3, 384)
```

3. Compare similarity

   Use the model's similarity helper to compare the embeddings.

```python
similarities = model.similarity(embeddings, embeddings)
print(similarities)
# tensor([[1.0000, 0.6660, 0.1046],
#         [0.6660, 1.0000, 0.1411],
#         [0.1046, 0.1411, 1.0000]])
```

4. Rerank with a Cross Encoder

   Load a reranker model and rank passages for a query without manual sorting.

```python
from sentence_transformers import CrossEncoder

model = CrossEncoder("cross-encoder/ms-marco-MiniLM-L6-v2")

query = "How many people live in Berlin?"
passages = [
    "Berlin had a population of 3,520,031 registered inhabitants in an area of 891.82 square kilometers.",
    "Berlin has a yearly total of about 135 million day visitors, making it one of the most-visited cities in the European Union.",
    "In 2013 around 600,000 Berliners were registered in one of the more than 2,300 sport and fitness clubs.",
]
ranks = model.rank(query, passages, return_documents=True)
```

## When to use it

- Build semantic search over a document collection by embedding the corpus and matching queries by similarity
- Rerank an initial set of retrieved candidates with a cross-encoder for more accurate top results in a RAG pipeline
- Measure semantic textual similarity or run paraphrase mining across large text sets
- Train or finetune a custom embedding or reranker model for a domain-specific dataset

## Version history

- 2026-08-18 v6.0: Sentence Transformers v6.0 adds MultiVectorEncoder, a ColBERT-style late-interaction model type keeping one vector per token; built with LightOn, it loads PyLate and Stanford ColBERT checkpoints.
