{"record":{"id":"f07a8fe9ae9290ff","repo":"HumanSignal/label-studio","slug":"replace-old-value-new-value-requires-two-argumen","errorCode":null,"errorMessage":"replace(old_value, new_value) requires two arguments.","messagePattern":"replace\\(old_value, new_value\\) requires two arguments\\.","errorType":"validation","errorClass":"ValidationError","httpStatus":400,"severity":"error","filePath":"label_studio/data_manager/actions/data_columns.py","lineNumber":310,"sourceCode":"    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\n    elif command == 'random':\n        if len(args) != 2:\n            raise ValidationError({'value': 'random(min, max) requires two arguments.'})\n        minimum, maximum = int(args[0]), int(args[1])\n\n    elif command == 'choices':\n        if not 0 < len(args) < 3:\n            raise ValidationError({'value': 'choices(values:list, weights:list) requires one or two arguments.'})\n        weights = json.loads(args[1]) if len(args) == 2 else None\n        choices = json.loads(args[0])\n\n    elif command == 'replace':\n        if len(args) != 2:\n            raise ValidationError({'value': 'replace(old_value, new_value) requires two arguments.'})\n        old_value, new_value = json.loads(args[0]), json.loads(args[1])\n\n    else:\n        raise ValidationError('Undefined expression, you can use: ' + add_data_field_examples)\n\n    offset = 0\n    task_iterator = iterate_queryset(queryset.only('id', 'data'), chunk_size=settings.UPDATE_COLUMN_BATCH_SIZE)\n    for task_batch in batched_iterator(task_iterator, settings.UPDATE_COLUMN_BATCH_SIZE):\n        if command == 'range':\n            for index, task in enumerate(task_batch):\n                task.data[value_name] = start + offset + index\n        elif command == 'sample':\n            for task in task_batch:\n                task.data[value_name] = next(sample_values)\n        elif command == 'random':\n            for task in task_batch:\n                task.data[value_name] = random.randint(minimum, maximum)\n        elif command == 'choices':","sourceCodeStart":292,"sourceCodeEnd":328,"githubUrl":"https://github.com/HumanSignal/label-studio/blob/0b49e9b53917880baf1dd85d574fe5541a9aafb2/label_studio/data_manager/actions/data_columns.py#L292-L328","documentation":"The DataManager 'replace' expression substitutes occurrences of old_value with new_value in a column. add_expression enforces exactly two JSON-encoded arguments and raises this ValidationError when len(args) != 2.","triggerScenarios":"Calling the add-data-field action with {\"command\": \"replace\", \"args\": [\"foo\"]} (missing new value), args: [], or three+ args via the column mutation action.","commonSituations":"Developers pass only the old value assuming a delete-replace; arguments not JSON-encoded (plain strings like foo instead of \"foo\") so they are dropped or malformed; UI sending empty optional fields.","solutions":["Provide exactly two JSON-encoded arguments: {\"args\": [\"\\\"foo\\\"\", \"\\\"bar\\\"\"]}","To replace with empty string, pass the JSON string \"\" as new_value rather than omitting the argument","Ensure old_value and new_value round-trip through json.loads (quoted strings, numbers, etc.)"],"exampleFix":"// before\n{\"command\": \"replace\", \"args\": [\"\\\"foo\\\"\"]}\n// after\n{\"command\": \"replace\", \"args\": [\"\\\"foo\\\"\", \"\\\"bar\\\"\"]}","handlingStrategy":"validation","validationCode":"if (expr.command === 'replace' && (!Array.isArray(expr.args) || expr.args.length !== 2)) {\n  throw new Error('replace requires exactly [old_value, new_value]');\n}","typeGuard":"function isValidReplace(e) { return e.command === 'replace' && Array.isArray(e.args) && e.args.length === 2; }","tryCatchPattern":"try {\n  await dm.addAction({id: 'add_data_field', expression: expr});\n} catch (e) {\n  if (String(e).includes('replace(old_value, new_value) requires two')) {\n    expr.args = [JSON.stringify(oldV), JSON.stringify(newV)];\n    return retry();\n  }\n  throw e;\n}","preventionTips":["Always JSON-stringify both old_value and new_value args","For deletion-like replaces pass \"\" as new_value instead of omitting it","Centralize payload construction so arg counts stay consistent"],"tags":["label-studio","data-manager","validation","api"],"backgroundTag":"invalid-arguments","analyzedSha":"0b49e9b53917880baf1dd85d574fe5541a9aafb2","analyzedAt":"2026-08-29T00:39:52.578Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}