Skip to content

Commit 64cf88a

Browse files
authored
Remove @Nullable from VectorStore similaritySearch (#3613)
The VectorStore#similaritySearch methods never return null, but there's a data flow issue in AbstractObservationVectorStore caused by Micrometer's Observation#observe being marked as @nullable. The returned value is actually controlled by Spring AI's supplier, which never returns null. Fixes #3613 Auto-cherry-pick to 1.0.x Signed-off-by: Filip Hrisafov <[email protected]>
1 parent e86ce73 commit 64cf88a

File tree

5 files changed

+6
-12
lines changed

5 files changed

+6
-12
lines changed

spring-ai-vector-store/src/main/java/org/springframework/ai/vectorstore/VectorStore.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.springframework.ai.vectorstore.filter.Filter;
2828
import org.springframework.ai.vectorstore.observation.DefaultVectorStoreObservationConvention;
2929
import org.springframework.ai.vectorstore.observation.VectorStoreObservationConvention;
30-
import org.springframework.lang.Nullable;
3130
import org.springframework.util.Assert;
3231

3332
/**
@@ -91,7 +90,6 @@ default void delete(String filterExpression) {
9190
* topK, similarity threshold and metadata filter expressions.
9291
* @return Returns documents th match the query request conditions.
9392
*/
94-
@Nullable
9593
List<Document> similaritySearch(SearchRequest request);
9694

9795
/**
@@ -101,7 +99,6 @@ default void delete(String filterExpression) {
10199
* @return Returns a list of documents that have embeddings similar to the query text
102100
* embedding.
103101
*/
104-
@Nullable
105102
default List<Document> similaritySearch(String query) {
106103
return this.similaritySearch(SearchRequest.builder().query(query).build());
107104
}

spring-ai-vector-store/src/main/java/org/springframework/ai/vectorstore/observation/AbstractObservationVectorStore.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ public void delete(Filter.Expression filterExpression) {
111111
}
112112

113113
@Override
114-
@Nullable
114+
// Micrometer Observation#observe returns the value of the Supplier, which is never
115+
// null
116+
@SuppressWarnings("DataFlowIssue")
115117
public List<Document> similaritySearch(SearchRequest request) {
116118

117119
VectorStoreObservationContext searchObservationContext = this

vector-stores/spring-ai-azure-cosmos-db-store/src/main/java/org/springframework/ai/vectorstore/cosmosdb/CosmosDBVectorStore.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -434,15 +434,13 @@ public List<Document> doSimilaritySearch(SearchRequest request) {
434434
}
435435

436436
// Convert JsonNode to Document
437-
List<Document> docs = documents.stream()
437+
return documents.stream()
438438
.map(doc -> Document.builder()
439439
.id(doc.get("id").asText())
440440
.text(doc.get("content").asText())
441441
.metadata(docFields)
442442
.build())
443443
.collect(Collectors.toList());
444-
445-
return docs != null ? docs : List.of();
446444
}
447445
catch (Exception e) {
448446
logger.error("Error during similarity search: {}", e.getMessage());

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
import org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore;
4646
import org.springframework.ai.vectorstore.observation.VectorStoreObservationContext;
4747
import org.springframework.beans.factory.InitializingBean;
48-
import org.springframework.lang.NonNull;
4948
import org.springframework.lang.Nullable;
5049
import org.springframework.util.Assert;
5150
import org.springframework.util.CollectionUtils;
@@ -148,7 +147,7 @@ public void afterPropertiesSet() throws Exception {
148147
}
149148

150149
@Override
151-
public void doAdd(@NonNull List<Document> documents) {
150+
public void doAdd(List<Document> documents) {
152151
Assert.notNull(documents, "Documents must not be null");
153152
if (CollectionUtils.isEmpty(documents)) {
154153
return;
@@ -202,8 +201,7 @@ protected void doDelete(Filter.Expression expression) {
202201
}
203202

204203
@Override
205-
@NonNull
206-
public List<Document> doSimilaritySearch(@NonNull SearchRequest request) {
204+
public List<Document> doSimilaritySearch(SearchRequest request) {
207205

208206
String query = request.getQuery();
209207
Assert.notNull(query, "Query string must not be null");

vector-stores/spring-ai-gemfire-store/src/main/java/org/springframework/ai/vectorstore/gemfire/GemFireVectorStore.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,6 @@ public void doDelete(List<String> idList) {
243243
}
244244

245245
@Override
246-
@Nullable
247246
public List<Document> doSimilaritySearch(SearchRequest request) {
248247
if (request.hasFilterExpression()) {
249248
throw new UnsupportedOperationException("GemFire currently does not support metadata filter expressions.");

0 commit comments

Comments
 (0)