What is a contextual embedding?
A contextual embedding is an embedding type in which a word's or token's vector is not fixed but changes with the sentence it appears in; for example, the Turkish word "yüz" gets different vectors as a number and as a face.
Unlike a classic word embedding, which gives each word one fixed vector and cannot separate senses, a contextual embedding computes the vector from surrounding words. Contextual retrieval is a different concept: adding document context to chunks before embedding.
- Topic
- Embeddings, document processing and RAG
- Also known as
- Contextualised embedding
- Origin
- Peters et al., 2018
How a contextual embedding works
- Text passes through a pre-trained language model, such as a bidirectional transformer encoder.
- A vector accounting for its surroundings is taken for each token from the model's internal layers.
- For sentence or paragraph embeddings, token vectors are averaged or a special token's vector is used.
- These vectors feed tasks such as classification, entity recognition or semantic search.
In more detail
Anisotropy and sentence-level fine-tuning (SBERT)
Raw contextual vectors from general pre-trained models suffer from anisotropy, clustering into a narrow cone in vector space, so unweighted averaging works poorly for semantic search. Contrastive bi-encoder fine-tuning (pioneered by Sentence-BERT) reshapes the embedding space so cosine similarity accurately reflects semantic closeness across sentences and passages.
Decoder-based embedding models and Matryoshka dimension flexibility
While early contextual embeddings relied on bidirectional encoders like ELMo and BERT, many modern high-performing embedding models adapt causal decoder-only LLMs by pooling the final token or enabling bidirectional attention. Many also use Matryoshka Representation Learning (MRL), allowing vectors to be truncated to smaller dimensions (for example, from 1024 to 256) with minimal accuracy loss to save vector-database memory.
Multi-vector late interaction (ColBERT) instead of a single vector
Instead of collapsing all token vectors into one passage vector, late-interaction architectures such as ColBERT retain each token's contextual embedding and compute fine-grained token-to-token maximum similarity (MaxSim) at query time. This preserves exact lexical and contextual nuances at the cost of higher index storage.
Subword tokenisation and agglutinative Turkish
Subword tokenisers trained primarily on English often split agglutinative Turkish words into many small pieces, meaning a word's meaning is spread across multiple subword contextual vectors and long Turkish documents hit token limits sooner. Evaluating models on Turkish morphological benchmarks and checking token fertility is essential before deployment.
Commonly confused terms
| Term | How it differs |
|---|---|
| Contextual retrieval | Contextual embedding refers to representation vectors conditioned on surrounding tokens within a model's layers; contextual retrieval is a RAG ingestion technique that prepends an LLM-generated document summary to text chunks before indexing. |
| Token-level embeddings versus sentence embeddings | Token-level contextual embeddings produce one vector per token for tasks like NER or late-interaction search (ColBERT), whereas sentence/document embeddings pool or train a single dense vector representing an entire passage for vector database lookup. |
Why a contextual embedding matters for on-premises AI
Contextual embeddings perform far better than static ones on real text where meaning shifts with context. The paper introducing the approach showed word representations learned from a bidirectional language model's internal states markedly improved the state of the art on six hard tasks including question answering and sentiment analysis; the following bidirectional transformer model used left and right context in all layers to reach the state of the art on eleven tasks. Context matters especially in Turkish, where suffixes shift meaning.
An insurer builds a classifier telling apart "hasar" (damage) in car, health or home complaints using contextual embeddings and compares routing accuracy with its old model built on static word embeddings.
Common questions
How does a contextual embedding differ from a word embedding?
A word embedding gives each word one fixed vector. In a contextual embedding the vector changes with the sentence.
Are contextual embedding and contextual retrieval the same?
No. Contextual embedding is token representation varying with context. Contextual retrieval adds document context to chunks before embedding.
Related terms
Sources
- Peters et al., Deep contextualized word representations, 2018
- Devlin et al., BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding, 2018
In the glossary: Glossary, A to C