microsoft/semantic-kernel · error · ValueError
Invalid vectors, cannot compute cosine similarity scoresfor
Error message
Invalid vectors, cannot compute cosine similarity scoresfor zero vectors{embedding_array} or {embedding} What it means
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.
Source
Thrown at python/semantic_kernel/connectors/memory_stores/chroma/utils.py:121
# Compute indices for which the similarity scores can be computed
valid_indices = (query_norm != 0) & (collection_norm != 0)
# Initialize the similarity scores with -1 to distinguish the cases
# between zero similarity from orthogonal vectors and invalid similarity
similarity_scores = array([-1.0] * embedding_array.shape[0])
if valid_indices.any():
similarity_scores[valid_indices] = embedding.dot(embedding_array[valid_indices].T) / (
query_norm * collection_norm[valid_indices]
)
if not valid_indices.all():
logger.warning(
"Some vectors in the embedding collection are zero vectors."
"Ignoring cosine similarity score computation for those vectors."
)
else:
raise ValueError(
f"Invalid vectors, cannot compute cosine similarity scoresfor zero vectors{embedding_array} or {embedding}"
)
return similarity_scores
View on GitHub (pinned to c028a0c7dc)
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.
Example fix
// before
matches = await store.get_nearest_matches('docs', embedding, limit=5) # ValueError if embedding is zero
// after
import numpy as np
if np.linalg.norm(embedding) == 0:
matches = []
else:
matches = await store.get_nearest_matches('docs', embedding, limit=5) Defensive patterns
Strategy: validation
Validate before calling
import numpy as np
def is_nonzero_embedding(emb: np.ndarray) -> bool:
return float(np.linalg.norm(emb)) != 0.0
if is_nonzero_embedding(embedding):
matches = await store.get_nearest_matches('docs', embedding, limit=5)
else:
matches = [] Type guard
import numpy as np
def is_valid_query_embedding(emb) -> bool:
return (
isinstance(emb, np.ndarray)
and emb.size > 0
and float(np.linalg.norm(emb)) != 0.0
) Try / catch
try:
matches = await store.get_nearest_matches('docs', embedding, limit=5)
except ValueError as e:
if 'zero vectors' in str(e):
logging.warning('Zero-vector embedding; regenerating')
embedding = await regenerate_embedding()
else:
raise Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Invalid operator
- Radius and height must be non-negative.
- City '{city}' is not in the list of cities: {', '.join(citie
- Tool message found without a preceding message.
- Tool message found after a user or system message.
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/856c69c93db79f2c.
Report an issue: GitHub.