Docling Core
Docling Core is a Python library for defining, validating, and structuring data types and schemas. It's designed to align with the Docling specification, offering robust data validation capabilities. Currently at version 2.73.0, it undergoes frequent updates, often with daily or weekly releases in its 2.x series.
Warnings
- breaking The `Type` class was removed and replaced by `DataType`. Code using `from docling_core import Type` or instantiating `Type(...)` will break.
- breaking The primary exception class for validation errors was renamed from `ValidationError` to `ValidationErrors`.
- breaking The `description` argument for `DataType` classes was renamed to `_description` to avoid potential conflicts with user-defined fields.
- gotcha The `format` argument in `Field` has stricter validation in 2.x and only accepts specific predefined formats (like 'email', 'uri') or a callable validator. Arbitrary strings might no longer work as expected.
- gotcha By default, `DataType` is strict and will raise `ValidationErrors` if extra fields are provided in the input dictionary that are not defined in the `DataType` schema. Explicitly allow extra fields if needed.
Install
-
pip install docling-core
Imports
- DataType
from docling_core import DataType
- Field
from docling_core import Field
- ValidationErrors
from docling_core import ValidationErrors
Quickstart
from docling_core import Field, DataType
class User(DataType):
name = Field(str, description="The user's full name")
age = Field(int, min_value=0, description="The user's age")
email = Field(str, format="email", description="The user's email address")
user_data = {
"name": "John Doe",
"age": 30,
"email": "john.doe@example.com"
}
try:
user = User(user_data)
print(f"Validated user: {user.name}, {user.age}, {user.email}")
except Exception as e:
print(f"Validation error: {e}")
# Example of invalid data
invalid_user_data = {
"name": "Jane Doe",
"age": -5, # Invalid age
"email": "invalid-email"
}
try:
User(invalid_user_data)
except Exception as e:
print(f"Validation error for invalid data: {e}")