Skip to content

Commit c407822

Browse files
committed
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 <hanjiehu@zksite.com>
1 parent f6ff20a commit c407822

File tree

1 file changed

+11
-1
lines changed
  • vector-stores/spring-ai-chroma-store/src/main/java/org/springframework/ai/chroma/vectorstore

1 file changed

+11
-1
lines changed

vector-stores/spring-ai-chroma-store/src/main/java/org/springframework/ai/chroma/vectorstore/ChromaVectorStore.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.springframework.lang.Nullable;
4949
import org.springframework.util.Assert;
5050
import org.springframework.util.CollectionUtils;
51+
import org.springframework.web.client.HttpClientErrorException;
5152

5253
/**
5354
* {@link ChromaVectorStore} is a concrete implementation of the {@link VectorStore}
@@ -117,7 +118,16 @@ public static Builder builder(ChromaApi chromaApi, EmbeddingModel embeddingModel
117118
@Override
118119
public void afterPropertiesSet() throws Exception {
119120
if (!this.initialized) {
120-
var collection = this.chromaApi.getCollection(this.tenantName, this.databaseName, this.collectionName);
121+
//if the collection doesn't exist, chromaApi.getCollection will throw an exception
122+
ChromaApi.Collection collection = null;
123+
try {
124+
collection = this.chromaApi.getCollection(this.tenantName, this.databaseName, this.collectionName);
125+
}
126+
catch (Exception ex) {
127+
if (!(ex.getCause() instanceof HttpClientErrorException.NotFound)) {
128+
throw ex;
129+
}
130+
}
121131
if (collection == null) {
122132
if (this.initializeSchema) {
123133
var tenant = this.chromaApi.getTenant(this.tenantName);

0 commit comments

Comments
 (0)