{"record":{"id":"30c95eb78e6b3d2e","repo":"spring-projects/spring-ai","slug":"cannot-compare-values-of-types-s-and-s","errorCode":null,"errorMessage":"Cannot compare values of types %s and %s","messagePattern":"Cannot compare values of types (.+?) and (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"spring-ai-vector-store/src/main/java/org/springframework/ai/vectorstore/SimpleVectorStoreFilterExpressionEvaluator.java","lineNumber":209,"sourceCode":"\t\tif (filterVal == null) {\n\t\t\treturn 1;\n\t\t}\n\t\tif (metaVal instanceof Number n1 && filterVal instanceof Number n2) {\n\t\t\treturn Double.compare(n1.doubleValue(), n2.doubleValue());\n\t\t}\n\t\tif (Objects.equals(metaVal, filterVal)) {\n\t\t\treturn 0;\n\t\t}\n\t\tif (metaVal instanceof Comparable comparable && filterVal instanceof Comparable) {\n\t\t\ttry {\n\t\t\t\treturn comparable.compareTo(filterVal);\n\t\t\t}\n\t\t\tcatch (ClassCastException ex) {\n\t\t\t\tthrow new IllegalArgumentException(\"Cannot compare values of incompatible types %s and %s\"\n\t\t\t\t\t.formatted(metaVal.getClass().getName(), filterVal.getClass().getName()), ex);\n\t\t\t}\n\t\t}\n\t\tthrow new IllegalArgumentException(\"Cannot compare values of types %s and %s\"\n\t\t\t.formatted(metaVal.getClass().getName(), filterVal.getClass().getName()));\n\t}\n\n\tprivate List<?> asList(Object value, Filter.Expression expression) {\n\t\tif (value instanceof List<?> list) {\n\t\t\treturn list;\n\t\t}\n\t\tthrow new IllegalArgumentException(\n\t\t\t\t\"Expected a List value for %s expression but got: %s\".formatted(expression.type(), value));\n\t}\n\n}\n","sourceCodeStart":191,"sourceCodeEnd":222,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/spring-ai-vector-store/src/main/java/org/springframework/ai/vectorstore/SimpleVectorStoreFilterExpressionEvaluator.java#L191-L222","documentation":"If either the metadata value or the filter value is not Comparable, the evaluator cannot perform an ordered/inequality comparison and throws IllegalArgumentException 'Cannot compare values of types'. Unlike error 504, this fires without attempting compareTo — the types simply do not support comparison.","triggerScenarios":"Filtering with GT/LT/GTE/LTE (or even EQ) on a metadata value that is null, a List, or a Map — types that are not Comparable.","commonSituations":"Metadata field missing from a document (null) while the filter uses inequality operators; filtering on list-valued metadata with a scalar comparison.","solutions":["Ensure every document carries the metadata key used in filters, with a Comparable value (String, Number, Date).","Use IN/NIN instead of scalar comparisons when metadata values are lists.","Validate document metadata completeness at ingestion time."],"exampleFix":"// before\n// metadata missing 'year' on some docs; filter uses\nnew Filter.Expression(GTE, new Filter.Key(\"year\"), new Filter.Value(2020));\n// after\n// ingest with metadata.put(\"year\", 2020) fallback, or filter\nnew Filter.Expression(IN, new Filter.Key(\"year\"), new Filter.Value(List.of(2020, 2021)));","handlingStrategy":"validation","validationCode":"boolean isComparable(Object v) {\n    return v instanceof Comparable<?>;\n}","typeGuard":"<T extends Comparable<T>> boolean canCompare(Object v, Class<T> type) { return type.isInstance(v); }","tryCatchPattern":"try {\n    vectorStore.similaritySearch(request);\n} catch (IllegalArgumentException ex) {\n    if (ex.getMessage().startsWith(\"Cannot compare values of types\")) {\n        // ensure metadata key exists and is Comparable on all documents\n    } else throw ex;\n}","preventionTips":["Require filter metadata keys on every ingested document","Use IN/NIN for list-valued metadata instead of scalar comparisons"],"tags":["vector-store","filter-expression","type-mismatch","comparison","spring-ai"],"backgroundTag":"type-mismatch","analyzedSha":"98a7beda4f29d80a71c5837eb4053b03a93a46f7","analyzedAt":"2026-09-11T14:15:49.441Z","contentChangedAt":"2026-09-11T14:15:49.441Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}