{"record":{"id":"172e57acc42d9e9b","repo":"MemPalace/mempalace","slug":"embedding-dimension-must-be-positive","errorCode":null,"errorMessage":"embedding dimension must be positive","messagePattern":"embedding dimension must be positive","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"mempalace/backends/milvus.py","lineNumber":393,"sourceCode":"            return None\n        fields = []\n        if isinstance(info, dict):\n            fields = info.get(\"fields\") or (info.get(\"schema\") or {}).get(\"fields\") or []\n        for field in fields:\n            name = field.get(\"name\") or field.get(\"field_name\")\n            if name != FIELD_VECTOR:\n                continue\n            params = field.get(\"params\") or field.get(\"type_params\") or {}\n            dim = field.get(\"dim\") or params.get(\"dim\")\n            try:\n                return int(dim)\n            except (TypeError, ValueError):\n                return None\n        return None\n\n    def _ensure_remote_collection(self, dimension: int) -> None:\n        if dimension <= 0:\n            raise ValueError(\"embedding dimension must be positive\")\n        with self._lock:\n            self._ensure_open()\n            if self._known_dimension is not None:\n                if self._known_dimension != dimension:\n                    raise DimensionMismatchError(\n                        f\"milvus collection {self._collection_name!r} expects \"\n                        f\"embedding dimension {self._known_dimension}, got {dimension}\"\n                    )\n                return\n            if not self._remote_exists():\n                self._backend._create_remote_collection(\n                    self._client,\n                    self._remote_collection,\n                    dimension,\n                    consistency_level=self._config.consistency_level,\n                )\n                self._backend._load_remote_collection(self._client, self._remote_collection)\n                self._known_dimension = dimension","sourceCodeStart":375,"sourceCodeEnd":411,"githubUrl":"https://github.com/MemPalace/mempalace/blob/06cb6987f02610784fefbad4b2bd5d026d164ba6/mempalace/backends/milvus.py#L375-L411","documentation":"Raised by MilvusCollection._ensure_remote_collection() when the dimension argument is <= 0. Before creating or validating the remote Milvus collection the backend sanity-checks the embedding dimension; zero or negative dims (which can slip through when a dim is computed from empty config or parsed as -1 'unknown') are rejected with plain ValueError. Note this is the collection-creation path; batch-level shape errors are caught earlier by _as_vector_array (error 34).","triggerScenarios":"Calling add()/query() where the embedder reports dimension 0 (uninitialized model config), or code passing dim=-1 as an 'unknown' sentinel into the collection bootstrap path.","commonSituations":"Embedder not yet loaded so its dim attribute is 0/None→0; misconfigured embedding model section; custom embedders whose dimension property is computed before initialization.","solutions":["Ensure the embedder is loaded and reports its true dimension (e.g. 384/768) before first add()","Validate config: dimension must be a positive int, not 0/-1/None","Lazy-create the collection only after the first real embedding is available"],"exampleFix":"# before\ndim = embedder.dimension  # 0 because model not loaded\ncollection._ensure_remote_collection(dim)\n\n# after\nembedder.load()\ndim = embedder.dimension  # e.g. 768\nassert dim > 0, f\"bad embedding dimension: {dim}\"\ncollection._ensure_remote_collection(dim)","handlingStrategy":"validation","validationCode":"def valid_dimension(dim) -> bool:\n    return isinstance(dim, int) and not isinstance(dim, bool) and dim > 0","typeGuard":null,"tryCatchPattern":"try:\n    collection.add(ids=ids, documents=docs, embeddings=embs)\nexcept ValueError as e:\n    if \"dimension must be positive\" in str(e):\n        raise RuntimeError(\"embedder dimension not initialized — load the model before first insert\") from e\n    raise","preventionTips":["Load the embedder and read its real dimension before any collection write","Reject 0/-1/None dimensions in config validation at startup","Create/validate the collection lazily on first real embedding"],"tags":["milvus","embeddings","dimension","validation"],"backgroundTag":null,"analyzedSha":"06cb6987f02610784fefbad4b2bd5d026d164ba6","analyzedAt":"2026-08-15T03:03:36.213Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}