Handling Bad Vectors and Empty Results

edge cases: zero vectors, dimension mismatches, empty corpora

Part of: Semantic Search Engine

Every piece works on clean input. Real corpora aren't clean. A document that failed to embed leaves a zero vector behind, a bug attaches a vector of the wrong length, or a search runs before the corpus has anything in it. This lesson hardens the pipeline against all three. Zero vectors You already guard against this in cosine similarity (lesson 3): if either vector's norm is 0, dividing would crash, so you return 0.0 instead. But why would a zero vector show up? Usually something upstream failed silently, an empty string got embedded, a placeholder document was never processed, or a bug zeroed out a field. Scoring it as 0.0 instead of crashing the search means one bad document doesn't take down every other result. Dimension mismatches Every vector in an index should have the same length, the dimension your embedding model produces. When one doesn't, cosine similarity's zip(a, b) silently truncates to the shorter length and returns a meaningless number instead of raising an error. That's worse than a crash, because it looks like a valid result. Check the dimension before scoring and skip (or flag) anything that doesn't match: Empty corpora If the index is empty there is nothing to r

Challenge: Search That Doesn't Crash