Skip to content

Commit 56b3c42

Browse files
authored
fix: DoclingDocument model validator should deal with any raw input (#419)
* fix: DoclingDocument model validator should deal with any raw input Signed-off-by: Cesar Berrospi Ramis <[email protected]> * docs: mention 3.14 as valid Python version for development Signed-off-by: Cesar Berrospi Ramis <[email protected]> --------- Signed-off-by: Cesar Berrospi Ramis <[email protected]>
1 parent d52bd37 commit 56b3c42

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Docling Core
22

33
[![PyPI version](https://img.shields.io/pypi/v/docling-core)](https://pypi.org/project/docling-core/)
4-
![Python](https://img.shields.io/badge/python-3.9%20%7C%203.10%20%7C%20%203.11%20%7C%203.12%20%7C%203.13-blue)
4+
![Python](https://img.shields.io/badge/python-3.9%20%7C%203.10%20%7C%20%203.11%20%7C%203.12%20%7C%203.13%20%7C%203.14-blue)
55
[![uv](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json)](https://github.com/astral-sh/uv)
66
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
77
[![Imports: isort](https://img.shields.io/badge/%20imports-isort-%231674b1?style=flat&labelColor=ef8336)](https://pycqa.github.io/isort/)
@@ -21,7 +21,7 @@ pip install docling-core
2121

2222
### Development setup
2323

24-
To develop for Docling Core, you need Python 3.9 / 3.10 / 3.11 / 3.12 / 3.13 and uv. You can then install from your local clone's root dir:
24+
To develop for Docling Core, you need Python3.9 through 3.14 and the `uv` package. You can then install it from your local clone's root directory:
2525
```bash
2626
uv sync --all-extras
2727
```

docling_core/types/doc/document.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2308,12 +2308,12 @@ class DoclingDocument(BaseModel):
23082308

23092309
@model_validator(mode="before")
23102310
@classmethod
2311-
def transform_to_content_layer(cls, data: dict) -> dict:
2311+
def transform_to_content_layer(cls, data: Any) -> Any:
23122312
"""transform_to_content_layer."""
23132313
# Since version 1.1.0, all NodeItems carry content_layer property.
23142314
# We must assign previous page_header and page_footer instances to furniture.
23152315
# Note: model_validators which check on the version must use "before".
2316-
if "version" in data and data["version"] == "1.0.0":
2316+
if isinstance(data, dict) and data.get("version", "") == "1.0.0":
23172317
for item in data.get("texts", []):
23182318
if "label" in item and item["label"] in [
23192319
DocItemLabel.PAGE_HEADER.value,

test/test_docling_doc.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import yaml
1010
from PIL import Image as PILImage
1111
from PIL import ImageDraw
12-
from pydantic import AnyUrl, ValidationError
12+
from pydantic import AnyUrl, BaseModel, ValidationError
1313

1414
from docling_core.types.doc.base import BoundingBox, CoordOrigin, ImageRefMode, Size
1515
from docling_core.types.doc.document import ( # BoundingBox,
@@ -1156,6 +1156,14 @@ def test_upgrade_content_layer_from_1_0_0():
11561156
assert doc.version == CURRENT_VERSION
11571157
assert doc.texts[0].content_layer == ContentLayer.FURNITURE
11581158

1159+
# test that transform_to_content_layer model validator can handle any data type
1160+
class ContentOutput(BaseModel):
1161+
content: Union[str, DoclingDocument]
1162+
1163+
co = ContentOutput.model_validate_json('{"content": "Random string with version"}')
1164+
assert co
1165+
assert isinstance(co.content, str)
1166+
11591167

11601168
def test_version_doc():
11611169

0 commit comments

Comments
 (0)