gchq/CyberChef · error · OperationError

Incorrect number of samples. Check your input and/or delimit

Error message

Incorrect number of samples. Check your input and/or delimiter.

What it means

Thrown by the Levenshtein Distance operation when the input, split on the configured 'Sample delimiter' argument, does not produce exactly two samples. The operation compares exactly two strings, so it cannot proceed without a clear source and destination. The check is `samples.length !== 2` on line 59-61 of src/core/operations/LevenshteinDistance.mjs.

Source

Thrown at src/core/operations/LevenshteinDistance.mjs:60

            },
            {
                name: "Substitution cost",
                type: "number",
                value: 1
            },
        ];
    }

    /**
     * @param {string} input
     * @param {Object[]} args
     * @returns {number}
     */
    run(input, args) {
        const [delim, insCost, delCost, subCost] = args;
        const samples = input.split(delim);
        if (samples.length !== 2) {
            throw new OperationError("Incorrect number of samples. Check your input and/or delimiter.");
        }
        if (insCost < 0 || delCost < 0 || subCost < 0) {
            throw new OperationError("Negative costs are not allowed.");
        }
        const src = samples[0], dest = samples[1];
        let currentCost = new Array(src.length + 1);
        let nextCost = new Array(src.length + 1);
        for (let i = 0; i < currentCost.length; i++) {
            currentCost[i] = delCost * i;
        }
        for (let i = 0; i < dest.length; i++) {
            const destc = dest.charAt(i);
            nextCost[0] = currentCost[0] + insCost;
            for (let j = 0; j < src.length; j++) {
                let candidate;
                // insertion
                let optCost = currentCost[j + 1] + insCost;
                // deletion

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Ensure the input contains exactly one occurrence of the delimiter so it splits into precisely two samples (e.g. 'abc\nabd').
  2. Verify the 'Sample delimiter' argument matches the actual separator in your input; if input uses a comma, set the delimiter to ','.
  3. Trim trailing newlines/whitespace from the input before running so no spurious empty third sample is produced.
  4. If calling via the Node API, normalize the input to the form `src + delim + dest` before invoking run().

Example fix

// before: input = "kitten" (no delimiter), delim = "\n"  -> throws
// after:  input = "kitten\nsitting", delim = "\n"            -> returns 3
Defensive patterns

Strategy: validation

Validate before calling

// Before calling LevenshteinDistance.run(input, [delim, insCost, delCost, subCost]):
function validateLevenshteinInput(input, delim) {
  if (typeof input !== "string" || typeof delim !== "string") {
    throw new TypeError("input and delim must be strings");
  }
  const parts = input.split(delim);
  if (parts.length !== 2) {
    throw new Error(`Expected exactly 2 samples separated by the delimiter, got ${parts.length}.`);
  }
  return parts; // [src, dest]
}

Type guard

function isLevenshteinArgs(args) {
  return Array.isArray(args)
    && typeof args[0] === "string" // delim
    && Number.isFinite(args[1]) && Number.isFinite(args[2]) && Number.isFinite(args[3]); // costs
}

Try / catch

try {
  const result = levenshtein.run(input, args);
} catch (err) {
  if (err instanceof OperationError && /Incorrect number of samples/.test(err.message)) {
    // fix the input/delimiter, then retry with a corrected pair
  } else {
    throw err;
  }
}

Prevention

When it happens

Trigger: Calling LevenshteinDistance.run(input, args) where `input.split(delim)` yields 0, 1, or 3+ parts. This happens when the input contains no delimiter occurrence, contains it more than once, or uses a different delimiter than the `delim` argument (default '\n'). An empty input also yields length 1 and triggers this.

Common situations: Default delimiter is '\n' but the user pasted two words separated by a space or comma on one line; a trailing newline produces a third empty sample; CRLF line endings split into an extra empty token when the delimiter is a single '\n'; the delimiter argument was changed but the input was not updated to match.

Related errors


AI-assisted analysis of gchq/CyberChef@4290ea7539 (2026-08-13). Data as JSON: /api/errors/3912a7c0b5865734. Report an issue: GitHub.