microsoft/semantic-kernel · error · ServiceInitializationError

Dimensionality must be a positive integer.

Error message

Dimensionality must be a positive integer. 

What it means

Raised by PostgresMemoryStore._check_dimensionality() when dimension_num <= 0. It is a ServiceInitializationError fired at construction or at create_collection time, indicating an invalid (non-positive) dimensionality was supplied.

Source

Thrown at python/semantic_kernel/connectors/memory_stores/postgres/postgres_memory_store.py:499

    async def __get_collections(self, cur: Cursor) -> list[str]:
        cur.execute(
            """
            SELECT table_name
            FROM information_schema.tables
            WHERE table_schema = %s
            """,
            (self._schema,),
        )
        return [row[0] for row in cur.fetchall()]

    def _check_dimensionality(self, dimension_num):
        if dimension_num > MAX_DIMENSIONALITY:
            raise ServiceInitializationError(
                f"Dimensionality of {dimension_num} exceeds " + f"the maximum allowed value of {MAX_DIMENSIONALITY}."
            )
        if dimension_num <= 0:
            raise ServiceInitializationError("Dimensionality must be a positive integer. ")

    def __serialize_metadata(self, record: MemoryRecord) -> str:
        return json.dumps({
            "text": record._text,
            "description": record._description,
            "additional_metadata": record._additional_metadata,
        })

    # Enable the connection pool to be closed when using as a context manager
    def __enter__(self) -> "PostgresMemoryStore":
        """Enter the runtime context."""
        return self

    def __exit__(self, exc_type, exc_value, traceback) -> bool:
        """Exit the runtime context and dispose of the connection pool."""
        self._connection_pool.close()
        return False

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Supply a concrete positive integer (e.g. 1536 for ada-002, 1536/3072 for text-embedding-3).
  2. Validate config: assert dimension_num and dimension_num > 0 before constructing the store.
  3. Fix the source of the value (env var, settings model) so it resolves to the model's real dimensionality.
  4. Default to the embedding generator's documented dimension rather than 0.

Example fix

// before
store = PostgresMemoryStore(conn_str, default_dimensionality=int(os.getenv('DIM') or 0))
// after
dim = int(os.getenv('DIM') or 1536)
assert dim > 0
store = PostgresMemoryStore(conn_str, default_dimensionality=dim)
Defensive patterns

Strategy: validation

Validate before calling

assert isinstance(dim, int) and dim > 0, 'dimensionality must be a positive integer'
store = PostgresMemoryStore(conn_str, default_dimensionality=dim)

Type guard

def valid_dim(dim) -> bool:
    return isinstance(dim, int) and dim > 0

Prevention

When it happens

Trigger: Passing default_dimensionality=0 or a negative value to the constructor, or create_collection(name, dimension_num=0). Often happens when the value is read from an unset config/env var that defaults to 0.

Common situations: Unset embedding-dimension env var coerced to 0; passing None through an int cast that yields 0; logic that computes dimension from a failed model load.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/b87dffafe7c218f6. Report an issue: GitHub.