{"record":{"id":"856c69c93db79f2c","repo":"microsoft/semantic-kernel","slug":"invalid-vectors-cannot-compute-cosine-similarity","errorCode":null,"errorMessage":"Invalid vectors, cannot compute cosine similarity scoresfor zero vectors{embedding_array} or {embedding}","messagePattern":"Invalid vectors, cannot compute cosine similarity scoresfor zero vectors(.+?) or (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/connectors/memory_stores/chroma/utils.py","lineNumber":121,"sourceCode":"\n    # Compute indices for which the similarity scores can be computed\n    valid_indices = (query_norm != 0) & (collection_norm != 0)\n\n    # Initialize the similarity scores with -1 to distinguish the cases\n    # between zero similarity from orthogonal vectors and invalid similarity\n    similarity_scores = array([-1.0] * embedding_array.shape[0])\n\n    if valid_indices.any():\n        similarity_scores[valid_indices] = embedding.dot(embedding_array[valid_indices].T) / (\n            query_norm * collection_norm[valid_indices]\n        )\n        if not valid_indices.all():\n            logger.warning(\n                \"Some vectors in the embedding collection are zero vectors.\"\n                \"Ignoring cosine similarity score computation for those vectors.\"\n            )\n    else:\n        raise ValueError(\n            f\"Invalid vectors, cannot compute cosine similarity scoresfor zero vectors{embedding_array} or {embedding}\"\n        )\n    return similarity_scores\n","sourceCodeStart":103,"sourceCodeEnd":125,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/memory_stores/chroma/utils.py#L103-L125","documentation":"Raised as a ValueError in chroma_compute_similarity_scores (chroma/utils.py) when cosine similarity cannot be computed for ANY vector in the batch. The function precomputes valid_indices = (query_norm != 0) & (collection_norm != 0); when none are valid it raises. This happens when the query embedding is a zero vector (making all collection vectors invalid) or when every collection embedding is a zero vector. Non-zero vectors mixed with some zero vectors only trigger a warning, not this error.","triggerScenarios":"Calling get_nearest_matches with an embedding that is all zeros (e.g. a failed embedding model call returning zeros). Or querying against a collection where every stored embedding is a zero vector. The function is called internally by the store's similarity search.","commonSituations":"An embedding service returning a zero vector on error/timeout without raising. Sentinel/debug embeddings set to zeros. Dimension mismatch causing a degenerate embedding. A model not yet loaded producing zeros. Note the message has a typo ('scoresfor', no space before the array dump) but the condition is legitimate.","solutions":["Validate the query embedding is non-zero before searching: if numpy.linalg.norm(embedding) == 0, skip or regenerate the embedding.","Investigate why the embedding model produced a zero vector (API failure, wrong input, unloaded model).","If stored embeddings are zero vectors, re-embed the affected records.","Catch ValueError around the search call and fall back to a different retrieval strategy."],"exampleFix":"// before\nmatches = await store.get_nearest_matches('docs', embedding, limit=5)  # ValueError if embedding is zero\n// after\nimport numpy as np\nif np.linalg.norm(embedding) == 0:\n    matches = []\nelse:\n    matches = await store.get_nearest_matches('docs', embedding, limit=5)","handlingStrategy":"validation","validationCode":"import numpy as np\n\ndef is_nonzero_embedding(emb: np.ndarray) -> bool:\n    return float(np.linalg.norm(emb)) != 0.0\n\nif is_nonzero_embedding(embedding):\n    matches = await store.get_nearest_matches('docs', embedding, limit=5)\nelse:\n    matches = []","typeGuard":"import numpy as np\n\ndef is_valid_query_embedding(emb) -> bool:\n    return (\n        isinstance(emb, np.ndarray)\n        and emb.size > 0\n        and float(np.linalg.norm(emb)) != 0.0\n    )","tryCatchPattern":"try:\n    matches = await store.get_nearest_matches('docs', embedding, limit=5)\nexcept ValueError as e:\n    if 'zero vectors' in str(e):\n        logging.warning('Zero-vector embedding; regenerating')\n        embedding = await regenerate_embedding()\n    else:\n        raise","preventionTips":["Validate embedding norm is non-zero before searching.","Investigate embedding models that silently return zero vectors on failure.","Re-embed stored records that are zero vectors.","Catch ValueError around similarity search and fall back gracefully."],"tags":["chroma","cosine-similarity","vector","validation","python"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}