HumanSignal/label-studio · error · ValidationError

Expression command is required.

Error message

Expression command is required.

What it means

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.'}.

Source

Thrown at label_studio/data_manager/actions/data_columns.py:278

        start = end + params[end:].find('[') + 1
    return params


add_data_field_examples = (
    'range(2) or '
    'sample() or '
    'random(<min_int>, <max_int>) or '
    'choices(["<value1>", "<value2>", ...], [<weight1>, <weight2>, ...]) or '
    'replace("old-string", "new-string")'
)


def add_expression(queryset, size, value, value_name):
    if '(' not in value or not value.endswith(')'):
        raise ValidationError({'value': 'Expression must use the form command(arguments).'})
    command, args = value.split('(', 1)
    if not command:
        raise ValidationError({'value': 'Expression command is required.'})
    args = process_arrays(args)
    args = args.replace(')', '').split(',')
    args = [] if len(args) == 1 and args[0] == '' else args
    for i, arg in enumerate(args):
        args[i] = arg.replace(';', ',').replace("'", '"')

    sample_values = None

    if command == 'range':
        if len(args) != 1:
            raise ValidationError({'value': 'range(start:int) requires one start argument.'})
        start = int(args[0])

    elif command == 'sample':
        if args:
            raise ValidationError({'value': 'sample() does not accept arguments.'})
        sample_values = iter(random.sample(range(0, size), size))

View on GitHub (pinned to 0b49e9b539)

Solutions

  1. Prefix the expression with a valid command name, e.g. 'range(5)' instead of '(5)'.
  2. Validate the expression client-side before submission.
  3. Rebuild the expression from the supported command list (range, sample, replace, etc.).

Example fix

// before
value = '(5)'
// after
value = 'range(5)'
Defensive patterns

Strategy: validation

Validate before calling

cmd = value.split('(', 1)[0] if '(' in value else ''
assert cmd.strip(), 'expression needs a command name before the parenthesis'

Type guard

def has_expression_command(value):
    return isinstance(value, str) and value.split('(', 1)[0].strip() != '' if '(' in value else False

Try / catch

from rest_framework.exceptions import ValidationError
try:
    add_data_field(project, qs, request=request)
except ValidationError as e:
    if 'Expression command is required' in str(e):
        prepend_default_command()

Prevention

When it happens

Trigger: Submitting an Expression whose value starts with '(' e.g. '(5)', or a value like '(...)' where the text before '(' is empty/whitespace trimmed away.

Common situations: User pastes only the argument list without the command; string manipulation strips the command prefix; malformed template output.

Related errors


AI-assisted analysis of HumanSignal/label-studio@0b49e9b539 (2026-08-29). Data as JSON: /api/errors/1d25ea623f65ef04. Report an issue: GitHub.