Contextual Document Embeddings

John Xavier Morris, Alexander M Rush

In the world of Natural Language Processing, we’ve long known that context is king. The transition from static word vectors (like Word2Vec) to contextualized ones (like BERT) revolutionized the field because the meaning of a word inherently depends on its neighbors. Yet, in neural text retrieval, we have largely remained in the "static" era. Most modern retrievers encode documents in total isolation, ignoring the broader corpus to which they belong.

A new paper from researchers at Cornell University, Contextual Document Embeddings (CDE), aims to change this paradigm. By introducing context into both the training process and the model architecture, they have achieved state-of-the-art results on the MTEB benchmark for small models—all without the expensive "tricks" usually required to top the leaderboards.

The Problem with "Out-of-Context" Retrievers

Most modern retrieval systems rely on a dual-encoder (or bi-encoder) architecture. They convert a document into a single vector and a query into another, then find the closest match in vector space. The catch is that the document's vector is computed entirely independently of every other document in the database.

This isolation is problematic because the "importance" of a word is relative. Consider the acronym "NFL." In a general Wikipedia search, "NFL" is a highly specific and useful term. However, in a database consisting entirely of sports articles, "NFL" appears everywhere and provides almost no distinguishing signal.

Old-school statistical methods like TF-IDF or BM25 handle this naturally by utilizing "Inverse Document Frequency" (IDF)—they inherently know which words are rare or common across the entire collection. Neural models, by encoding documents one by one, are effectively "out-of-context." Consequently, they struggle to adapt when moved from their training data to a new, specific domain (the "out-of-domain" problem).

Methodology: Injecting Context

The authors propose a two-pronged solution to inject context into the embedding process:

Overview of Contextual Batching and Contextual Embedding architecture.

1. Contextual Training (Adversarial Contrastive Learning)

Standard training uses random batches of documents. CDE instead uses fast K-Means clustering to group similar documents into the same training batch.

  • Why it works: By forcing the model to distinguish between very similar documents in the same batch (e.g., five different articles about the exact same football game), the model must learn much finer, more discriminative features. This acts as a natural "hard negative" generator without requiring complex mining algorithms.

2. Contextual Architecture

The authors designed a novel two-stage encoder to explicitly capture neighborhood information:

  • Stage 1 (Gather): A first-pass encoder embeds a set of "neighboring" documents from the corpus.
  • Stage 2 (Embed): The target document is encoded by a second transformer that takes both the document's own tokens and the embeddings of its neighbors as input.
  • Efficiency: To make this computationally feasible, the team utilizes gradient caching (to fit more context into memory) and context reuse (sharing the same neighborhood embeddings across a batch).

Datasets and Evaluation

The models were subjected to rigorous evaluation:

  • Pre-training: 200 million weakly-supervised pairs scraped from Wikipedia and Reddit.
  • Supervised Fine-tuning: 1.8 million query-document pairs from popular sets like MS MARCO and HotpotQA, plus the BGE meta-datasets.
  • Evaluation: The MTEB (Massive Text Embedding Benchmark) and a truncated version of BEIR for smaller-scale ablation studies.

Key Insights and Results

  • The "Neighborhood" is a Proxy for IDF: By observing a sample of neighboring documents during encoding, the model dynamically "senses" which terms are common in the current context and which are unique, effectively learning a neural version of IDF.
  • Harder is Better: The clustering-based batching strategy proved that how training data is organized is just as critical as the data itself. Making the model's "in-batch" choices harder leads to significantly more robust embeddings.
Performance comparison across various datasets showing Contextual improvements.
  • State-of-the-Art Performance: CDE achieved the highest score on the MTEB benchmark for models with fewer than 250 million parameters (scoring 65.00 overall). It outperformed popular models like bge-base and gte-base despite using a simpler training recipe.
  • Zero-Shot Power & Domain Adaptation: The most significant gains were observed in "out-of-domain" tasks. Because the model learns to look at its neighbors, it can adapt to the statistics of a new, unseen dataset at test time without further training. On specific datasets like ClimateFEVER (CFEVER) and ArguAna (Arg), the contextual architecture provided a massive boost compared to standard baselines.

Limitations and Future Work

While highly effective, the approach has a few limitations:

  • False Negatives: Because the model clusters similar documents together, there is a risk that a "negative" in a training batch is actually a valid answer to the query. The authors use a filtering heuristic to mitigate this, but it remains an ongoing challenge.
  • Inference Latency: While the "Stage 1" neighborhood embeddings can be pre-computed and cached, the "Stage 2" encoding still requires processing extra context tokens, adding computational overhead compared to a vanilla bi-encoder.
  • Scaling: The current work focuses on smaller models (<250M parameters). Exploring how these contextual benefits scale to multi-billion parameter Large Language Models (LLMs) is a natural and exciting next step.

Summary updated

BibTeX
@inproceedings{morris2025contextual,
  author = {John Xavier Morris and Alexander M Rush},
  title = {{Contextual Document Embeddings}},
  booktitle = {International Conference on Learning Representations},
  year = {2025},
  url = {https://openreview.net/forum?id=Wqsk3FbD6D},
}
Figure