What happens when you compute cosine similarity against the zero vector?
Cosine with the zero vector is undefined (0/0); NumPy returns NaN, scikit-learn returns 0, some DBs throw, making zero vectors a frequent silent-bug source.
Imagine measuring the angle between two arrows. One arrow has length zero, so it has no direction at all. There is no angle to measure. Different calculators handle that question differently. Some shrug and say 'I don't know' (NaN), some round it to zero, some refuse and throw an error. The trap in production is that a single zero vector in your corpus can quietly poison search results because the system doesn't crash; it just returns something wrong.
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.
6-8 min: state the math + library behavior matrix + sources of zero vectors + mitigation pattern + relationship to L2 normalization.
Real products, models, and research that use this idea.
- NumPy raises RuntimeWarning and returns NaN for zero-norm cosine calls.
- scikit-learn cosine_similarity returns 0 silently for the zero vector.
- Pinecone client validates non-zero norm at upsert time and raises a 400.
- OpenAI embeddings API returns a 400 for empty input strings, preventing the upstream cause for that surface.
What an interviewer would ask next. Try answering before peeking at the approach.
QBeyond cosine, where else does the zero vector cause undefined behavior?
QHow would you canary an existing index for zero vectors after the fact?
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.
Trusting that the embedding pipeline never produces a zero vector. Empty strings, all-pad tokenization, and failed model calls can return zeros that survive into the index.
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.