{"record":{"id":"50d32f43f7d5bab8","repo":"microsoft/semantic-kernel","slug":"addition-in-filter-expressions-is-only-allowed-for","errorCode":null,"errorMessage":"Addition in filter expressions is only allowed for numeric values and same-type sequences.","messagePattern":"Addition in filter expressions is only allowed for numeric values and same-type sequences\\.","errorType":"exception","errorClass":"VectorStoreOperationException","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/connectors/in_memory.py","lineNumber":347,"sourceCode":"        if isinstance(operator_node, ast.Is):\n            return left is right\n        if isinstance(operator_node, ast.IsNot):\n            return left is not right\n        raise VectorStoreOperationException(\n            f\"Comparison operator '{type(operator_node).__name__}' is not allowed in filter expressions.\"\n        )\n\n    def _safe_add(self, left: Any, right: Any) -> Any:\n        \"\"\"Safely evaluate addition.\"\"\"\n        if isinstance(left, (int, float)) and isinstance(right, (int, float)):\n            return left + right\n        if isinstance(left, str) and isinstance(right, str):\n            return self._ensure_sequence_result_size(left, right, lambda a, b: a + b)\n        if isinstance(left, list) and isinstance(right, list):\n            return self._ensure_sequence_result_size(left, right, lambda a, b: a + b)\n        if isinstance(left, tuple) and isinstance(right, tuple):\n            return self._ensure_sequence_result_size(left, right, lambda a, b: a + b)\n        raise VectorStoreOperationException(\n            \"Addition in filter expressions is only allowed for numeric values and same-type sequences.\"\n        )\n\n    def _safe_mult(self, left: Any, right: Any) -> Any:\n        \"\"\"Safely evaluate multiplication.\"\"\"\n        if isinstance(left, (int, float)) and isinstance(right, (int, float)):\n            return left * right\n        if isinstance(left, int) and isinstance(right, (str, list, tuple)):\n            return self._safe_repeat(right, left)\n        if isinstance(right, int) and isinstance(left, (str, list, tuple)):\n            return self._safe_repeat(left, right)\n        raise VectorStoreOperationException(\n            \"Multiplication in filter expressions is only allowed for numeric values and bounded sequence repetition.\"\n        )\n\n    def _safe_repeat(self, value: str | list[Any] | tuple[Any, ...], repeat_count: int) -> Any:\n        \"\"\"Safely repeat a sequence.\"\"\"\n        if repeat_count <= 0 or len(value) == 0:","sourceCodeStart":329,"sourceCodeEnd":365,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/in_memory.py#L329-L365","documentation":"Thrown by _safe_add (in_memory.py:347-348) when the operands of `+` are not both numeric, not both str, not both list, and not both tuple. Parse-time only checks that a BinOp/Add node is allowed; the actual operand value types are only known at evaluation, so mixed-type or unsupported addition (str+int, dict+dict, list+str) fails here.","triggerScenarios":"A filter like `lambda x: x.tags + 1` (list + int), `lambda x: x.count + x.name` (int + str), or `lambda x: x.meta + x.other` where the fields are dicts.","commonSituations":"Assuming filter arithmetic is permissive; field type mismatches between the model and the filter; concatenating a numeric field with a string literal.","solutions":["Make both operands of `+` the same numeric type or the same sequence type (str+str, list+list, tuple+tuple).","Cast operands with the allowlisted builtins int(), float(), str() before adding.","Verify the actual runtime types of the fields used in arithmetic."],"exampleFix":"# before\nVectorSearchOptions(filter=\"lambda x: x.count + x.suffix == 5\")  # int + str\n# after\nVectorSearchOptions(filter=\"lambda x: x.count + int(x.suffix) == 5\")","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    results = await collection.search(search_type=SearchType.VECTOR, options=opts)\nexcept VectorStoreOperationException as e:\n    logger.warning(\"filter rejected: %s\", e.__cause__ or e)\n    results = None","preventionTips":["Confirm field types in the data model before writing arithmetic filters.","Cast with int()/float()/str() when operand types are uncertain."],"tags":["filter","in-memory","semantic-kernel","evaluation","arithmetic"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}