microsoft/semantic-kernel · error · VectorStoreOperationException

Unsupported constant type: {type(value)}

Error message

Unsupported constant type: {type(value)}

What it means

Raised in the ast.Constant branch of _lambda_parser when the constant's value is not one of the handled types (str, bytes, bool, None, int, float). The parser can emit OData literals for those primitives, but any other Python type held in an ast.Constant (e.g. complex, or an enum/object that the AST represents as a constant) cannot be serialized to OData, so a VectorStoreOperationException is raised.

Source

Thrown at python/semantic_kernel/connectors/azure_ai_search.py:731

                        return f"not {self._lambda_parser(node.operand)}"
            case ast.Attribute():
                # Support nested property chains
                return _parse_attribute_chain(node)
            case ast.Name():
                raise NotImplementedError("Constants are not supported, make sure to use a value or a attribute.")
            case ast.Constant():
                value = node.value
                if isinstance(value, str):
                    return "'" + value.replace("'", "''") + "'"
                if isinstance(value, bytes):
                    return "'" + value.decode("utf-8").replace("'", "''") + "'"
                if isinstance(value, bool):
                    return str(value).lower()
                if value is None:
                    return "null"
                if isinstance(value, (int, float)):
                    return str(value)
                raise VectorStoreOperationException(f"Unsupported constant type: {type(value)}")
        raise NotImplementedError(f"Unsupported AST node: {type(node)}")

    @override
    def _get_record_from_result(self, result: dict[str, Any]) -> dict[str, Any]:
        return result

    @override
    def _get_score_from_result(self, result: dict[str, Any]) -> float | None:
        return result.get("@search.score")

    @override
    async def __aexit__(self, exc_type, exc_value, traceback) -> None:
        """Exit the context manager."""
        if self.managed_client:
            await self.search_client.close()
        if self.managed_search_index_client:
            await self.search_index_client.close()

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Use only primitive literal types in filter comparisons: str, int, float, bool, None (and bytes for binary).
  2. Convert enums to their underlying primitive value (e.g. MyEnum.X.value) before using in the filter.
  3. For complex/datetime values, compare against an ISO-format string constant instead.

Example fix

// before
options.filter = lambda x: x.color == Color.RED   # Color.RED is not a primitive

// after
options.filter = lambda x: x.color == 'red'   # primitive str constant
Defensive patterns

Strategy: validation

Validate before calling

import ast

def filter_constants_are_primitives(filter_lambda) -> None:
    tree = ast.parse(ast.getsource(filter_lambda), mode="exec")
    for node in ast.walk(tree):
        if isinstance(node, ast.Constant):
            assert isinstance(node.value, (str, int, float, bool, type(None), bytes)), \
                f"Unsupported constant type: {type(node.value).__name__}"

filter_constants_are_primitives(opts.filter)

Try / catch

from semantic_kernel.exceptions import VectorStoreOperationException
try:
    res = await collection.search(values=q, options=opts)
except VectorStoreOperationException as e:
    if "Unsupported constant type" in str(e):
        opts.filter = lambda x: x.color == 'red'  # primitive instead of enum/object
        res = await collection.search(values=q, options=opts)
    raise

Prevention

When it happens

Trigger: Using a constant of an unsupported type in a filter lambda — e.g. lambda x: x.value == 3j (complex), or a constant that evaluates to a type the parser does not recognize. Less common in normal code, but can arise with frozen dataclasses/enums that compile into unusual constant nodes.

Common situations: Comparing a field against a complex number; using an object/enum instance as a literal that the AST surfaces as a non-primitive constant; edge cases from AST produced by decorators or codegen.

Related errors


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