{"record":{"id":"1030365bb4ade17e","repo":"mastra-ai/mastra","slug":"chunking-must-be-word-line-a-regexp-an-intl","errorCode":null,"errorMessage":"chunking must be \"word\", \"line\", a RegExp, an Intl.Segmenter, or a chunk detector function.","messagePattern":"chunking must be \"word\", \"line\", a RegExp, an Intl\\.Segmenter, or a chunk detector function\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/core/src/stream/smooth-stream.ts","lineNumber":77,"sourceCode":"        return null;\n      }\n\n      if (!match.length) {\n        throw new TypeError('The chunking function must return a non-empty string.');\n      }\n\n      if (!buffer.startsWith(match)) {\n        throw new TypeError('The chunking function must return a prefix of the buffered text.');\n      }\n\n      return match;\n    };\n  }\n\n  const pattern = typeof chunking === 'string' ? CHUNKING_PATTERNS[chunking] : chunking;\n\n  if (!(pattern instanceof RegExp)) {\n    throw new TypeError('chunking must be \"word\", \"line\", a RegExp, an Intl.Segmenter, or a chunk detector function.');\n  }\n\n  return buffer => {\n    pattern.lastIndex = 0;\n    const match = pattern.exec(buffer);\n\n    if (!match) {\n      return null;\n    }\n\n    const detected = buffer.slice(0, match.index) + match[0];\n    if (!detected.length) {\n      throw new TypeError('The chunking RegExp must match a non-empty string.');\n    }\n\n    return detected;\n  };\n}","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/stream/smooth-stream.ts#L59-L95","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Use exactly 'word' or 'line' for the preset string chunking values.","Pass a real RegExp literal (e.g. /\\s+/) for custom splitting.","For language-aware splitting, pass an actual Intl.Segmenter instance (new Intl.Segmenter('en', { granularity: 'sentence' })).","For custom logic, pass a function (buffer: string) => string | null."],"exampleFix":"// before\nsmoothStream({ model, chunking: 'sentence' }); // invalid preset\n\n// after\nsmoothStream({ model, chunking: new Intl.Segmenter('en', { granularity: 'sentence' }) });","handlingStrategy":"validation","validationCode":"type ValidChunking = 'word' | 'line' | RegExp | Intl.Segmenter | ((buffer: string) => string | null);\nfunction assertValidChunking(c: unknown): void {\n  const ok = c === 'word' || c === 'line' || c instanceof RegExp || c instanceof Intl.Segmenter || typeof c === 'function';\n  if (!ok) throw new TypeError(`invalid chunking: ${typeof c}`);\n}","typeGuard":"function isValidChunking(c: unknown): c is 'word' | 'line' | RegExp | Intl.Segmenter | ((b: string) => string | null) {\n  return c === 'word' || c === 'line' || c instanceof RegExp || c instanceof Intl.Segmenter || typeof c === 'function';\n}","tryCatchPattern":"try {\n  streamText({ model, smoothStream: { chunking } });\n} catch (err) {\n  if (err instanceof TypeError && err.message.includes('chunking must be')) {\n    console.error(`bad chunking value: ${String(chunking)} — use 'word', 'line', RegExp, Intl.Segmenter, or fn`);\n  } else throw err;\n}","preventionTips":["Type the chunking param as the narrow union type so TS catches invalid values at compile time.","Use only the documented preset strings: 'word' and 'line'.","Use real Intl.Segmenter instances, not segmenter-like objects.","Validate user/config-supplied chunking values before passing them in."],"tags":["streaming","smooth-stream","validation","configuration"],"backgroundTag":"invalid-chunking-option","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}