microsoft/semantic-kernel · error · VectorStoreOperationException

Error evaluating subscript access: {e}

Error message

Error evaluating subscript access: {e}

What it means

_eval_Subscript evaluates value[slice_value]; if that indexing raises (KeyError, IndexError, TypeError) it is caught and re-raised as VectorStoreOperationException with the original message embedded. This covers dict lookups by a missing key, indexing a non-indexable value, or invalid slice operands.

Source

Thrown at python/semantic_kernel/connectors/in_memory.py:179

            raise VectorStoreOperationException(
                f"Access to attribute '{node.attr}' is not allowed in filter expressions."
            )
        value = self.evaluate(node.value, context)
        try:
            return ReadOnlyAttributeDict._wrap_value(getattr(value, node.attr))
        except AttributeError as e:
            raise VectorStoreOperationException(
                f"Attribute '{node.attr}' is not available in filter expressions."
            ) from e

    def _eval_Subscript(self, node: ast.Subscript, context: Mapping[str, Any]) -> Any:
        """Evaluate an index or slice operation."""
        value = self.evaluate(node.value, context)
        slice_value = self.evaluate(node.slice, context)
        try:
            return ReadOnlyAttributeDict._wrap_value(value[slice_value])
        except Exception as e:
            raise VectorStoreOperationException(f"Error evaluating subscript access: {e}") from e

    def _eval_Slice(self, node: ast.Slice, context: Mapping[str, Any]) -> slice:
        """Evaluate a slice node."""
        lower = self._evaluate_optional(node.lower, context)
        upper = self._evaluate_optional(node.upper, context)
        step = self._evaluate_optional(node.step, context)
        return slice(lower, upper, step)

    def _eval_List(self, node: ast.List, context: Mapping[str, Any]) -> list[Any]:
        """Evaluate a list literal."""
        self._ensure_literal_collection_size(len(node.elts))
        return [self.evaluate(element, context) for element in node.elts]

    def _eval_Tuple(self, node: ast.Tuple, context: Mapping[str, Any]) -> tuple[Any, ...]:
        """Evaluate a tuple literal."""
        self._ensure_literal_collection_size(len(node.elts))
        return tuple(self.evaluate(element, context) for element in node.elts)

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Use a valid key that exists on the record, or switch to attribute access (x.field).
  2. Guard with x.get('key') or 'key' in x before subscripting.
  3. Confirm the field's runtime type matches what the subscript assumes.

Example fix

# before
await collection.search(vector=[...], options=VectorSearchOptions(filter=lambda x: x['nope'] == 1))  # -> [1312]

# after
await collection.search(vector=[...], options=VectorSearchOptions(filter=lambda x: x.get('real_key') == 1))
Defensive patterns

Strategy: validation

Validate before calling

def prefer_safe_subscript(filter_str: str) -> None:
    import ast
    for node in ast.walk(ast.parse(filter_str, mode='eval')):
        if isinstance(node, ast.Subscript):
            raise ValueError("prefer x.get('key') over x['key'] in filters to avoid missing-key errors")

Try / catch

from semantic_kernel.exceptions import VectorStoreOperationException
try:
    await collection.search(vector=[...], options=opts)
except VectorStoreOperationException as ex:
    if 'Error evaluating subscript access' in str(ex):
        opts.filter = "lambda x: x.get('key') == value"
        await collection.search(vector=[...], options=opts)
    else:
        raise

Prevention

When it happens

Trigger: A filter like lambda x: x['missing'] == 1; subscripting a scalar (lambda x: x.count[0]); a slice on a non-sliceable value; a key whose type does not match the dict's key type.

Common situations: Dict-style access with the wrong key; mixing attribute and subscript styles; expecting a list where the field is a scalar; inconsistent field types across records.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/ff06b794f05b84b4. Report an issue: GitHub.