DoclingDocument Builder API#
DoclingDocument can be constructed programmatically using a set of add_* and insert_* methods defined directly on the class, independent of the converter pipeline. The canonical implementation lives in docling_core/types/doc/document.py.
Initialization#
Construct an empty document with a name :
from docling_core.types import DoclingDocument
doc = DoclingDocument(name="My Document")
Optionally add page metadata with add_page(page_no, size, image) .
add_* Builder Methods#
All add_* methods append to the appropriate flat list (texts, tables, pictures, groups, etc.), assign a JSON Pointer self_ref, and wire up parent–child links automatically. The parent parameter defaults to doc.body when omitted.
| Method | Returns | Notes |
|---|---|---|
add_title(text, ...) | TitleItem | Stored in texts |
add_heading(text, level, ...) | SectionHeaderItem | level 1–100 |
add_text(label, text, ...) | TextItem | Dispatches to typed methods for TITLE, LIST_ITEM, SECTION_HEADER, CODE, FORMULA labels |
add_code(text, code_language, ...) | CodeItem | code_language uses CodeLanguageLabel enum |
add_formula(text, ...) | FormulaItem | |
add_list_group(name, parent, ...) | ListGroup | Container for list items |
add_list_item(text, enumerated, marker, ...) | ListItem | Parent must be a ListGroup; auto-creates one if not |
add_inline_group(name, parent, ...) | InlineGroup | For inline mixed-content runs |
add_group(label, name, parent, ...) | GroupItem | Generic container; dispatches to typed groups for LIST/INLINE labels |
add_table(data, caption, prov, ...) | TableItem | data is a TableData instance |
add_picture(image, caption, prov, ...) | PictureItem | image is an ImageRef |
add_key_values(graph, prov, ...) | KeyValueItem | graph is a GraphData |
add_form(graph, prov, ...) | FormItem | |
add_page(page_no, size, image) | PageItem | |
add_comment(text, targets, ...) | TextItem | Added to ContentLayer.NOTES; links back to target items |
Provenance is attached by passing a ProvenanceItem — a (page_no, bbox, charspan) tuple — to the prov parameter. Formatting (bold, italic, underline, strikethrough, script) is controlled by a Formatting instance.
A complete, annotated construction example (nested lists, tables with captions, pictures with ImageRef, inline groups, key-value graphs, rich formatting) is in test/conftest.py.
Sibling-Relative Insertion (insert_* Methods)#
For inserting at a specific position rather than appending, a parallel set of insert_* methods accepts a sibling node and an after: bool flag :
insert_list_group(sibling, name, after=True)insert_list_item(sibling, text, enumerated, after=True)insert_inline_group(sibling, name, after=True)insert_group(sibling, label, name, after=True)insert_text(sibling, label, text, after=True)
Building Tables#
Construct TableData from TableCell objects, then pass to add_table . For tables with rich (structured) cells, use RichTableCell (which holds a ref to another document item) and register via add_table_cell(table_item, cell) . TableData also exposes convenience methods add_row, insert_row, and remove_row for post-construction edits .
Importing from DocTags Format#
DocTags is an XML-like token format emitted by vision models (e.g. SmolDocling). To import it:
- Build a
DocTagsDocument— either from per-page(tokens_str, PIL.Image)pairs viaDocTagsDocument.from_doctags_and_image_pairs, or from a multipage string with<page_break>separators viafrom_multipage_doctags_and_images. - Convert to a
DoclingDocumentwith the static methodDoclingDocument.load_from_doctags(doctag_document, document_name).
The parser maps DocTags XML tags to DocItemLabel values , extracts <loc_N> bounding boxes (normalised by /500), and calls the same add_* builder methods internally. Supported tags include title, section_header_level_[1-6], text, otsl (table), picture, list_item, ordered_list, unordered_list, caption, footnote, code, formula, key_value_region, page_header, page_footer, and inline.
To round-trip back to DocTags, use export_to_doctags() / save_as_doctags().
Document-Level Utilities#
- Merge documents:
DoclingDocument.concatenate(docs)— merges multiple documents, re-numbering pages and re-assigning refs. - Filter by page:
doc.filter(page_nrs={1, 2}) - Hierarchize headings:
doc._hierarchize()restructures flat body items into heading-based subtrees . - Delete items:
doc.delete_items(node_items=[...])removes items and heals all cross-references.
Key Source Files#
| File | Purpose |
|---|---|
docling_core/types/doc/document.py | All add_* / insert_* methods, DocTagsDocument, load_from_doctags, concatenate, filter |
test/conftest.py | Comprehensive builder usage examples |
test/test_docling_doc.py | Test coverage for builder edge cases |