{"record":{"id":"1d25ea623f65ef04","repo":"HumanSignal/label-studio","slug":"expression-command-is-required","errorCode":null,"errorMessage":"Expression command is required.","messagePattern":"Expression command is required\\.","errorType":"validation","errorClass":"ValidationError","httpStatus":400,"severity":"error","filePath":"label_studio/data_manager/actions/data_columns.py","lineNumber":278,"sourceCode":"        start = end + params[end:].find('[') + 1\n    return params\n\n\nadd_data_field_examples = (\n    'range(2) or '\n    'sample() or '\n    'random(<min_int>, <max_int>) or '\n    'choices([\"<value1>\", \"<value2>\", ...], [<weight1>, <weight2>, ...]) or '\n    'replace(\"old-string\", \"new-string\")'\n)\n\n\ndef add_expression(queryset, size, value, value_name):\n    if '(' not in value or not value.endswith(')'):\n        raise ValidationError({'value': 'Expression must use the form command(arguments).'})\n    command, args = value.split('(', 1)\n    if not command:\n        raise ValidationError({'value': 'Expression command is required.'})\n    args = process_arrays(args)\n    args = args.replace(')', '').split(',')\n    args = [] if len(args) == 1 and args[0] == '' else args\n    for i, arg in enumerate(args):\n        args[i] = arg.replace(';', ',').replace(\"'\", '\"')\n\n    sample_values = None\n\n    if command == 'range':\n        if len(args) != 1:\n            raise ValidationError({'value': 'range(start:int) requires one start argument.'})\n        start = int(args[0])\n\n    elif command == 'sample':\n        if args:\n            raise ValidationError({'value': 'sample() does not accept arguments.'})\n        sample_values = iter(random.sample(range(0, size), size))\n","sourceCodeStart":260,"sourceCodeEnd":296,"githubUrl":"https://github.com/HumanSignal/label-studio/blob/0b49e9b53917880baf1dd85d574fe5541a9aafb2/label_studio/data_manager/actions/data_columns.py#L260-L296","documentation":"After splitting an expression value on the first '(', add_expression checks the command part is non-empty. An empty command (value like '(args)' or '(...') raises ValidationError {'value': 'Expression command is required.'}.","triggerScenarios":"Submitting an Expression whose value starts with '(' e.g. '(5)', or a value like '(...)' where the text before '(' is empty/whitespace trimmed away.","commonSituations":"User pastes only the argument list without the command; string manipulation strips the command prefix; malformed template output.","solutions":["Prefix the expression with a valid command name, e.g. 'range(5)' instead of '(5)'.","Validate the expression client-side before submission.","Rebuild the expression from the supported command list (range, sample, replace, etc.)."],"exampleFix":"// before\nvalue = '(5)'\n// after\nvalue = 'range(5)'","handlingStrategy":"validation","validationCode":"cmd = value.split('(', 1)[0] if '(' in value else ''\nassert cmd.strip(), 'expression needs a command name before the parenthesis'","typeGuard":"def has_expression_command(value):\n    return isinstance(value, str) and value.split('(', 1)[0].strip() != '' if '(' in value else False","tryCatchPattern":"from rest_framework.exceptions import ValidationError\ntry:\n    add_data_field(project, qs, request=request)\nexcept ValidationError as e:\n    if 'Expression command is required' in str(e):\n        prepend_default_command()","preventionTips":["Validate the command prefix client-side.","Escape/avoid preprocessing that strips text before '('.","Pick commands from a fixed list (range, sample, replace, ...) in the UI."],"tags":["django","validation","data-manager","expression"],"backgroundTag":"expression-syntax-error","analyzedSha":"0b49e9b53917880baf1dd85d574fe5541a9aafb2","analyzedAt":"2026-08-29T00:39:52.578Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}