MemPalace/mempalace · error · ValueError

embedding dimension must be positive

Error message

embedding dimension must be positive

What it means

Raised by QdrantCollection._ensure_remote_collection() when the dimension passed for creating/validating the remote collection is zero or negative. A vector collection needs a positive vector size; this guard fires before any server call, typically because the computed embedding dimension was 0.

Source

Thrown at mempalace/backends/qdrant.py:740

            info = self._client.get_collection_info(self._remote_collection)
        except _QdrantHTTPError as exc:
            if exc.status == 404:
                return None
            raise
        result = info.get("result") or info
        params = (result.get("config") or {}).get("params") or {}
        vectors = params.get("vectors") or params.get("vectors_config") or {}
        if isinstance(vectors, dict) and "size" in vectors:
            return int(vectors["size"])
        if isinstance(vectors, dict):
            for value in vectors.values():
                if isinstance(value, dict) and "size" in value:
                    return int(value["size"])
        return None

    def _ensure_remote_collection(self, dimension: int) -> None:
        if dimension <= 0:
            raise ValueError("embedding dimension must be positive")
        with self._lock:
            self._ensure_open()
            if self._known_dimension is not None:
                if self._known_dimension != dimension:
                    raise DimensionMismatchError(
                        f"qdrant collection {self._collection_name!r} expects "
                        f"embedding dimension {self._known_dimension}, got {dimension}"
                    )
                return
            if not self._remote_exists():
                self._client.create_collection(self._remote_collection, dimension)
                self._client.create_payload_index(
                    self._remote_collection, _PAYLOAD_DOCUMENT, "text"
                )
                self._known_dimension = dimension
                return
            remote_dim = self._remote_dimension()
            if remote_dim is not None and remote_dim != dimension:

View on GitHub (pinned to 06cb6987f0)

Solutions

  1. Log the dimension value right before the write; find why it is <= 0 (usually an empty embeddings list slipped past earlier checks)
  2. Verify the embedding model returns vectors: len(model.embed('test')) > 0
  3. If dimension comes from config, correct the value to the model's actual output size (e.g. 768)
  4. Add a startup assertion that the configured model's embedding length is positive

Example fix

# before
 dim = len(embeddings[0]) if embeddings else 0
collection._ensure_remote_collection(dim)  # ValueError
# after
 dim = len(embeddings[0])
assert dim > 0, f"bad embedding dimension: {dim}"
Defensive patterns

Strategy: validation

Validate before calling

if not embeddings or len(embeddings[0]) <= 0:
    raise ValueError("cannot derive a positive embedding dimension")

Type guard

def positive_dim(embeddings) -> bool:
    return bool(embeddings) and isinstance(embeddings[0], (list, tuple)) and len(embeddings[0]) > 0

Prevention

When it happens

Trigger: Passing embeddings that are empty is caught earlier, but code paths that derive a dimension (e.g. int(arr.size) from a degenerate batch, or a caller-supplied dimension of 0) reach this check. Most commonly a bug where dimension is computed from an empty list or a failed embedder result.

Common situations: Embedding model not loaded (returns empty), dimension inferred from the first batch which was empty, or hardcoded/typo'd dimension config (0 or -1).

Related errors


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