Skip to content

OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.

Python

fix: Update docling custom component to fix OCR handling (#2389)

Mike Fortman1d2dcb2fd0eb
diff --git a/custom_components/openrag/export_docling_document.py b/custom_components/openrag/export_docling_document.py
index 68780c5f4..4190b1b05 100644
--- a/custom_components/openrag/export_docling_document.py
+++ b/custom_components/openrag/export_docling_document.py
@@ -1,11 +1,39 @@
+import inspect
from typing import Any
-from lfx.base.data.docling_utils import coerce_docling_document, extract_docling_documents, get_docling_image_ref_mode
+from lfx.base.data.docling_utils import (
+ coerce_docling_document,
+ extract_docling_documents,
+ get_docling_image_ref_mode,
+)
from lfx.custom import Component
from lfx.io import DropdownInput, HandleInput, MessageTextInput, Output, StrInput
from lfx.schema import Data, DataFrame
+def traverse_pictures_kwargs(export_fn) -> dict[str, Any]:
+ """Ask export_to_markdown to serialize text nested under pictures, when it can.
+
+ Scanned/image pages come back from Docling with all their OCR text attached
+ as children of a PictureItem rather than to the document body. Markdown
+ export skips picture children by default, which silently drops every OCR'd
+ word and leaves only the image placeholder.
+
+ Older docling-core releases have no `traverse_pictures` parameter and raise
+ TypeError if it is passed, so probe the signature rather than assume.
+ """
+ try:
+ parameters = inspect.signature(export_fn).parameters
+ except (TypeError, ValueError):
+ # Some callables (C builtins, exotic wrappers) have no inspectable
+ # signature. Omitting the flag keeps the old behaviour rather than
+ # risking a TypeError that would fail the whole export.
+ return {}
+ if "traverse_pictures" in parameters:
+ return {"traverse_pictures": True}
+ return {}
+
+
class ExportDoclingDocumentComponent(Component):
display_name: str = "Export DoclingDocument"
description: str = "Export DoclingDocument to markdown, html or other formats."
@@ -67,7 +95,9 @@ class ExportDoclingDocumentComponent(Component):
Output(display_name="Table", name="dataframe", method="as_dataframe"),
]
- def update_build_config(self, build_config: dict, field_value: Any, field_name: str | None = None) -> dict:
+ def update_build_config(
+ self, build_config: dict, field_value: Any, field_name: str | None = None
+ ) -> dict:
if field_name == "export_format" and field_value == "Markdown":
build_config["md_image_placeholder"]["show"] = True
build_config["md_page_break_placeholder"]["show"] = True
@@ -90,7 +120,6 @@ class ExportDoclingDocumentComponent(Component):
def _coerce_exportable_document(doc: Any) -> Any:
return coerce_docling_document(doc)
-
def _base_metadata(self, doc) -> dict:
"""Build shared metadata from a DoclingDocument."""
metadata: dict = {"export_format": self.export_format}
@@ -124,6 +153,7 @@ class ExportDoclingDocumentComponent(Component):
image_mode=image_mode,
image_placeholder=self.md_image_placeholder,
page_no=page_no,
+ **traverse_pictures_kwargs(doc.export_to_markdown),
)
except TypeError:
# Fallback to from_page/to_page parameters
@@ -132,6 +162,7 @@ class ExportDoclingDocumentComponent(Component):
image_placeholder=self.md_image_placeholder,
from_page=page_no,
to_page=page_no,
+ **traverse_pictures_kwargs(doc.export_to_markdown),
)
except Exception:
# Any exception from either attempt: fall back to whole-document export
@@ -169,12 +200,14 @@ class ExportDoclingDocumentComponent(Component):
image_mode=image_mode,
image_placeholder=self.md_image_placeholder,
page_break_placeholder=self.md_page_break_placeholder,
+ **traverse_pictures_kwargs(doc.export_to_markdown),
)
except TypeError:
# Older docling-core versions lack page_break_placeholder
content = doc.export_to_markdown(
image_mode=image_mode,
image_placeholder=self.md_image_placeholder,
+ **traverse_pictures_kwargs(doc.export_to_markdown),
)
elif self.export_format == "HTML":
content = doc.export_to_html(image_mode=image_mode)
diff --git a/flows/ingestion_flow.json b/flows/ingestion_flow.json
index 5759d6cae..3d8598d36 100644
--- a/flows/ingestion_flow.json
+++ b/flows/ingestion_flow.json
@@ -787,7 +787,7 @@
"show": true,
"title_case": false,
"type": "code",
- "value": "from typing import Any\n\nfrom lfx.base.data.docling_utils import coerce_docling_document, extract_docling_documents, get_docling_image_ref_mode\nfrom lfx.custom import Component\nfrom lfx.io import DropdownInput, HandleInput, MessageTextInput, Output, StrInput\nfrom lfx.schema import Data, DataFrame\n\n\nclass ExportDoclingDocumentComponent(Component):\n display_name: str = \"Export DoclingDocument\"\n description: str = \"Export DoclingDocument to markdown, html or other formats.\"\n documentation = \"https://docling-project.github.io/docling/\"\n icon = \"Docling\"\n name = \"ExportDoclingDocument\"\n\n inputs = [\n HandleInput(\n name=\"data_inputs\",\n display_name=\"JSON or Table\",\n info=\"The data with documents to export.\",\n input_types=[\"Data\", \"JSON\", \"DataFrame\", \"Table\"],\n required=True,\n ),\n DropdownInput(\n name=\"export_format\",\n display_name=\"Export format\",\n options=[\"Markdown\", \"HTML\", \"Plaintext\", \"DocTags\"],\n info=\"Select the export format to convert the input.\",\n value=\"Markdown\",\n real_time_refresh=True,\n ),\n DropdownInput(\n name=\"image_mode\",\n display_name=\"Image export mode\",\n options=[\"placeholder\", \"embedded\"],\n info=(\n \"Specify how images are exported in the output. Placeholder will replace the images with a string, \"\n \"whereas Embedded will include them as base64 encoded images.\"\n ),\n value=\"placeholder\",\n ),\n StrInput(\n name=\"md_image_placeholder\",\n display_name=\"Image placeholder\",\n info=\"Specify the image placeholder for markdown exports.\",\n value=\"<!-- image -->\",\n advanced=True,\n ),\n StrInput(\n name=\"md_page_break_placeholder\",\n display_name=\"Page break placeholder\",\n info=\"Add this placeholder between pages in the markdown output.\",\n value=\"\",\n advanced=True,\n ),\n MessageTextInput(\n name=\"doc_key\",\n display_name=\"Doc Key\",\n info=\"The key to use for the DoclingDocument column.\",\n value=\"doc\",\n advanced=True,\n ),\n ]\n\n outputs = [\n Output(display_name=\"Exported data\", name=\"data\", method=\"export_document\"),\n Output(display_name=\"Table\", name=\"dataframe\", method=\"as_dataframe\"),\n ]\n\n def update_build_config(self, build_config: dict, field_value: Any, field_name: str | None = None) -> dict:\n if field_name == \"export_format\" and field_value == \"Markdown\":\n build_config[\"md_image_placeholder\"][\"show\"] = True\n build_config[\"md_page_break_placeholder\"][\"show\"] = True\n build_config[\"image_mode\"][\"show\"] = True\n elif field_name == \"export_format\" and field_value == \"HTML\":\n build_config[\"md_image_placeholder\"][\"show\"] = False\n build_config[\"md_page_break_placeholder\"][\"show\"] = False\n build_config[\"image_mode\"][\"show\"] = True\n elif field_name == \"export_format\" and field_value in {\"Plaintext\", \"DocTags\"}:\n build_config[\"md_image_placeholder\"][\"show\"] = False\n build_config[\"md_page_break_placeholder\"][\"show\"] = False\n build_config[\"image_mode\"][\"show\"] = False\n\n return build_config\n\n def _get_image_mode(self) -> Any:\n return get_docling_image_ref_mode(self.image_mode)\n\n @staticmethod\n def _coerce_exportable_document(doc: Any) -> Any:\n return coerce_docling_document(doc)\n\n\n def _base_metadata(self, doc) -> dict:\n \"\"\"Build shared metadata from a DoclingDocument.\"\"\"\n metadata: dict = {\"export_format\": self.export_format}\n if hasattr(doc, \"name\") and doc.name:\n metadata[\"name\"] = doc.name\n if hasattr(doc, \"origin\") and doc.origin is not None:\n if hasattr(doc.origin, \"filename\") and doc.origin.filename:\n metadata[\"filename\"] = doc.origin.filename\n if hasattr(doc.origin, \"binary_hash\") and doc.origin.binary_hash:\n metadata[\"document_id\"] = str(doc.origin.binary_hash)\n if hasattr(doc.origin, \"mimetype\") and doc.origin.mimetype:\n metadata[\"mimetype\"] = doc.origin.mimetype\n return metadata\n\n def _export_per_page_markdown(self, doc, image_mode: Any, base_meta: dict) -> list[Data]:\n \"\"\"Export one Data chunk per page, tagging each with page=N.\n\n Uses DoclingDocument.pages (ordered dict of page_no -> PageItem) to\n iterate pages.\n \"\"\"\n pages_dict = getattr(doc, \"pages\", None)\n if not pages_dict:\n return []\n\n results: list[Data] = []\n for page_no in sorted(pages_dict.keys()):\n try:\n try:\n # Try standard page_no first (docling-core 2.x+)\n page_content = doc.export_to_markdown(\n image_mode=image_mode,\n image_placeholder=self.md_image_placeholder,\n page_no=page_no,\n )\n except TypeError:\n # Fallback to from_page/to_page parameters\n page_content = doc.export_to_markdown(\n image_mode=image_mode,\n image_placeholder=self.md_image_placeholder,\n from_page=page_no,\n to_page=page_no,\n )\n except Exception:\n # Any exception from either attempt: fall back to whole-document export\n return []\n\n if page_content and page_content.strip():\n meta = {**base_meta, \"page\": int(page_no)}\n results.append(Data(text=page_content, data={\"text\": page_content, **meta}))\n\n return results\n\n def export_document(self) -> list[Data]:\n documents, warning = extract_docling_documents(self.data_inputs, self.doc_key)\n if warning:\n self.status = warning\n\n results: list[Data] = []\n try:\n image_mode = self._get_image_mode()\n for raw_doc in documents:\n doc = self._coerce_exportable_document(raw_doc)\n base_meta = self._base_metadata(doc)\n\n # For Markdown: attempt per-page export so downstream chunks\n # carry an accurate page number (used by the chat citation UI).\n if self.export_format == \"Markdown\":\n per_page = self._export_per_page_markdown(doc, image_mode, base_meta)\n if per_page:\n results.extend(per_page)\n continue\n\n # Fall back to whole-document export (no page granularity)\n try:\n content = doc.export_to_markdown(\n image_mode=image_mode,\n image_placeholder=self.md_image_placeholder,\n page_break_placeholder=self.md_page_break_placeholder,\n )\n except TypeError:\n # Older docling-core versions lack page_break_placeholder\n content = doc.export_to_markdown(\n image_mode=image_mode,\n image_placeholder=self.md_image_placeholder,\n )\n elif self.export_format == \"HTML\":\n content = doc.export_to_html(image_mode=image_mode)\n elif self.export_format == \"Plaintext\":\n content = doc.export_to_text()\n elif self.export_format == \"DocTags\":\n content = doc.export_to_doctags()\n else:\n content = \"\"\n\n results.append(Data(text=content, data={\"text\": content, **base_meta}))\n\n except Exception as e:\n msg = f\"Error exporting document: {e}\"\n raise TypeError(msg) from e\n\n return results\n\n def as_dataframe(self) -> DataFrame:\n return DataFrame(self.export_document())\n"
+ "value": "import inspect\nfrom typing import Any\n\nfrom lfx.base.data.docling_utils import coerce_docling_document, extract_docling_documents, get_docling_image_ref_mode\nfrom lfx.custom import Component\nfrom lfx.io import DropdownInput, HandleInput, MessageTextInput, Output, StrInput\nfrom lfx.schema import Data, DataFrame\n\n\ndef traverse_pictures_kwargs(export_fn) -> dict[str, Any]:\n \"\"\"Ask export_to_markdown to serialize text nested under pictures, when it can.\n\n Scanned/image pages come back from Docling with all their OCR text attached\n as children of a PictureItem rather than to the document body. Markdown\n export skips picture children by default, which silently drops every OCR'd\n word and leaves only the image placeholder.\n\n Older docling-core releases have no `traverse_pictures` parameter and raise\n TypeError if it is passed, so probe the signature rather than assume.\n \"\"\"\n try:\n parameters = inspect.signature(export_fn).parameters\n except (TypeError, ValueError):\n # Some callables (C builtins, exotic wrappers) have no inspectable\n # signature. Omitting the flag keeps the old behaviour rather than\n # risking a TypeError that would fail the whole export.\n return {}\n if \"traverse_pictures\" in parameters:\n return {\"traverse_pictures\": True}\n return {}\n\n\nclass ExportDoclingDocumentComponent(Component):\n display_name: str = \"Export DoclingDocument\"\n description: str = \"Export DoclingDocument to markdown, html or other formats.\"\n documentation = \"https://docling-project.github.io/docling/\"\n icon = \"Docling\"\n name = \"ExportDoclingDocument\"\n\n inputs = [\n HandleInput(\n name=\"data_inputs\",\n display_name=\"JSON or Table\",\n info=\"The data with documents to export.\",\n input_types=[\"Data\", \"JSON\", \"DataFrame\", \"Table\"],\n required=True,\n ),\n DropdownInput(\n name=\"export_format\",\n display_name=\"Export format\",\n options=[\"Markdown\", \"HTML\", \"Plaintext\", \"DocTags\"],\n info=\"Select the export format to convert the input.\",\n value=\"Markdown\",\n real_time_refresh=True,\n ),\n DropdownInput(\n name=\"image_mode\",\n display_name=\"Image export mode\",\n options=[\"placeholder\", \"embedded\"],\n info=(\n \"Specify how images are exported in the output. Placeholder will replace the images with a string, \"\n \"whereas Embedded will include them as base64 encoded images.\"\n ),\n value=\"placeholder\",\n ),\n StrInput(\n name=\"md_image_placeholder\",\n display_name=\"Image placeholder\",\n info=\"Specify the image placeholder for markdown exports.\",\n value=\"<!-- image -->\",\n advanced=True,\n ),\n StrInput(\n name=\"md_page_break_placeholder\",\n display_name=\"Page break placeholder\",\n info=\"Add this placeholder between pages in the markdown output.\",\n value=\"\",\n advanced=True,\n ),\n MessageTextInput(\n name=\"doc_key\",\n display_name=\"Doc Key\",\n info=\"The key to use for the DoclingDocument column.\",\n value=\"doc\",\n advanced=True,\n ),\n ]\n\n outputs = [\n Output(display_name=\"Exported data\", name=\"data\", method=\"export_document\"),\n Output(display_name=\"Table\", name=\"dataframe\", method=\"as_dataframe\"),\n ]\n\n def update_build_config(self, build_config: dict, field_value: Any, field_name: str | None = None) -> dict:\n if field_name == \"export_format\" and field_value == \"Markdown\":\n build_config[\"md_image_placeholder\"][\"show\"] = True\n build_config[\"md_page_break_placeholder\"][\"show\"] = True\n build_config[\"image_mode\"][\"show\"] = True\n elif field_name == \"export_format\" and field_value == \"HTML\":\n build_config[\"md_image_placeholder\"][\"show\"] = False\n build_config[\"md_page_break_placeholder\"][\"show\"] = False\n build_config[\"image_mode\"][\"show\"] = True\n elif field_name == \"export_format\" and field_value in {\"Plaintext\", \"DocTags\"}:\n build_config[\"md_image_placeholder\"][\"show\"] = False\n build_config[\"md_page_break_placeholder\"][\"show\"] = False\n build_config[\"image_mode\"][\"show\"] = False\n\n return build_config\n\n def _get_image_mode(self) -> Any:\n return get_docling_image_ref_mode(self.image_mode)\n\n @staticmethod\n def _coerce_exportable_document(doc: Any) -> Any:\n return coerce_docling_document(doc)\n\n\n def _base_metadata(self, doc) -> dict:\n \"\"\"Build shared metadata from a DoclingDocument.\"\"\"\n metadata: dict = {\"export_format\": self.export_format}\n if hasattr(doc, \"name\") and doc.name:\n metadata[\"name\"] = doc.name\n if hasattr(doc, \"origin\") and doc.origin is not None:\n if hasattr(doc.origin, \"filename\") and doc.origin.filename:\n metadata[\"filename\"] = doc.origin.filename\n if hasattr(doc.origin, \"binary_hash\") and doc.origin.binary_hash:\n metadata[\"document_id\"] = str(doc.origin.binary_hash)\n if hasattr(doc.origin, \"mimetype\") and doc.origin.mimetype:\n metadata[\"mimetype\"] = doc.origin.mimetype\n return metadata\n\n def _export_per_page_markdown(self, doc, image_mode: Any, base_meta: dict) -> list[Data]:\n \"\"\"Export one Data chunk per page, tagging each with page=N.\n\n Uses DoclingDocument.pages (ordered dict of page_no -> PageItem) to\n iterate pages.\n \"\"\"\n pages_dict = getattr(doc, \"pages\", None)\n if not pages_dict:\n return []\n\n results: list[Data] = []\n for page_no in sorted(pages_dict.keys()):\n try:\n try:\n # Try standard page_no first (docling-core 2.x+)\n page_content = doc.export_to_markdown(\n image_mode=image_mode,\n image_placeholder=self.md_image_placeholder,\n page_no=page_no,\n **traverse_pictures_kwargs(doc.export_to_markdown),\n )\n except TypeError:\n # Fallback to from_page/to_page parameters\n page_content = doc.export_to_markdown(\n image_mode=image_mode,\n image_placeholder=self.md_image_placeholder,\n from_page=page_no,\n to_page=page_no,\n **traverse_pictures_kwargs(doc.export_to_markdown),\n )\n except Exception:\n # Any exception from either attempt: fall back to whole-document export\n return []\n\n if page_content and page_content.strip():\n meta = {**base_meta, \"page\": int(page_no)}\n results.append(Data(text=page_content, data={\"text\": page_content, **meta}))\n\n return results\n\n def export_document(self) -> list[Data]:\n documents, warning = extract_docling_documents(self.data_inputs, self.doc_key)\n if warning:\n self.status = warning\n\n results: list[Data] = []\n try:\n image_mode = self._get_image_mode()\n for raw_doc in documents:\n doc = self._coerce_exportable_document(raw_doc)\n base_meta = self._base_metadata(doc)\n\n # For Markdown: attempt per-page export so downstream chunks\n # carry an accurate page number (used by the chat citation UI).\n if self.export_format == \"Markdown\":\n per_page = self._export_per_page_markdown(doc, image_mode, base_meta)\n if per_page:\n results.extend(per_page)\n continue\n\n # Fall back to whole-document export (no page granularity)\n try:\n content = doc.export_to_markdown(\n image_mode=image_mode,\n image_placeholder=self.md_image_placeholder,\n page_break_placeholder=self.md_page_break_placeholder,\n **traverse_pictures_kwargs(doc.export_to_markdown),\n )\n except TypeError:\n # Older docling-core versions lack page_break_placeholder\n content = doc.export_to_markdown(\n image_mode=image_mode,\n image_placeholder=self.md_image_placeholder,\n **traverse_pictures_kwargs(doc.export_to_markdown),\n )\n elif self.export_format == \"HTML\":\n content = doc.export_to_html(image_mode=image_mode)\n elif self.export_format == \"Plaintext\":\n content = doc.export_to_text()\n elif self.export_format == \"DocTags\":\n content = doc.export_to_doctags()\n else:\n content = \"\"\n\n results.append(Data(text=content, data={\"text\": content, **base_meta}))\n\n except Exception as e:\n msg = f\"Error exporting document: {e}\"\n raise TypeError(msg) from e\n\n return results\n\n def as_dataframe(self) -> DataFrame:\n return DataFrame(self.export_document())\n"
},
"data_inputs": {
"_input_type": "HandleInput",
diff --git a/tests/unit/test_export_docling_traverse_pictures.py b/tests/unit/test_export_docling_traverse_pictures.py
new file mode 100644
index 000000000..07d77c697
--- /dev/null
+++ b/tests/unit/test_export_docling_traverse_pictures.py
@@ -0,0 +1,215 @@
+"""Export DoclingDocument must not drop OCR text nested under a picture.
+
+Scanned/image pages come back from Docling with their OCR text attached as
+children of a PictureItem rather than to the document body. `export_to_markdown`
+skips picture children unless `traverse_pictures=True`, so without it the
+component emits a bare `<!-- image -->` and every OCR'd word is lost.
+
+The component imports `lfx` (Langflow) and docling-core, neither of which is in
+the backend venv, so these tests skip there and run wherever those are present
+(the Langflow image). The pure signature-probe test always runs.
+"""
+
+import importlib.util
+import inspect
+import sys
+import types
+from pathlib import Path
+
+import pytest
+
+COMPONENT_PATH = (
+ Path(__file__).resolve().parents[2]
+ / "custom_components"
+ / "openrag"
+ / "export_docling_document.py"
+)
+
+
+def _load_component_module():
+ """Import the component with `lfx` stubbed out, since Langflow isn't installed."""
+ if "lfx" not in sys.modules:
+ for name in (
+ "lfx",
+ "lfx.base",
+ "lfx.base.data",
+ "lfx.base.data.docling_utils",
+ "lfx.custom",
+ "lfx.io",
+ "lfx.schema",
+ ):
+ sys.modules.setdefault(name, types.ModuleType(name))
+ sys.modules["lfx.base.data.docling_utils"].coerce_docling_document = lambda *a, **k: None
+ sys.modules["lfx.base.data.docling_utils"].extract_docling_documents = lambda *a, **k: (
+ [],
+ None,
+ )
+ sys.modules["lfx.base.data.docling_utils"].get_docling_image_ref_mode = lambda *a, **k: None
+ sys.modules["lfx.custom"].Component = object
+ for io_name in ("DropdownInput", "HandleInput", "MessageTextInput", "Output", "StrInput"):
+ setattr(sys.modules["lfx.io"], io_name, lambda *a, **k: None)
+ sys.modules["lfx.schema"].Data = object
+ sys.modules["lfx.schema"].DataFrame = object
+
+ spec = importlib.util.spec_from_file_location("export_docling_document", COMPONENT_PATH)
+ module = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(module)
+ return module
+
+
+def _scanned_page_document():
+ """A page whose OCR text hangs off a PictureItem, as Docling emits for scans."""
+ body_texts = ["3. Leave Entitlements:"]
+ picture_texts = [
+ "Earned Leave (EL): 18 days annually.",
+ "Sick Leave (SL): 12 days per year.",
+ ]
+
+ def text_item(index, text, parent):
+ return {
+ "self_ref": f"#/texts/{index}",
+ "parent": {"cref": parent},
+ "children": [],
+ "content_layer": "body",
+ "label": "text",
+ "prov": [
+ {
+ "page_no": 1,
+ "bbox": {"l": 38, "t": 800, "r": 420, "b": 790, "coord_origin": "BOTTOMLEFT"},
+ "charspan": [0, len(text)],
+ }
+ ],
+ "orig": text,
+ "text": text,
+ }
+
+ texts = [text_item(0, body_texts[0], "#/body")]
+ texts += [text_item(i + 1, t, "#/pictures/0") for i, t in enumerate(picture_texts)]
+
+ return {
+ "schema_name": "DoclingDocument",
+ "version": "1.10.0",
+ "name": "scan",
+ "origin": {
+ "mimetype": "application/pdf",
+ "binary_hash": 1,
+ "filename": "scan.pdf",
+ "uri": None,
+ },
+ "furniture": {
+ "self_ref": "#/furniture",
+ "parent": None,
+ "children": [],
+ "content_layer": "furniture",
+ "name": "_root_",
+ "label": "unspecified",
+ },
+ "body": {
+ "self_ref": "#/body",
+ "parent": None,
+ "children": [{"cref": "#/texts/0"}, {"cref": "#/pictures/0"}],
+ "content_layer": "body",
+ "name": "_root_",
+ "label": "unspecified",
+ },
+ "groups": [],
+ "texts": texts,
+ "pictures": [
+ {
+ "self_ref": "#/pictures/0",
+ "parent": {"cref": "#/body"},
+ "children": [{"cref": f"#/texts/{i + 1}"} for i in range(len(picture_texts))],
+ "content_layer": "body",
+ "label": "picture",
+ "prov": [
+ {
+ "page_no": 1,
+ "bbox": {
+ "l": 71,
+ "t": 770,
+ "r": 523,
+ "b": 473,
+ "coord_origin": "BOTTOMLEFT",
+ },
+ "charspan": [0, 0],
+ }
+ ],
+ "captions": [],
+ "references": [],
+ "footnotes": [],
+ "annotations": [],
+ }
+ ],
+ "tables": [],
+ "key_value_items": [],
+ "form_items": [],
+ "pages": {"1": {"size": {"width": 595.2, "height": 841.92}, "image": None, "page_no": 1}},
+ }
+
+
+def test_traverse_pictures_requested_when_supported():
+ """The probe asks for traverse_pictures on a version that accepts it."""
+ module = _load_component_module()
+
+ def supports(traverse_pictures=False):
+ return None
+
+ assert module.traverse_pictures_kwargs(supports) == {"traverse_pictures": True}
+
+
+def test_traverse_pictures_omitted_when_unsupported():
+ """Older docling-core lacks the parameter; passing it would raise TypeError."""
+ module = _load_component_module()
+
+ def no_support(image_mode=None):
+ return None
+
+ assert module.traverse_pictures_kwargs(no_support) == {}
+
+
+def test_markdown_export_keeps_text_nested_under_a_picture():
+ """The real defect: OCR text under a PictureItem must survive the export."""
+ pytest.importorskip("docling_core")
+ from docling_core.types.doc.document import DoclingDocument
+
+ module = _load_component_module()
+ doc = DoclingDocument.model_validate(_scanned_page_document())
+
+ exported = doc.export_to_markdown(**module.traverse_pictures_kwargs(doc.export_to_markdown))
+
+ assert "Earned Leave (EL): 18 days annually." in exported
+ assert "Sick Leave (SL): 12 days per year." in exported
+
+
+def test_component_source_requests_traverse_pictures_at_every_call_site():
+ """Every export_to_markdown call must opt in, including the fallback paths."""
+ source = COMPONENT_PATH.read_text()
+
+ assert source.count("export_to_markdown(") == 4
+ assert source.count("traverse_pictures_kwargs(") == 5 # 1 definition + 4 call sites
+
+
+def test_flow_embeds_the_same_component_source():
+ """Langflow runs the copy stored in the flow, so it must not drift from the file."""
+ import json
+
+ flow = json.loads(
+ (Path(__file__).resolve().parents[2] / "flows" / "ingestion_flow.json").read_text()
+ )
+ embedded = next(
+ node["data"]["node"]["template"]["code"]["value"]
+ for node in flow["data"]["nodes"]
+ if node["data"]["id"] == "ExportDoclingDocument-zZdRg"
+ )
+
+ assert embedded == COMPONENT_PATH.read_text()
+
+
+def test_signature_probe_tolerates_builtins():
+ """inspect.signature raises on some callables; the probe must not blow up."""
+ module = _load_component_module()
+
+ try:
+ inspect.signature(len)
+ except (TypeError, ValueError):
+ assert module.traverse_pictures_kwargs(len) == {}