{"record":{"id":"feb7845d130ccacc","repo":"spring-projects/spring-ai","slug":"failed-to-obtain-the-embedding-dimensions-from-the-feb784","errorCode":null,"errorMessage":"Failed to obtain the embedding dimensions from the embedding model and fall backs to default: ${OPENAI_EMBEDDING_DIMENSION_SIZE}","messagePattern":"Failed to obtain the embedding dimensions from the embedding model and fall backs to default: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"vector-stores/spring-ai-pgvector-store/src/main/java/org/springframework/ai/vectorstore/pgvector/PgVectorStore.java","lineNumber":511,"sourceCode":"\t\t\tcase SERIAL -> \"serial\";\n\t\t\tcase BIGSERIAL -> \"bigserial\";\n\t\t};\n\t}\n\n\tint embeddingDimensions() {\n\t\t// The manually set dimensions have precedence over the computed one.\n\t\tif (this.dimensions > 0) {\n\t\t\treturn this.dimensions;\n\t\t}\n\n\t\ttry {\n\t\t\tint embeddingDimensions = this.embeddingModel.dimensions();\n\t\t\tif (embeddingDimensions > 0) {\n\t\t\t\treturn embeddingDimensions;\n\t\t\t}\n\t\t}\n\t\tcatch (Exception e) {\n\t\t\tlogger.warn(\"Failed to obtain the embedding dimensions from the embedding model and fall backs to default:\"\n\t\t\t\t\t+ OPENAI_EMBEDDING_DIMENSION_SIZE, e);\n\t\t}\n\t\treturn OPENAI_EMBEDDING_DIMENSION_SIZE;\n\t}\n\n\t@Override\n\tpublic VectorStoreObservationContext.Builder createObservationContextBuilder(String operationName) {\n\n\t\treturn VectorStoreObservationContext.builder(VectorStoreProvider.PG_VECTOR.value(), operationName)\n\t\t\t.collectionName(this.vectorTableName)\n\t\t\t.dimensions(this.embeddingDimensions())\n\t\t\t.namespace(this.schemaName)\n\t\t\t.similarityMetric(getSimilarityMetric());\n\t}\n\n\tprivate String getSimilarityMetric() {\n\t\tVectorStoreSimilarityMetric metric = SIMILARITY_TYPE_MAPPING.get(this.distanceType);\n\t\treturn metric != null ? metric.value() : this.distanceType.name();","sourceCodeStart":493,"sourceCodeEnd":529,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/vector-stores/spring-ai-pgvector-store/src/main/java/org/springframework/ai/vectorstore/pgvector/PgVectorStore.java#L493-L529","documentation":"PgVectorStore.embeddingDimensions() calls embeddingModel.dimensions() to size the vector column; on failure or a non-positive result it warns and returns OPENAI_EMBEDDING_DIMENSION_SIZE (1536). The pgvector table may then be created with a 1536-dimension vector column that mismatches the actual embedding size, causing insert errors.","triggerScenarios":"Initializing the store (afterPropertiesSet, createObservationContextBuilder, dim, actualDimensions) when embeddingModel.dimensions() throws or returns <= 0 — e.g. unreachable embedding API, missing key, or custom model.","commonSituations":"Non-OpenAI models (Ollama, Mistral, local models) used with default builder config; offline CI environments; misconfigured base URL or API key at startup.","solutions":["Set the dimension explicitly: PgVectorStore.builder(jdbcTemplate, embeddingModel).dimensions(768).build()","Fix the embedding model configuration (API key, endpoint) so dimensions() succeeds","Verify the EmbeddingModel implementation supports dimensions() and returns a positive value","If the table was created with the wrong dimension, drop and recreate the vector_store table"],"exampleFix":"// before\nPgVectorStore.builder(jdbcTemplate, embeddingModel).build();\n\n// after\nPgVectorStore.builder(jdbcTemplate, embeddingModel)\n    .dimensions(1536) // explicit, matches your model\n    .build();","handlingStrategy":"validation","validationCode":"int dims = embeddingModel.dimensions();\nif (dims <= 0) {\n    throw new IllegalStateException(\"PgVectorStore: set dimensions(n) explicitly; model reported \" + dims);\n}","typeGuard":null,"tryCatchPattern":"try {\n    pgVectorStore.afterPropertiesSet();\n} catch (Exception e) {\n    logger.error(\"pgvector init fell back to 1536 dimensions\", e);\n}","preventionTips":["Set .dimensions(n) explicitly in PgVectorStore.builder for non-OpenAI models","Validate embedding connectivity before schema creation","Keep a schema migration asserting vector column size","Recreate the table if created with wrong dimension"],"tags":["embedding","dimensions","pgvector","fallback"],"backgroundTag":"unexpected-response-shape","analyzedSha":"98a7beda4f29d80a71c5837eb4053b03a93a46f7","analyzedAt":"2026-09-11T14:15:49.441Z","contentChangedAt":"2026-09-11T14:15:49.441Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}