From c407822f793873a62b3772ddb8794b5c8c4d3571 Mon Sep 17 00:00:00 2001 From: hanjie Date: Thu, 23 Oct 2025 10:49:24 +0800 Subject: [PATCH] fix(chroma): handle collection not found exception during initialization Add try-catch block to handle HttpClientErrorException.NotFound when getting collection during initialization. This prevents initialization failures when the collection doesn't exist yet. Signed-off-by: hanjie --- .../ai/chroma/vectorstore/ChromaVectorStore.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/vector-stores/spring-ai-chroma-store/src/main/java/org/springframework/ai/chroma/vectorstore/ChromaVectorStore.java b/vector-stores/spring-ai-chroma-store/src/main/java/org/springframework/ai/chroma/vectorstore/ChromaVectorStore.java index bb9468cd285..850f780ada6 100644 --- a/vector-stores/spring-ai-chroma-store/src/main/java/org/springframework/ai/chroma/vectorstore/ChromaVectorStore.java +++ b/vector-stores/spring-ai-chroma-store/src/main/java/org/springframework/ai/chroma/vectorstore/ChromaVectorStore.java @@ -48,6 +48,7 @@ import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; +import org.springframework.web.client.HttpClientErrorException; /** * {@link ChromaVectorStore} is a concrete implementation of the {@link VectorStore} @@ -117,7 +118,16 @@ public static Builder builder(ChromaApi chromaApi, EmbeddingModel embeddingModel @Override public void afterPropertiesSet() throws Exception { if (!this.initialized) { - var collection = this.chromaApi.getCollection(this.tenantName, this.databaseName, this.collectionName); + //if the collection doesn't exist, chromaApi.getCollection will throw an exception + ChromaApi.Collection collection = null; + try { + collection = this.chromaApi.getCollection(this.tenantName, this.databaseName, this.collectionName); + } + catch (Exception ex) { + if (!(ex.getCause() instanceof HttpClientErrorException.NotFound)) { + throw ex; + } + } if (collection == null) { if (this.initializeSchema) { var tenant = this.chromaApi.getTenant(this.tenantName);