gchq/CyberChef · error · OperationError

Incorrect number of samples, perhaps you need to modify the

Error message

Incorrect number of samples, perhaps you need to modify the sample delimiter or add more samples?

What it means

Thrown by Diff run() when input.split(sampleDelim) does not yield exactly two samples. The operation compares two inputs; the default sample delimiter is '\n\n' (a literal backslash-n-backslash-n rendered from the binaryString default). If the input does not contain the delimiter, split returns a 1-element array; if it contains it more than once, the array is longer than 2.

Source

Thrown at src/core/operations/Diff.mjs:86

     */
    run(input, args) {
        const [
                sampleDelim,
                diffBy,
                showAdded,
                showRemoved,
                showSubtraction,
                ignoreWhitespace
            ] = args,
            samples = input.split(sampleDelim);
        let output = "",
            diff;

        // Node and Webpack load modules slightly differently
        const jsdiff = JsDiff.default ? JsDiff.default : JsDiff;

        if (!samples || samples.length !== 2) {
            throw new OperationError("Incorrect number of samples, perhaps you need to modify the sample delimiter or add more samples?");
        }

        switch (diffBy) {
            case "Character":
                diff = jsdiff.diffChars(samples[0], samples[1]);
                break;
            case "Word":
                if (ignoreWhitespace) {
                    diff = jsdiff.diffWords(samples[0], samples[1]);
                } else {
                    diff = jsdiff.diffWordsWithSpace(samples[0], samples[1]);
                }
                break;
            case "Line":
                if (ignoreWhitespace) {
                    diff = jsdiff.diffTrimmedLines(samples[0], samples[1]);
                } else {
                    diff = jsdiff.diffLines(samples[0], samples[1]);

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Ensure the input contains exactly one occurrence of the sample delimiter separating the two samples.
  2. Adjust the Sample delimiter argument to match the actual separator in your input (e.g. a single newline '\n', a comma, a tab).
  3. If the delimiter is a control sequence, confirm whether the binaryString field interprets it literally or as an escape (the default '\n\n' is an escape).
  4. Remove extra occurrences of the separator so only two samples result.

Example fix

// before - samples joined by one newline, delimiter expects two
input: "lineA\nlineB"
delimiter: "\n\n"  // split -> 1 sample -> error

// after
delimiter: "\n"      // split -> 2 samples
Defensive patterns

Strategy: validation

Validate before calling

function hasExactlyTwoSamples(input, sampleDelim) {
    return input.split(sampleDelim).length === 2;
}

Type guard

/** @returns {boolean} */
function hasTwoSamples(input, sampleDelim) {
    return typeof input === "string"
        && typeof sampleDelim === "string"
        && sampleDelim.length > 0
        && input.split(sampleDelim).length === 2;
}

Try / catch

try {
    out = diff.run(input, args);
} catch (e) {
    if (e instanceof OperationError && /Incorrect number of samples/.test(e.message)) {
        // adjust sampleDelim to match the actual separator in input
    } else throw e;
}

Prevention

When it happens

Trigger: Input with zero occurrences of the delimiter (returns ['whole input'], length 1); input with 2+ occurrences (length 3+); delimiter set to a value that does not actually appear between the two samples (e.g. samples separated by a single newline while delimiter expects two).

Common situations: Pasting two text blocks separated by a single blank line when the delimiter expects '\n\n' rendered as actual newlines vs the literal characters; delimiter string not matching the input's separator; providing three or more samples; empty input.

Related errors


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