microsoft/semantic-kernel · error · NotImplementedError
Constants are not supported, make sure to use a value or a a
Error message
Constants are not supported, make sure to use a value or a attribute.
What it means
Raised in the ast.Name branch of _lambda_parser when the AST contains a bare variable reference (ast.Name) that is not part of an attribute chain. A standalone name in a filter (other than the implicit lambda-parameter root, which is skipped inside attribute chains) cannot be translated to an OData field path or value, so NotImplementedError is raised with a hint to use a value or an attribute.
Source
Thrown at python/semantic_kernel/connectors/azure_ai_search.py:718
raise NotImplementedError(f"Unsupported operator: {type(op)}")
case ast.BoolOp():
op_str = "and" if isinstance(node.op, ast.And) else "or"
return "(" + f" {op_str} ".join([self._lambda_parser(v) for v in node.values]) + ")"
case ast.UnaryOp():
match node.op:
case ast.UAdd():
return f"+{self._lambda_parser(node.operand)}"
case ast.USub():
return f"-{self._lambda_parser(node.operand)}"
case ast.Invert():
raise NotImplementedError("Invert operation is not supported.")
case ast.Not():
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 resultView on GitHub (pinned to c028a0c7dc)
Solutions
- Use literal constant values (numbers, strings) directly in the filter lambda instead of referencing variables by name.
- Reference fields as x.field (attribute chains starting at the lambda parameter), not as bare names.
- If you need a dynamic value, substitute it into the lambda as a literal before passing to the filter.
Example fix
// before threshold = 10 options.filter = lambda x: x.count > threshold # 'threshold' is a Name // after options.filter = lambda x: x.count > 10 # literal Constant
Defensive patterns
Strategy: validation
Validate before calling
import ast
def filter_no_bare_names(filter_lambda) -> None:
tree = ast.parse(ast.getsource(filter_lambda), mode="exec")
for node in ast.walk(tree):
if isinstance(node, ast.Name):
# only the lambda param root inside attribute chains is allowed;
# bare names as comparators are not
assert node.id == "x", (
f"Bare name '{node.id}' is not supported; use a literal constant or x.field"
)
filter_no_bare_names(opts.filter) Try / catch
try:
res = await collection.search(values=q, options=opts)
except NotImplementedError as e:
if "Constants are not supported" in str(e):
opts.filter = lambda x: x.count > 10 # literal instead of variable ref
res = await collection.search(values=q, options=opts)
raise Prevention
- Use literal constants (numbers/strings) on the right-hand side of comparisons.
- Reference fields as x.field attribute chains, not bare names.
- Substitute dynamic values into the lambda as literals before filtering.
When it happens
Trigger: Writing a filter lambda that references a bare variable on the right-hand side that the parser encounters as an ast.Name, e.g. lambda x: x.field == threshold where 'threshold' is resolved as a Name rather than a Constant (this happens with certain AST shapes / non-literal references). Also lambda x: x == y where y is a Name.
Common situations: Referencing an outer-scope variable by name instead of a literal/constant in the filter; using a bare variable where the parser expects a constant value or an x.field attribute.
Related errors
- Unsupported attribute chain root: {type(current)}
- Unsupported operator: {type(op)}
- Invert operation is not supported.
- Unsupported constant type: {type(value)}
- Field '{top_level}' not in data model (storage property name
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/3850768a226d50b1.
Report an issue: GitHub.