MemPalace/mempalace · error · ValueError

embedding must be a non-empty 1D vector

Error message

embedding must be a non-empty 1D vector

What it means

Raised by _as_vector_array() when an individual embedding passed to the Qdrant backend is not a non-empty 1D sequence of numbers. The helper converts the input to a float32 numpy array and requires arr.ndim == 1 and arr.size > 0. This is a fail-fast guard so malformed vectors never reach the remote Qdrant server.

Source

Thrown at mempalace/backends/qdrant.py:239

    *,
    documents: list[str],
    ids: list[str],
    metadatas: Optional[list[dict]],
    embeddings: Optional[list[list[float]]],
) -> None:
    n = len(ids)
    if len(documents) != n:
        raise ValueError(f"documents length {len(documents)} does not match ids length {n}")
    if metadatas is not None and len(metadatas) != n:
        raise ValueError(f"metadatas length {len(metadatas)} does not match ids length {n}")
    if embeddings is not None and len(embeddings) != n:
        raise ValueError(f"embeddings length {len(embeddings)} does not match ids length {n}")


def _as_vector_array(vector: list[float]) -> np.ndarray:
    arr = np.asarray(vector, dtype=np.float32)
    if arr.ndim != 1 or arr.size == 0:
        raise ValueError("embedding must be a non-empty 1D vector")
    return arr


def _normalize_vectors(embeddings: list[list[float]]) -> tuple[list[list[float]], int]:
    vectors = []
    dims = set()
    for embedding in embeddings:
        arr = _as_vector_array(embedding)
        vectors.append(arr.astype(float).tolist())
        dims.add(int(arr.size))
    if len(dims) > 1:
        raise DimensionMismatchError(f"qdrant batch cannot mix embedding dimensions {sorted(dims)}")
    return vectors, dims.pop() if dims else 0


def _jsonable_metadata(meta: dict | None) -> dict:
    try:
        value = json.loads(json.dumps(meta or {}, ensure_ascii=False))

View on GitHub (pinned to 06cb6987f0)

Solutions

  1. Log and inspect the failing embedding: check len() and shape of each vector before submit; find which row is empty or nested
  2. Ensure the embedder call skips or rejects empty input text rather than returning an empty list
  3. If batching, validate embeddings with a helper (all(isinstance(v, (list, tuple)) and len(v) > 0 for v in embeddings)) before calling upsert/add
  4. If the model genuinely returns 0-dim for some input, filter those rows out or raise a clearer upstream error in your pipeline

Example fix

// before
 collection.upsert(documents=docs, ids=ids, embeddings=[model.embed(d) for d in docs])  # one embedding empty
// after
 embeddings = [model.embed(d) for d in docs]
 if any(not isinstance(v, (list, tuple)) or len(v) == 0 for v in embeddings):
     raise ValueError("embedder returned an empty/invalid vector")
 collection.upsert(documents=docs, ids=ids, embeddings=embeddings)
Defensive patterns

Strategy: validation

Validate before calling

def valid_embeddings(embeddings):
    return all(
        isinstance(e, (list, tuple)) and len(e) > 0 and all(isinstance(x, (int, float)) for x in e)
        for e in embeddings
    )

if not valid_embeddings(embeddings):
    raise ValueError("bad embeddings batch")

Type guard

def is_embedding_list(v) -> bool:
    return isinstance(v, list) and bool(v) and all(
        isinstance(e, list) and len(e) > 0 and all(isinstance(x, (int, float)) for x in e)
        for e in v
    )

Prevention

When it happens

Trigger: Calling collection.upsert()/add() with an embedding that is an empty list [], a scalar (e.g. 0.5), a nested list ([[1,2],[3,4]] as a single embedding), or a ragged/nested structure that numpy flattens to ndim != 1. Also triggered via _normalize_vectors() during upsert, or during query() when a query_vector inside query_embeddings is empty/scalar.

Common situations: The embedding model returned an empty vector (Ollama/LM Studio returned no embedding for an empty string), a caller passed a batch where one row is [], or a dimension mismatch caused nesting like [[...],[...]] being treated as one vector. Also when text is empty and the embedder silently yields empty output.

Related errors


AI-assisted analysis of MemPalace/mempalace@06cb6987f0 (2026-08-15). Data as JSON: /api/errors/8d5b184e88f8d095. Report an issue: GitHub.