Skip to content

Reference

chunkDocument

Chunks one document and returns its chunks, stats, and warnings.

Chunks one document.

function chunkDocument(source: DocumentSource, options?: ChunkOptions): Promise<ChunkResult>;
Parameter Type Description
source DocumentSource The document: { path }, { bytes, filename?, format? }, or { content, format }.
options ChunkOptions Optional. The strategy, its options, and the common options. Without options, the structure strategy is used with its defaults.

A promise of a ChunkResult:

Field Type Description
chunks Chunk[] The chunks, in document order.
document DocumentInfo The document’s id, format, path, and Markdown.
stats ChunkStats Chunk count, sizes, and time taken.
warnings ChunkWarning[] Problems that didn’t stop chunking. See Warnings.

A DocchunkError with one of these codes:

Code When
INVALID_OPTIONS An option or the source is invalid.
UNSUPPORTED_FORMAT The format isn’t supported, or can’t be detected.
FILE_NOT_FOUND The file at path can’t be read.
CONVERSION_FAILED The file couldn’t be converted to Markdown.
ABORTED options.signal was aborted.

See Error codes for every case.

import { chunkDocument } from "docchunk";
const { chunks, warnings } = await chunkDocument(
{ path: "handbook.pdf" },
{ strategy: "structure", size: 1000 },
);