Similarity utilities reject dense tensor lists returned by encode

Author: LimbC-CCreated Sep 30, 2026Updated Sep 30, 2026

SentenceTransformer.encode(..., convert_to_numpy=False) returns a list of dense tensors. Passing that result to the public similarity utilities raises ValueError: only one element tensors can be converted to Python scalars. Stacking the same list manually works.

Minimal reproduction (no model download):

python
import torch
from sentence_transformers.util import cos_sim

embeddings = [torch.tensor([1.0, 0.0]), torch.tensor([0.0, 1.0])]
print(cos_sim(torch.stack(embeddings), torch.stack(embeddings)))  # identity matrix
print(cos_sim(embeddings, embeddings))  # ValueError

The conversion helper has specialized paths for sparse tensor lists and NumPy array lists, but dense tensor lists fall through to torch.tensor, which tries to interpret the vectors as scalar values. Expected behavior is to stack them while preserving dtype, device and gradients.

Reproduced on main 4a3b5cd6ec718e421f57e824a41ed3fd99595df6, Python 3.12.14, PyTorch 2.14.0+cpu, Windows. Eight regression cases using the existing local WordEmbeddings model fixture fail before the fix, covering cosine, dot, Euclidean and Manhattan scores and their pairwise variants.

Related: #4064 changes semantic_search to accept NumPy lists in retrieval.py. This report concerns existing similarity APIs and the dense tensor list path in tensor.py; it does not duplicate that patch.

AI disclosure: OpenAI Codex assisted with investigation, implementation and local testing. No remote model or paid API is needed for this reproduction.

Source: huggingface/sentence-transformers