{"record":{"id":"8fce65d9082ec3a8","repo":"microsoft/semantic-kernel","slug":"string-literals-in-filter-expressions-exceed-the-m","errorCode":null,"errorMessage":"String literals in filter expressions exceed the maximum allowed size.","messagePattern":"String literals in filter expressions exceed the maximum allowed size\\.","errorType":"exception","errorClass":"VectorStoreOperationException","httpStatus":null,"severity":"warning","filePath":"python/semantic_kernel/connectors/in_memory.py","lineNumber":147,"sourceCode":"        self._direct_call_functions = direct_call_functions\n        self._blocked_attributes = blocked_attributes\n        self._max_literal_collection_size = max_literal_collection_size\n        self._max_sequence_repeat_size = max_sequence_repeat_size\n\n    def evaluate(self, node: ast.AST, context: Mapping[str, Any]) -> Any:\n        \"\"\"Evaluate a supported AST node.\"\"\"\n        evaluator = getattr(self, f\"_eval_{type(node).__name__}\", None)\n        if evaluator is None:\n            raise VectorStoreOperationException(\n                f\"AST node type '{type(node).__name__}' is not supported during filter evaluation.\"\n            )\n        return evaluator(node, context)\n\n    def _eval_Constant(self, node: ast.Constant, context: Mapping[str, Any]) -> Any:\n        \"\"\"Evaluate a constant literal.\"\"\"\n        del context\n        if isinstance(node.value, str) and len(node.value) > self._max_literal_collection_size:\n            raise VectorStoreOperationException(\n                \"String literals in filter expressions exceed the maximum allowed size.\"\n            )\n        return node.value\n\n    def _eval_Name(self, node: ast.Name, context: Mapping[str, Any]) -> Any:\n        \"\"\"Evaluate a variable reference.\"\"\"\n        if node.id not in context:\n            raise VectorStoreOperationException(f\"Use of name '{node.id}' is not allowed in filter expressions.\")\n        return context[node.id]\n\n    def _eval_Attribute(self, node: ast.Attribute, context: Mapping[str, Any]) -> Any:\n        \"\"\"Evaluate an attribute access.\"\"\"\n        if node.attr in self._blocked_attributes:\n            raise VectorStoreOperationException(\n                f\"Access to attribute '{node.attr}' is not allowed in filter expressions.\"\n            )\n        value = self.evaluate(node.value, context)\n        try:","sourceCodeStart":129,"sourceCodeEnd":165,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/in_memory.py#L129-L165","documentation":"When _eval_Constant evaluates a string literal whose length exceeds the evaluator's max_literal_collection_size (default 256), it raises VectorStoreOperationException. This mirrors the same check performed during static parsing, so in normal use the static check fires first; the evaluator check is a defense-in-depth net that triggers when the evaluator instance was configured with a smaller cap than the parser.","triggerScenarios":"A string filter lambda containing a literal longer than 256 chars, e.g. lambda x: x.desc == '<very long string>', especially when the literal is built dynamically from user input.","commonSituations":"Embedding a large blob/document text into a filter; comparing against a long URL or payload; generating filters from unbounded user input.","solutions":["Shorten the literal, or compare against a field value rather than a giant inline constant.","Raise max_filter_literal_collection_size on the collection instance so the parser and evaluator share the larger limit.","Pre-filter large values in application code instead of inside the filter expression."],"exampleFix":"# before\ncollection = MyCollection(...)\nawait collection.search(vector=[...], options=VectorSearchOptions(\n    filter=lambda x: x.text == 'A' * 1000))  # -> [1308]\n\n# after\ncollection.max_filter_literal_collection_size = 4096\n# or shorten / pre-filter the value before building the filter","handlingStrategy":"validation","validationCode":"def check_string_literals(filter_str: str, limit: int) -> None:\n    import ast\n    for node in ast.walk(ast.parse(filter_str, mode='eval')):\n        if isinstance(node, ast.Constant) and isinstance(node.value, str) and len(node.value) > limit:\n            raise ValueError(f\"string literal exceeds limit {limit}: {len(node.value)} chars\")","typeGuard":null,"tryCatchPattern":"from semantic_kernel.exceptions import VectorStoreOperationException\ntry:\n    await collection.search(vector=[...], options=opts)\nexcept VectorStoreOperationException as ex:\n    if 'exceed the maximum allowed size' in str(ex):\n        collection.max_filter_literal_collection_size = 4096\n        await collection.search(vector=[...], options=opts)\n    else:\n        raise","preventionTips":["Keep inline string literals short; move large values out of the filter.","If large literals are legitimate, raise max_filter_literal_collection_size on the collection."],"tags":["in-memory","filter","limits"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}