Vector normalization
L2 normalization · Unit vectors
Dividing a vector by its length so it has length 1, which makes dot product, cosine similarity and Euclidean distance agree on rankings.
In DocSlurpGlossary entry
On this page
Normalizing a vector divides each value by the vector’s length, so the result has length exactly 1 and points in the same direction. For v = (3, 4), the length is √(9 + 16) = 5, so the normalized vector is (0.6, 0.8).
Embedding systems normalize at ingest for three reasons:
- Cosine becomes a dot product. With both lengths equal to 1,
cos = a · b, the cheapest comparison there is. - Metrics agree. For unit vectors, squared Euclidean distance is
2 − 2cos, so cosine, dot product and L2 distance all rank results identically. You can use whichever metric your index supports. - Length stops leaking in. Without it, a vector that happens to be longer can win a dot-product comparison for reasons that have nothing to do with meaning.
Normalization must be redone after any operation that changes length. Cutting a Matryoshka vector short is the common case: (0.6, 0.48, 0.64) has length 1, but its first two values (0.6, 0.48) have length 0.768 and must be rescaled to (0.781, 0.625).
The gotchas: a vector of all zeros has no direction and can’t be normalized, so a failed or empty embedding should be rejected rather than stored. And normalizing discards whatever the length meant. If a model was trained to use length as a signal (some dot-product models are), normalizing its output throws that signal away. Follow the model’s documentation, and normalize queries and passages the same way, since mixing normalized and raw vectors in one index gives inconsistent rankings.
In DocSlurp
In DocSlurp
Every catalog embedding profile sets normalize: true; hosted vectors are checked for finite, nonzero values and divided by their length (core/packages/shared/src/embedding.ts). Truncated vectors are re-normalized after cutting.