Why is cosine similarity preferred over Euclidean distance for text embeddings?
Text embedding norms vary with content length and density, so cosine measures direction (semantic meaning) while ignoring magnitude. Euclidean distance would conflate length with semantic difference.
Imagine arrows on a giant map, all starting from the centre and pointing outward. Each arrow stands for a sentence. We want to measure how alike two sentences are. One way: ask if the arrows point in the same direction, no matter how long they are. Another way: ask how close the tips of the arrows are. The first way only cares about direction. The second cares about direction and length together. For sentences, the direction is what carries meaning. The length usually just reflects boring things like how many words were in the sentence. If we measured by tip position, two paragraphs about the same topic but with different lengths would look unrelated. By only looking at direction, they line up the way a person would expect: same idea, same heading on the map.
Detailed answer & concept explanation~4 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
Open with direction vs magnitude as the conceptual axis. State why direction carries meaning and magnitude carries length-and-density noise. Note that modern APIs normalise, which makes cosine and Euclidean rank equivalently. Close with the contrastive-training argument tying the inference metric to the training objective.
Real products, models, and research that use this idea.
- OpenAI's `text-embedding-3-large` returns unit-normalised vectors, so dot-product equals cosine and either is the natural index metric.
- Voyage AI's `voyage-3` and Cohere's `embed-english-v3` both follow the same unit-norm convention for the same reason.
- pgvector supports cosine, L2, and inner product distances and lets the user pick per index, but the documentation recommends cosine for sentence-embedding workloads.
- Pinecone's documentation explicitly recommends cosine for text and L2 for image embeddings, reflecting how the training objective shapes the right metric.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhy does the contrastive training objective most modern embedding models use favour cosine at inference?
QHow does Matryoshka representation learning interact with the choice of distance metric?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Assuming cosine and Euclidean rank results identically. They agree only when all vectors are normalised to unit length. Modern embedding APIs often already L2-normalise, hiding the distinction in practice.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.