{"record":{"id":"f5ab81669a20cef7","repo":"vercel/ai","slug":"chunking-function-must-return-a-match-that-is-a-pr","errorCode":null,"errorMessage":"Chunking function must return a match that is a prefix of the buffer. Received: \"${match}\" expected to start with \"${buffer}\"","messagePattern":"Chunking function must return a match that is a prefix of the buffer\\. Received: \"(.+?)\" expected to start with \"(.+?)\"","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/ai/src/generate-text/smooth-stream.ts","lineNumber":75,"sourceCode":"      if (buffer.length === 0) return null;\n      const iterator = segmenter.segment(buffer)[Symbol.iterator]();\n      const first = iterator.next().value;\n      return first?.segment || null;\n    };\n  } else if (typeof chunking === 'function') {\n    detectChunk = buffer => {\n      const match = chunking(buffer);\n\n      if (match == null) {\n        return null;\n      }\n\n      if (!match.length) {\n        throw new Error(`Chunking function must return a non-empty string.`);\n      }\n\n      if (!buffer.startsWith(match)) {\n        throw new Error(\n          `Chunking function must return a match that is a prefix of the buffer. Received: \"${match}\" expected to start with \"${buffer}\"`,\n        );\n      }\n\n      return match;\n    };\n  } else {\n    const chunkingRegex =\n      typeof chunking === 'string'\n        ? CHUNKING_REGEXPS[chunking]\n        : chunking instanceof RegExp\n          ? chunking\n          : undefined;\n\n    if (chunkingRegex == null) {\n      throw new InvalidArgumentError({\n        argument: 'chunking',\n        message: `Chunking must be \"word\", \"line\", a RegExp, an Intl.Segmenter, or a ChunkDetector function. Received: ${chunking}`,","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/vercel/ai/blob/69428b1f8b037e4d118fb4853428d5c4e620493c/packages/ai/src/generate-text/smooth-stream.ts#L57-L93","documentation":"Error thrown by smoothStream when the ChunkDetector returns a non-empty string that is NOT a prefix of the current buffer. The returned match must be the leading portion of the buffered text, since the stream trims exactly that prefix before continuing.","triggerScenarios":"Custom detector returning text not at the buffer start — e.g. returning the whole buffer transformed, returning a lowercased/trimmed variant, returning match groups from the middle of the buffer, or returning a chunk computed from stale state.","commonSituations":"Detectors that normalize case or strip whitespace before returning; returning `match[1]` where the match isn't anchored at index 0; caching previous buffers and slicing incorrectly.","solutions":["Return the exact leading substring of the buffer, e.g. `buffer.slice(0, idx)`.","Do not transform the match (trim/lowercase) before returning; emit null instead if the prefix isn't complete.","If using a regex, anchor logic to `match.index === 0` and return `match[0]` only when it starts the buffer."],"exampleFix":"// before\nconst chunking = (buffer: string) => buffer.trim(); // not a prefix if leading whitespace\n// after\nconst chunking = (buffer: string) => {\n  const idx = buffer.search(/\\s/);\n  return idx === -1 ? null : buffer.slice(0, idx);\n};","handlingStrategy":"validation","validationCode":"const detector = (buffer: string) => {\n  const m = computeChunk(buffer);\n  if (m != null && !buffer.startsWith(m)) return null; // enforce prefix invariant\n  return m;\n};","typeGuard":"function isPrefixOfBuffer(m: string | null, buffer: string): m is string {\n  return m !== null && m.length > 0 && buffer.startsWith(m);\n}","tryCatchPattern":"try {\n  stream(fullStream.pipeThrough(smoothStream({ chunking: detector })));\n} catch (e) {\n  if (e instanceof Error && e.message.startsWith('Chunking function must return a match')) {\n    // disable smoothing and pass raw stream through\n  } else throw e;\n}","preventionTips":["Always slice the returned chunk out of the same buffer argument.","Never transform (trim/lowercase) the detector result before returning.","Add an assertion `buffer.startsWith(result)` in detector tests."],"tags":["streaming","smooth-stream","custom-detector"],"backgroundTag":"invalid-chunk-detector-return","analyzedSha":"69428b1f8b037e4d118fb4853428d5c4e620493c","analyzedAt":"2026-08-30T12:32:21.016Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}