mastra-ai/mastra · error · TypeError

chunking must be "word", "line", a RegExp, an Intl.Segmenter

Error message

chunking must be "word", "line", a RegExp, an Intl.Segmenter, or a chunk detector function.

What it means

smoothStream accepts chunking as 'word', 'line', a RegExp, an Intl.Segmenter, or a detector function. After handling strings and functions, the remaining value must be a RegExp (Intl.Segmenter is handled earlier). Passing any other value — or a string other than 'word'/'line', which maps to undefined in CHUNKING_PATTERNS — reaches this check and throws a TypeError, because there is no way to interpret it as a chunking strategy.

Source

Thrown at packages/core/src/stream/smooth-stream.ts:77

        return null;
      }

      if (!match.length) {
        throw new TypeError('The chunking function must return a non-empty string.');
      }

      if (!buffer.startsWith(match)) {
        throw new TypeError('The chunking function must return a prefix of the buffered text.');
      }

      return match;
    };
  }

  const pattern = typeof chunking === 'string' ? CHUNKING_PATTERNS[chunking] : chunking;

  if (!(pattern instanceof RegExp)) {
    throw new TypeError('chunking must be "word", "line", a RegExp, an Intl.Segmenter, or a chunk detector function.');
  }

  return buffer => {
    pattern.lastIndex = 0;
    const match = pattern.exec(buffer);

    if (!match) {
      return null;
    }

    const detected = buffer.slice(0, match.index) + match[0];
    if (!detected.length) {
      throw new TypeError('The chunking RegExp must match a non-empty string.');
    }

    return detected;
  };
}

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Use exactly 'word' or 'line' for the preset string chunking values.
  2. Pass a real RegExp literal (e.g. /\s+/) for custom splitting.
  3. For language-aware splitting, pass an actual Intl.Segmenter instance (new Intl.Segmenter('en', { granularity: 'sentence' })).
  4. For custom logic, pass a function (buffer: string) => string | null.

Example fix

// before
smoothStream({ model, chunking: 'sentence' }); // invalid preset

// after
smoothStream({ model, chunking: new Intl.Segmenter('en', { granularity: 'sentence' }) });
Defensive patterns

Strategy: validation

Validate before calling

type ValidChunking = 'word' | 'line' | RegExp | Intl.Segmenter | ((buffer: string) => string | null);
function assertValidChunking(c: unknown): void {
  const ok = c === 'word' || c === 'line' || c instanceof RegExp || c instanceof Intl.Segmenter || typeof c === 'function';
  if (!ok) throw new TypeError(`invalid chunking: ${typeof c}`);
}

Type guard

function isValidChunking(c: unknown): c is 'word' | 'line' | RegExp | Intl.Segmenter | ((b: string) => string | null) {
  return c === 'word' || c === 'line' || c instanceof RegExp || c instanceof Intl.Segmenter || typeof c === 'function';
}

Try / catch

try {
  streamText({ model, smoothStream: { chunking } });
} catch (err) {
  if (err instanceof TypeError && err.message.includes('chunking must be')) {
    console.error(`bad chunking value: ${String(chunking)} — use 'word', 'line', RegExp, Intl.Segmenter, or fn`);
  } else throw err;
}

Prevention

When it happens

Trigger: Calling smoothStream({ chunking: ... }) with: an unrecognized string like 'sentence' or 'token' (not 'word'/'line'); a plain object; an Intl.Segmenter-like object that is not actually an Intl.Segmenter instance; undefined/null explicitly passed as chunking.

Common situations: Typos in the chunking preset name ('word ' with a space, 'Line' capitalized); copying AI SDK v4 options that allowed other values; assuming a custom object with an exec method or a segmenter from another library is accepted.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/1030365bb4ade17e. Report an issue: GitHub.