Overview
PyLate is a Python library for working with ColBERT-style models, where queries and documents are compared token by token (late interaction) instead of being squeezed into a single embedding. It is built on top of Sentence Transformers and reuses its trainer and training arguments, so the workflow will feel familiar if you have used that library before.
It is aimed at developers and researchers who want to fine-tune their own retrieval or reranking models and run document retrieval over their data. You can start from most pre-trained language models (for example bert-base-uncased) and PyLate will turn them into ColBERT models, adding a linear projection layer when the base encoder is not already a ColBERT model.
Within the RAG and retrieval space, PyLate sits in the rerankers and hybrid-search area: late-interaction scoring tends to be more accurate than a plain single-vector search, which makes it useful as a stronger retriever or as a reranking step in a RAG pipeline.
What it does
- Builds ColBERT models from most pre-trained language models, adding a linear layer when the base encoder is not already ColBERT
- Built on Sentence Transformers, reusing SentenceTransformerTrainer and SentenceTransformerTrainingArguments
- Contrastive training with options like a tunable temperature and CachedContrastive (GradCache) to emulate larger batches without more memory
- Knowledge distillation training from a strong teacher model's scores for higher quality
- Single- and multi-GPU training, including gathering examples across devices for larger effective batch sizes
- Built-in evaluation with ColBERTTripletEvaluator for held-out triplet eval sets
Getting started
Install PyLate with pip, then load or train a ColBERT model. The example below trains a model with contrastive loss on MS MARCO triplets.
Install PyLate
Install the package from PyPI. For evaluation dependencies, install the eval extra instead.
pip install pylateLoad or create a ColBERT model
Pass any base model name; if it is not already a ColBERT model, PyLate adds a linear layer to the base encoder.
from pylate import models
model = models.ColBERT(model_name_or_path="bert-base-uncased")Train with contrastive loss
Use the Sentence Transformers trainer with PyLate's loss, evaluator, and ColBERT data collator on a triplet dataset.
import torch
from datasets import load_dataset
from sentence_transformers import (
SentenceTransformerTrainer,
SentenceTransformerTrainingArguments,
)
from pylate import evaluation, losses, models, utils
model = models.ColBERT(model_name_or_path="bert-base-uncased")
dataset = load_dataset("sentence-transformers/msmarco-bm25", "triplet", split="train")
splits = dataset.train_test_split(test_size=0.01)
train_dataset = splits["train"]
eval_dataset = splits["test"]
train_loss = losses.Contrastive(model=model)
dev_evaluator = evaluation.ColBERTTripletEvaluator(
anchors=eval_dataset["query"],
positives=eval_dataset["positive"],
negatives=eval_dataset["negative"],
)
args = SentenceTransformerTrainingArguments(
output_dir="output/contrastive-bert-base-uncased",
num_train_epochs=1,
per_device_train_batch_size=32,
per_device_eval_batch_size=32,
learning_rate=3e-6,
)
trainer = SentenceTransformerTrainer(
model=model,
args=args,
train_dataset=train_dataset,
eval_dataset=eval_dataset,
loss=train_loss,
evaluator=dev_evaluator,
data_collator=utils.ColBERTCollator(model.tokenize),
)
trainer.train()Commands and code are distilled from the project's own documentation — always check the official repo for the latest.
When to use it
- Fine-tune a ColBERT retrieval model on your own data using contrastive training or knowledge distillation
- Add a late-interaction reranking step to a RAG pipeline for more accurate query-document scoring
- Turn an existing pre-trained language model into a ColBERT model without writing the architecture by hand
- Train on single or multiple GPUs, using cached contrastive loss or cross-device gathering to scale batch size
How PyLate compares
PyLate alongside other open-source rerank, search & hybrid tools AI/TLDR tracks, ranked by GitHub stars.
| Tool | Stars | What it does |
|---|---|---|
| Elasticsearch | ★ 77.9k | Distributed search and analytics engine with a built-in vector database for dense/sparse embeddings and hybrid keyword-plus-semantic retrieval. |
| Meilisearch Cloud | ★ 59.3k | Managed cloud for the Meilisearch engine, combining fast full-text search with hybrid, semantic, and multimodal vector search. |
| Typesense Cloud | ★ 26.6k | Managed hosting for the Typesense search engine, offering typo-tolerant keyword search plus vector and semantic search via a simple API. |
| Tantivy | ★ 16.1k | A fast full-text search engine library in Rust that provides BM25 keyword search for the lexical half of hybrid retrieval. |
| FlagEmbedding | ★ 12.2k | BAAI's retrieval toolkit that provides the BGE embedding and cross-encoder reranker models used widely in RAG pipelines. |
| Vespa | ★ 7.1k | A search and serving engine that natively combines vector, keyword (BM25), and structured search with built-in ranking for large-scale retrieval. |
| RAGatouille | ★ 4k | A wrapper that makes it easy to train and use ColBERT late-interaction retrieval inside RAG pipelines. |
| PyLate | ★ 894 | Train and serve ColBERT late-interaction retrieval models on top of Sentence Transformers |
