From 6fe93140a77010bf843f46bf2df2fca17b9b85a8 Mon Sep 17 00:00:00 2001 From: Chris Bartholomew Date: Thu, 8 Jan 2026 16:48:25 -0500 Subject: [PATCH] Fix embedding dimension for tenant schemas (#135) Call ensure_embedding_dimension after running migrations for tenant schemas. This ensures the embedding column dimension matches the model's dimension, which may differ from the default 384 dimensions used in the initial migration. Without this fix, using embedding providers with different dimensions (e.g., Cohere's embed-english-v3.0 with 1024 dims) would fail with "expected 384 dimensions, not 1024" errors on tenant schemas. --- hindsight-api/hindsight_api/extensions/context.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/hindsight-api/hindsight_api/extensions/context.py b/hindsight-api/hindsight_api/extensions/context.py index d229d48d..e470f60b 100644 --- a/hindsight-api/hindsight_api/extensions/context.py +++ b/hindsight-api/hindsight_api/extensions/context.py @@ -96,7 +96,7 @@ class DefaultExtensionContext(ExtensionContext): async def run_migration(self, schema: str) -> None: """Run migrations for a specific schema.""" - from hindsight_api.migrations import run_migrations + from hindsight_api.migrations import ensure_embedding_dimension, run_migrations # Prefer getting URL from memory engine (handles pg0 case where URL is set after init) db_url = self._database_url @@ -107,6 +107,15 @@ class DefaultExtensionContext(ExtensionContext): run_migrations(db_url, schema=schema) + # Ensure embedding column dimension matches the model's dimension + # This is needed because migrations create columns with default dimension + if self._memory_engine is not None: + embeddings = getattr(self._memory_engine, "embeddings", None) + if embeddings is not None: + dimension = getattr(embeddings, "dimension", None) + if dimension is not None: + ensure_embedding_dimension(db_url, dimension, schema=schema) + def get_memory_engine(self) -> "MemoryEngineInterface": """Get the memory engine interface.""" if self._memory_engine is None: