vercel/ai · error · InvalidArgumentError

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

Error message

Chunking must be "word", "line", a RegExp, an Intl.Segmenter, or a ChunkDetector function. Received: ${chunking}

What it means

InvalidArgumentError thrown by smoothStream when the `chunking` option is none of the supported types: 'word', 'line', a RegExp, an Intl.Segmenter, or a ChunkDetector function. The value is validated up front so bad configuration fails fast instead of mid-stream.

Source

Thrown at packages/ai/src/generate-text/smooth-stream.ts:91

      if (!buffer.startsWith(match)) {
        throw new Error(
          `Chunking function must return a match that is a prefix of the buffer. Received: "${match}" expected to start with "${buffer}"`,
        );
      }

      return match;
    };
  } else {
    const chunkingRegex =
      typeof chunking === 'string'
        ? CHUNKING_REGEXPS[chunking]
        : chunking instanceof RegExp
          ? chunking
          : undefined;

    if (chunkingRegex == null) {
      throw new InvalidArgumentError({
        argument: 'chunking',
        message: `Chunking must be "word", "line", a RegExp, an Intl.Segmenter, or a ChunkDetector function. Received: ${chunking}`,
      });
    }

    detectChunk = buffer => {
      const lastIndex = chunkingRegex.lastIndex;
      chunkingRegex.lastIndex = 0;

      let match: RegExpExecArray | null;
      try {
        match = chunkingRegex.exec(buffer);
      } finally {
        chunkingRegex.lastIndex = lastIndex;
      }

      if (!match) {
        return null;

View on GitHub (pinned to 69428b1f8b)

Solutions

  1. Use exactly 'word' or 'line', or pass a RegExp like /(?<=\s)/, an Intl.Segmenter, or a function.
  2. Fix typos such as 'words' -> 'word' and 'lines' -> 'line'.
  3. Convert regex strings to real RegExp objects before passing them.

Example fix

// before
smoothStream({ chunking: 'words' });
// after
smoothStream({ chunking: 'word' });
Defensive patterns

Strategy: validation

Validate before calling

const ALLOWED = ['word', 'line'];
const ok =
  (typeof chunking === 'string' && ALLOWED.includes(chunking)) ||
  chunking instanceof RegExp ||
  chunking instanceof Intl.Segmenter ||
  typeof chunking === 'function';
if (!ok) throw new Error(`unsupported chunking: ${String(chunking)}`);

Type guard

function isChunking(v: unknown): v is 'word' | 'line' | RegExp | Intl.Segmenter | ChunkDetector {
  return v === 'word' || v === 'line' || v instanceof RegExp ||
    v instanceof Intl.Segmenter || typeof v === 'function';
}

Prevention

When it happens

Trigger: Passing chunking: 'words' (plural typo), 'sentence', a string like 'word ' with whitespace, a number, an object, or null/undefined where a detector was intended.

Common situations: Typos in the preset name; copying code from an older AI SDK version where different presets existed; passing a regex-like string instead of a RegExp literal; forgetting the option is typed but validated at runtime.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of vercel/ai@69428b1f8b (2026-08-30). Data as JSON: /api/errors/6fccf9d36600ad2c. Report an issue: GitHub.