spring-projects/spring-ai · warning

Failed to obtain the embedding dimensions from the…

Error message

Failed to obtain the embedding dimensions from the embedding model and fall backs to default: ${OPENAI_EMBEDDING_DIMENSION_SIZE}

What it means

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.

Solutions

  1. Set the dimension explicitly: PgVectorStore.builder(jdbcTemplate, embeddingModel).dimensions(768).build()
  2. Fix the embedding model configuration (API key, endpoint) so dimensions() succeeds
  3. Verify the EmbeddingModel implementation supports dimensions() and returns a positive value
  4. If the table was created with the wrong dimension, drop and recreate the vector_store table

Example fix

// before
PgVectorStore.builder(jdbcTemplate, embeddingModel).build();

// after
PgVectorStore.builder(jdbcTemplate, embeddingModel)
    .dimensions(1536) // explicit, matches your model
    .build();
Defensive patterns

Strategy: validation

Validate before calling

int dims = embeddingModel.dimensions();
if (dims <= 0) {
    throw new IllegalStateException("PgVectorStore: set dimensions(n) explicitly; model reported " + dims);
}

Try / catch

try {
    pgVectorStore.afterPropertiesSet();
} catch (Exception e) {
    logger.error("pgvector init fell back to 1536 dimensions", e);
}

Prevention

When it happens

Trigger: Initializing the store (afterPropertiesSet, createObservationContextBuilder, dim, actualDimensions) when embeddingModel.dimensions() throws or returns <= 0 — e.g. unreachable embedding API, missing key, or custom model.

Common situations: Non-OpenAI models (Ollama, Mistral, local models) used with default builder config; offline CI environments; misconfigured base URL or API key at startup.

Related errors


AI-assisted analysis of spring-projects/spring-ai@98a7beda4f (2026-09-11). Data as JSON: /api/errors/feb7845d130ccacc. Report an issue: GitHub.

Appendix: source

Thrown at vector-stores/spring-ai-pgvector-store/src/main/java/org/springframework/ai/vectorstore/pgvector/PgVectorStore.java:511

			case SERIAL -> "serial";
			case BIGSERIAL -> "bigserial";
		};
	}

	int embeddingDimensions() {
		// The manually set dimensions have precedence over the computed one.
		if (this.dimensions > 0) {
			return this.dimensions;
		}

		try {
			int embeddingDimensions = this.embeddingModel.dimensions();
			if (embeddingDimensions > 0) {
				return embeddingDimensions;
			}
		}
		catch (Exception e) {
			logger.warn("Failed to obtain the embedding dimensions from the embedding model and fall backs to default:"
					+ OPENAI_EMBEDDING_DIMENSION_SIZE, e);
		}
		return OPENAI_EMBEDDING_DIMENSION_SIZE;
	}

	@Override
	public VectorStoreObservationContext.Builder createObservationContextBuilder(String operationName) {

		return VectorStoreObservationContext.builder(VectorStoreProvider.PG_VECTOR.value(), operationName)
			.collectionName(this.vectorTableName)
			.dimensions(this.embeddingDimensions())
			.namespace(this.schemaName)
			.similarityMetric(getSimilarityMetric());
	}

	private String getSimilarityMetric() {
		VectorStoreSimilarityMetric metric = SIMILARITY_TYPE_MAPPING.get(this.distanceType);
		return metric != null ? metric.value() : this.distanceType.name();

View on GitHub (pinned to 98a7beda4f)