{"record":{"id":"9780fb84628e2cd4","repo":"mastra-ai/mastra","slug":"the-chunking-regexp-must-match-a-non-empty-string","errorCode":null,"errorMessage":"The chunking RegExp must match a non-empty string.","messagePattern":"The chunking RegExp must match a non-empty string\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/core/src/stream/smooth-stream.ts","lineNumber":90,"sourceCode":"  }\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}\n\n/**\n * Creates a transform stream that buffers text and reasoning deltas and emits\n * them in consistent, delayed chunks. Other stream parts pass through without\n * modification after any buffered content has been emitted.\n *\n * @experimental This API may change in a future release.\n */\nexport function smoothStream<OUTPUT = undefined>({\n  delayInMs = 10,\n  chunking = 'word',\n}: SmoothStreamOptions = {}): TransformStream<ChunkType<OUTPUT>, ChunkType<OUTPUT>> {\n  const detectChunk = createChunkDetector(chunking);","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/stream/smooth-stream.ts#L72-L108","documentation":"When a RegExp is used as the chunking strategy, createChunkDetector builds the detected chunk as everything before the match plus the match itself. If that combined string is empty — only possible when the regex matches at index 0 and matches the empty string (a zero-width match) — no progress can be made splitting the buffer and the smoothing loop would stall, so the library throws a TypeError.","triggerScenarios":"Calling smoothStream({ chunking: /someZeroWidthRegex/ }) where the pattern can match the empty string at the start of the buffer, e.g. /\\s*/, /(?=\\s)/, /x*/, or a regex composed entirely of optional groups.","commonSituations":"Writing split-on-boundary regexes with * instead of + quantifiers; using lookahead-only patterns; adapting a regex from another language where zero-width match semantics differed.","solutions":["Replace * with + quantifiers so the match consumes at least one character (e.g. /\\s+/ instead of /\\s*/).","If splitting at boundaries, include the boundary character itself in the match (e.g. /[.,!?]\\s/ instead of a lookahead alone).","Test your regex against typical buffered text to confirm match[0].length > 0 for every possible match."],"exampleFix":"// before\nsmoothStream({ model, chunking: /\\s*/ }); // zero-width match -> throws\n\n// after\nsmoothStream({ model, chunking: /\\s+/ });","handlingStrategy":"validation","validationCode":"function isNonEmptyMatchingRegex(re: RegExp, sample = 'a b c'): boolean {\n  re.lastIndex = 0;\n  const m = re.exec(sample);\n  return !!m && m[0].length > 0;\n}","typeGuard":"function matchIsNonEmpty(m: RegExpExecArray | null): m is RegExpExecArray {\n  return m !== null && m[0].length > 0;\n}","tryCatchPattern":"try {\n  streamText({ model, smoothStream: { chunking: myRegex } });\n} catch (err) {\n  if (err instanceof TypeError && err.message.includes('RegExp must match a non-empty string')) {\n    console.error('regex has zero-width matches; replace * with + or include consumed chars');\n  } else throw err;\n}","preventionTips":["Avoid * quantifiers and lookahead-only patterns in chunking regexes.","Assert every possible match consumes ≥1 character.","Test the regex against whitespace-only and punctuation-only buffers.","Prefer + or explicit character classes over optional-only groups."],"tags":["streaming","smooth-stream","regex","validation"],"backgroundTag":"empty-chunk-detector-result","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}