{"record":{"id":"fec7b4bb782cf49b","repo":"mastra-ai/mastra","slug":"the-chunking-function-must-return-a-non-empty-stri","errorCode":null,"errorMessage":"The chunking function must return a non-empty string.","messagePattern":"The chunking function must return a non-empty string\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/core/src/stream/smooth-stream.ts","lineNumber":63,"sourceCode":"      if (!buffer) {\n        return null;\n      }\n\n      const firstSegment = chunking.segment(buffer)[Symbol.iterator]().next().value;\n      return firstSegment?.segment || null;\n    };\n  }\n\n  if (typeof chunking === 'function') {\n    return buffer => {\n      const match = chunking(buffer);\n\n      if (match == null) {\n        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;","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/stream/smooth-stream.ts#L45-L81","documentation":"createChunkDetector wraps a custom chunking function for smoothStream. The wrapped detector validates the function's return value: it must return a string, and if it returns a match it must be non-empty. An empty string cannot be used to split the buffered text, so the library throws a TypeError immediately instead of producing an empty chunk that would cause an infinite loop in the smoothing loop.","triggerScenarios":"Calling smoothStream({ chunking: myFn }) where myFn(buffer) returns '' (empty string) on a non-null match — e.g. returning buffer.slice(0, 0), a match[0] that is empty, or a function that returns '' instead of null when no chunk boundary is found.","commonSituations":"Writing a custom chunk detector that uses String.match or exec and forgets to guard against an empty match (e.g. a regex with zero-width alternatives like /\\s*/); returning '' as a sentinel for 'no chunk yet' instead of null.","solutions":["Return null (not '') from your chunking function when no boundary should be emitted this tick.","Ensure any returned match has at least one character; adjust your regex to require content (use \\s+ not \\s*, avoid zero-width matches).","Add a guard in your chunking function: if (match.length === 0) return null;"],"exampleFix":"// before\nconst chunking = buffer => buffer.match(/\\s*/) ?? null; // can return ''\n\n// after\nconst chunking = buffer => {\n  const m = buffer.match(/\\s+/);\n  return m ? m[0] : null;\n};","handlingStrategy":"validation","validationCode":"function isValidChunker(fn: (buf: string) => string | null): boolean {\n  const out = fn('sample text boundary');\n  return out === null || out.length > 0;\n}","typeGuard":"function returnsNonEmptyOrNull(v: unknown): v is string | null {\n  return v === null || (typeof v === 'string' && v.length > 0);\n}","tryCatchPattern":"try {\n  streamText({ model, smoothStream: { chunking: myChunker } });\n} catch (err) {\n  if (err instanceof TypeError && err.message.includes('non-empty string')) {\n    console.error('chunker returned empty string; fix to return null instead');\n  } else throw err;\n}","preventionTips":["Always return null, never '', for 'no boundary yet'.","Use + quantifiers instead of * in boundary regexes to avoid empty matches.","Unit-test your chunker with edge inputs: empty buffer, whitespace-only buffer.","Wrap match[0] returns with a length check."],"tags":["streaming","smooth-stream","validation","typeerror"],"backgroundTag":"empty-chunk-detector-result","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}