{"record":{"id":"e7f4f9060620406f","repo":"microsoft/semantic-kernel","slug":"sequence-repetition-in-filter-expressions-exceeds","errorCode":null,"errorMessage":"Sequence repetition in filter expressions exceeds the maximum allowed size.","messagePattern":"Sequence repetition in filter expressions exceeds the maximum allowed size\\.","errorType":"exception","errorClass":"VectorStoreOperationException","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/connectors/in_memory.py","lineNumber":368,"sourceCode":"\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:\n            return value * repeat_count\n        if len(value) > self._max_sequence_repeat_size // repeat_count:\n            raise VectorStoreOperationException(\n                \"Sequence repetition in filter expressions exceeds the maximum allowed size.\"\n            )\n        return value * repeat_count\n\n    def _safe_numeric_operation(\n        self,\n        operator_node: ast.AST,\n        left: Any,\n        right: Any,\n        operation: Callable[[float | int, float | int], Any],\n    ) -> Any:\n        \"\"\"Safely evaluate a numeric binary operation.\"\"\"\n        if not isinstance(left, (int, float)) or not isinstance(right, (int, float)):\n            raise VectorStoreOperationException(\n                f\"Operator '{type(operator_node).__name__}' is only allowed for numeric values in filter expressions.\"\n            )\n        return operation(left, right)\n","sourceCodeStart":350,"sourceCodeEnd":386,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/in_memory.py#L350-L386","documentation":"Thrown by _safe_repeat (in_memory.py:367-370) when `seq * n` would exceed the cap: len(seq) > max_filter_sequence_repeat_size // n. Default cap is 1024. This prevents memory-exhaustion via repetition in filter expressions; the bound depends on actual runtime value lengths so it cannot be fully checked at parse time.","triggerScenarios":"A filter like `lambda x: x.name * 1000` where name is a non-empty string, or `lambda x: x.tags * 500` with a sizable list, producing a result larger than 1024 elements.","commonSituations":"Building large padded strings/lists inside a filter; legitimate but oversized repetition needs.","solutions":["Reduce the repetition count or the source sequence length.","Raise max_filter_sequence_repeat_size on the collection instance if the repetition is legitimate.","Avoid repetition in filters entirely; precompute and compare against a stored field instead."],"exampleFix":"# before\ncollection.max_filter_sequence_repeat_size = 1024  # default\nVectorSearchOptions(filter=\"lambda x: x.name * 10000 == x.target\")\n# after\ncollection.max_filter_sequence_repeat_size = 50_000\n# or avoid repetition:\nVectorSearchOptions(filter=\"lambda x: x.name == x.target[:len(x.name)]\")","handlingStrategy":"validation","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":["Do not build large repeated sequences inside filters.","Tune max_filter_sequence_repeat_size deliberately, not as a workaround."],"tags":["filter","in-memory","resource-limits","semantic-kernel","evaluation"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}