MemPalace/mempalace · error · DimensionMismatchError

pgvector collection {self._collection_name!r} expects embedd

Error message

pgvector collection {self._collection_name!r} expects embedding dimension {self._known_dimension}, got {int(q.size)}

What it means

Each PostgreSQL table backing a pgvector collection is created with one fixed embedding dimension (recorded and cached as _known_dimension). When a query vector's size differs, the backend raises DimensionMismatchError because pgvector's index and distance operators cannot mix dimensions — the query would either fail in SQL or match nothing. The expected and received dimensions are both included in the message.

Source

Thrown at mempalace/backends/pgvector.py:1116

        if not self._table_exists():
            if self._marker_exists():
                raise CollectionNotInitializedError(self._collection_name)
            return QueryResult.empty(
                num_queries=len(query_embeddings),
                embeddings_requested=bool(include and "embeddings" in include),
            )
        spec = _IncludeSpec.resolve(include, default_distances=True)
        outer_ids: list[list[str]] = []
        outer_docs: list[list[str]] = []
        outer_metas: list[list[dict]] = []
        outer_dists: list[list[float]] = []
        outer_embeds: list[list[list[float]]] = []
        for query_vector in query_embeddings:
            q = _as_vector_array(query_vector)
            if self._known_dimension is None:
                self._known_dimension = self._client.table_dimension(self._table)
            if self._known_dimension is not None and int(q.size) != self._known_dimension:
                raise DimensionMismatchError(
                    f"pgvector collection {self._collection_name!r} expects "
                    f"embedding dimension {self._known_dimension}, got {int(q.size)}"
                )
            rows = self._client.query_rows(
                self._table,
                vector=q.astype(float).tolist(),
                limit=n_results,
                where=where,
                with_embedding=spec.embeddings,
            )
            outer_ids.append([row["id"] for row in rows])
            outer_docs.append([row["document"] for row in rows] if spec.documents else [])
            outer_metas.append([row["metadata"] for row in rows] if spec.metadatas else [])
            outer_dists.append(
                [float(row["distance"]) if row["distance"] is not None else 1.0 for row in rows]
                if spec.distances
                else []
            )

View on GitHub (pinned to 06cb6987f0)

Solutions

  1. Re-embed your query with the same model used to build the collection (check the embedder sidecar recorded next to the marker).
  2. If you intentionally switched embedders, recreate the collection/table and re-ingest so all vectors share the new dimension.
  3. Inspect the collection dimension first (it is reported in maintenance/describe stats) and validate your vectors against it.

Example fix

# before
# collection built with 384-dim MiniLM
col.query(query_embeddings=[nomic_768d_vector], n_results=5)

# after
# same embedder as ingest:
col.query(query_embeddings=[minilm_384d_vector], n_results=5)
Defensive patterns

Strategy: type-guard

Validate before calling

dim = collection_dimension  # from backend describe/maintenance stats
vecs = [v for v in vecs if len(v) == dim]
col.query(query_embeddings=vecs, n_results=5)

Type guard

def matches_dimension(vectors, dim: int) -> bool:
    return all(len(v) == dim for v in vectors)

Try / catch

from mempalace.backends.base import DimensionMismatchError
try:
    col.query(query_embeddings=vecs, n_results=5)
except DimensionMismatchError as e:
    logger.error("embedder model changed; rebuild collection", exc_info=e)
    raise

Prevention

When it happens

Trigger: Calling query(query_embeddings=[[1.0, 2.0]]) against a collection whose table was built with 384-dimensional vectors; switching embedder models (e.g. all-MiniLM-L6-v2 384d → nomic-embed-text 768d) without rebuilding the collection; a single malformed short vector inside the batch.

Common situations: Changing the Ollama embedding model after the palace was built; mixing embeddings from different providers in one codebase; manually hand-crafting a test vector with the wrong length.

Related errors


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