{"record":{"id":"10682808143c806f","repo":"microsoft/semantic-kernel","slug":"constants-or-variables-are-not-supported-use-a-va","errorCode":null,"errorMessage":"Constants or variables are not supported, use a value or attribute.","messagePattern":"Constants or variables are not supported, use a value or attribute\\.","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/connectors/azure_cosmos_db.py","lineNumber":955,"sourceCode":"                        case ast.Not():\n                            return f\"NOT ({parse(node.operand)})\"\n                        case ast.UAdd():\n                            return f\"+{parse(node.operand)}\"\n                        case ast.USub():\n                            return f\"-{parse(node.operand)}\"\n                        case ast.Invert():\n                            raise NotImplementedError(\"Invert operation is not supported.\")\n                    raise NotImplementedError(f\"Unsupported unary operator: {type(node.op)}\")\n                case ast.Attribute():\n                    # Cosmos DB: c.field_name\n                    if node.attr not in self.definition.storage_names:\n                        raise VectorStoreOperationException(\n                            f\"Field '{node.attr}' not in data model (storage property names are used).\"\n                        )\n                    return f\"c.{node.attr}\"\n                case ast.Name():\n                    # Could be a variable or constant; not supported\n                    raise NotImplementedError(\"Constants or variables are not supported, use a value or attribute.\")\n                case ast.Constant():\n                    # Bind strings as query parameters to avoid SQL injection. Numbers and null\n                    # cannot carry injection, so they are inlined.\n                    if isinstance(node.value, str):\n                        name = f\"@filter_p{len(parameters)}\"\n                        parameters.append({\"name\": name, \"value\": node.value})\n                        return name\n                    if isinstance(node.value, (float, int)):\n                        return str(node.value)\n                    if node.value is None:\n                        return \"null\"\n                    raise NotImplementedError(f\"Unsupported constant type: {type(node.value)}\")\n            raise NotImplementedError(f\"Unsupported AST node: {type(node)}\")\n\n        return parse(node), parameters\n\n    @override\n    def _get_record_from_result(self, result: dict[str, Any]) -> dict[str, Any]:","sourceCodeStart":937,"sourceCodeEnd":973,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/azure_cosmos_db.py#L937-L973","documentation":"The AST translator rejects bare names (ast.Name nodes) entirely with NotImplementedError. In a filter lambda, a bare Name is a variable/identifier reference (e.g. an unquoted parameter, an imported constant, or the lambda parameter used on the value side), none of which Cosmos SQL parameter binding supports in this design. Only attribute accesses (c.field) and literal constants are allowed.","triggerScenarios":"Writing a filter lambda where the value side is a bare identifier instead of a literal, e.g. lambda x: x.category == CATEGORY where CATEGORY is an outer variable, or referencing the lambda parameter incorrectly. Such nodes compile to ast.Name and hit azure_cosmos_db.py:953-955.","commonSituations":"Capturing an outer-scope variable in the lambda (closure) rather than inlining its literal value; or writing the comparison operands in the wrong order so a name appears where a constant is expected.","solutions":["Inline literal values in the filter lambda (e.g. x.category == \"news\") instead of referencing outer variables; the constant gets bound as a query parameter automatically.","If you must parameterize, construct the lambda with the literal value substituted in (e.g. via a default argument: lambda x, v=category: x.category == v) — verify the AST still resolves to a Constant, or refactor to build the filter another way.","Ensure attribute accesses appear on the model side and constants on the value side of comparisons."],"exampleFix":"// before\ncat = \"news\"\noptions = VectorSearchOptions(filter=lambda x: x.category == cat)  # cat is an ast.Name\n// after\noptions = VectorSearchOptions(filter=lambda x: x.category == \"news\")  # literal Constant\n","handlingStrategy":"validation","validationCode":"import ast\ntree = ast.parse(filter_src, mode=\"eval\")\nfor node in ast.walk(tree):\n    if isinstance(node, ast.Name):\n        raise ValueError(\"Filter contains a bare variable/constant reference; inline literal values\")","typeGuard":"def has_no_bare_name(node: ast.AST) -> bool:\n    return not any(isinstance(n, ast.Name) for n in ast.walk(node))","tryCatchPattern":null,"preventionTips":["Inline literal constants in filter lambdas instead of capturing outer variables.","Keep the model attribute on the left and a literal on the right of comparisons."],"tags":["azure-cosmos-db","filter","ast","lambda"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}