Image Reference Handling#
ImageRef is the Pydantic model used throughout DoclingDocument to represent an image attached to a PictureItem or PageItem. It is defined in docling_core/types/doc/common/reference.py and carries four fields: mimetype, dpi, size (Size), and uri (Union[AnyUrl, Path]).
URI Schemes#
The uri field accepts three practical forms :
| Scheme | Example | Notes |
|---|---|---|
data: (data URI) | data:image/png;base64,… | Always accepted; decoded at access time |
file:// | file:///abs/path/to/img.png | Blocked by default; requires opt-in (see settings) |
Bare Path | /abs/path/to/img.png | Treated as a local filesystem path |
| HTTP/HTTPS | https://example.com/img.png | Intentionally never fetched (SSRF guard) |
Scheme handling is not enforced by a Pydantic validator on the field itself — it is enforced at runtime when .pil_image is accessed .
pil_image Property — Scheme Dispatch#
pil_image returns a PIL.Image.Image (or None) and implements all scheme-specific logic:
file://URI: Opens the file only ifsettings.allow_image_file_uriisTrue; otherwise raisesValueError. After the guard, calls_ensure_within_size_limit()before opening .data:URI: Splits on,, base64-decodes the payload, then checks the decoded byte length againstsettings.max_image_decoded_sizebefore opening with PIL .- Bare
Path: Calls_ensure_within_size_limit()then opens with PIL . - HTTP/HTTPS (and any other remote scheme): Explicitly not fetched. A source comment reads: "HTTP(S) and other remote schemes are intentionally not fetched. If remote fetch is enabled, it must use host allowlists and block private, link-local, and cloud-metadata addresses (SSRF controls)."
Construction Helper#
ImageRef.from_pil(image, dpi) is the canonical factory method for building an ImageRef from a PIL.Image. It encodes the image to PNG using cv2 (if installed) or PIL, produces a data:image/png;base64,… URI, and caches the PIL object in _pil to avoid re-decoding.
Security Settings#
Runtime behaviour is controlled by CoreSettings in docling_core/utils/settings.py, configurable via environment variables with the prefix DOCLINGCORE_:
| Setting | Default | Env var | Purpose |
|---|---|---|---|
allow_image_file_uri | False | DOCLINGCORE_ALLOW_IMAGE_FILE_URI | Permit file:// URIs in pil_image |
max_image_decoded_size | 20 MB | DOCLINGCORE_MAX_IMAGE_DECODED_SIZE | Cap on decoded image bytes for both data: and file:///Path URIs |
The size guard is implemented in _ensure_within_size_limit(path, *, max_size, label) in docling_core/types/doc/utils.py: it calls path.stat().st_size and raises ValueError if the file exceeds max_size.
MIME Type Validation#
The mimetype field is validated by validate_mimetype, which checks the value against mimetypes.types_map.values() and rejects unknown MIME types.
Key Source Files#
| File | Role |
|---|---|
docling_core/types/doc/common/reference.py | ImageRef model, from_pil() factory, pil_image property |
docling_core/utils/settings.py | CoreSettings: allow_image_file_uri, max_image_decoded_size |
docling_core/types/doc/utils.py | _ensure_within_size_limit() |
test/test_docling_doc.py (lines 811–896) | Tests for file:// blocking, data: size limits, env-var opt-in |