Skip to main content

A Python SDK for interacting with Docling Serve API using Pydantic models

Project description

Docling Serve SDK

A comprehensive Python SDK for interacting with Docling Serve API using Pydantic models. This SDK provides type-safe, async/sync support for document conversion, chunking, and processing.

PyPI version Python 3.8+ License: MIT

Author: Alberto Ferrer
Email: albertof@barrahome.org
Repository: https://github.com/bet0x/docling-serve-sdk
PyPI: https://pypi.org/project/docling-serve-sdk/

Features

  • Document Conversion: PDF, DOCX, PPTX, HTML, images, and more
  • Multiple Source Types: Local files, HTTP URLs, S3 storage
  • Flexible Output: In-body, ZIP, S3, PUT targets
  • OCR Processing: Multiple engines (EasyOCR, Tesseract, etc.)
  • Table Extraction: Structure analysis and cell matching
  • Image Handling: Processing, scaling, and embedding
  • Chunking: Hierarchical and hybrid document chunking
  • Async/Sync Support: Both synchronous and asynchronous operations
  • Type Safety: Full Pydantic model validation
  • Error Handling: Comprehensive exception handling
  • Production Ready: Connection pooling, retries, timeouts

Installation

From PyPI (Recommended)

pip install docling-serve-sdk

From Source

# Clone the repository
git clone https://github.com/bet0x/docling-serve-sdk.git
cd docling-serve-sdk

# Install with pip
pip install -e .

# Or with uv
uv pip install -e .

Requirements

  • Python 3.8+
  • httpx >= 0.24.0
  • pydantic >= 2.0.0

Quick Start

from docling_serve_sdk import DoclingClient

# Create client
client = DoclingClient(base_url="http://localhost:5001")

# Check health
health = client.health_check()
print(f"Status: {health.status}")

# Convert document
result = client.convert_file("document.pdf")
print(f"Content: {result.document['md_content']}")

Documentation

📖 Complete Usage Guide - Comprehensive examples and advanced usage patterns

Examples

Basic Conversion

from docling_serve_sdk import DoclingClient

client = DoclingClient(base_url="http://localhost:5001")
result = client.convert_file("document.pdf")

print(f"Status: {result.status}")
print(f"Processing time: {result.processing_time:.2f}s")
print(f"Content: {result.document['md_content']}")

Advanced Usage with Multiple Sources

from docling_serve_sdk import (
    DoclingClient, ConvertDocumentsRequest, ConvertDocumentsRequestOptions,
    FileSourceRequest, HttpSourceRequest, ZipTarget,
    InputFormat, OutputFormat
)
import base64

# Create file source
with open("document.pdf", "rb") as f:
    content = base64.b64encode(f.read()).decode('utf-8')

file_source = FileSourceRequest(base64_string=content, filename="document.pdf")
http_source = HttpSourceRequest(url="https://example.com/doc.pdf")

# Create request with multiple sources
request = ConvertDocumentsRequest(
    sources=[file_source, http_source],
    options=ConvertDocumentsRequestOptions(
        from_formats=[InputFormat.PDF, InputFormat.DOCX],
        to_formats=[OutputFormat.MD, OutputFormat.HTML],
        do_ocr=True,
        include_images=True
    ),
    target=ZipTarget()
)

Async Usage

import asyncio
from docling_serve_sdk import DoclingClient

async def convert_document():
    client = DoclingClient(base_url="http://localhost:5001")
    
    # Check health
    health = await client.health_check_async()
    print(f"Status: {health.status}")
    
    # Convert document
    result = await client.convert_file_async("document.pdf")
    print(f"Content: {result.document['md_content']}")

# Run async function
asyncio.run(convert_document())

Error Handling

from docling_serve_sdk import DoclingClient, DoclingError, DoclingAPIError

client = DoclingClient(base_url="http://localhost:5001")

try:
    result = client.convert_file("document.pdf")
    print(f"Success: {result.status}")
except DoclingError as e:
    print(f"Docling error: {e}")
except DoclingAPIError as e:
    print(f"API error: {e}")
    print(f"Status code: {e.status_code}")
except Exception as e:
    print(f"Unexpected error: {e}")

Configuration Options

ConvertDocumentsRequestOptions

Option Type Default Description
from_formats List[InputFormat] All formats Input formats to accept
to_formats List[OutputFormat] [MD] Output formats to generate
image_export_mode ImageRefMode EMBEDDED How to handle images
do_ocr bool True Enable OCR processing
force_ocr bool False Force OCR over existing text
ocr_engine OCREngine EASYOCR OCR engine to use
pdf_backend PdfBackend DLPARSE_V4 PDF processing backend
table_mode TableMode ACCURATE Table processing mode
include_images bool True Include images in output
images_scale float 2.0 Image scale factor

Supported Formats

Input Formats:

  • PDF, DOCX, PPTX, HTML, MD, CSV, XLSX
  • Images (PNG, JPG, etc.)
  • XML (USPTO, JATS)
  • Audio files

Output Formats:

  • Markdown (MD)
  • HTML
  • JSON
  • Text
  • DocTags

API Reference

Core Classes

  • DoclingClient: Main client for API interactions
  • ConvertDocumentsRequest: Request model for document conversion
  • ConvertDocumentsRequestOptions: Configuration options
  • ConvertDocumentResponse: Response model for conversions

Source Types

  • FileSourceRequest: Local files (base64 encoded)
  • HttpSourceRequest: HTTP/HTTPS URLs
  • S3SourceRequest: S3-compatible storage

Target Types

  • InBodyTarget: Return in response body (default)
  • ZipTarget: Return as ZIP file
  • S3Target: Upload to S3
  • PutTarget: Upload via PUT request

Chunking

  • HierarchicalChunkerOptions: Hierarchical document chunking
  • HybridChunkerOptions: Hybrid semantic chunking

Testing

# Run basic tests
uv run python test_sdk.py

# Run new features tests
uv run python test_new_features.py

# Run integration tests
uv run python test_client_integration.py

# Or with pytest
pytest test_*.py

License

MIT License

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests
  5. Submit a pull request

Support

For issues and questions:

Changelog

v1.1.0 (Latest)

  • ✅ Added complete API model coverage
  • ✅ Added multiple source types (File, HTTP, S3)
  • ✅ Added multiple target types (InBody, ZIP, S3, PUT)
  • ✅ Added chunking options (Hierarchical, Hybrid)
  • ✅ Added comprehensive error handling
  • ✅ Added async/sync support
  • ✅ Added complete documentation

v1.0.0

  • ✅ Initial release
  • ✅ Basic document conversion
  • ✅ Health check functionality
  • ✅ Custom options support

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

docling_serve_sdk-1.2.0.tar.gz (13.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

docling_serve_sdk-1.2.0-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file docling_serve_sdk-1.2.0.tar.gz.

File metadata

  • Download URL: docling_serve_sdk-1.2.0.tar.gz
  • Upload date:
  • Size: 13.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.18

File hashes

Hashes for docling_serve_sdk-1.2.0.tar.gz
Algorithm Hash digest
SHA256 b5aecfc253f09c61d5e7f4b5df803d1ef9bec9aba35ba63f9676c7cf44c29253
MD5 83b237e324218facf290688437b09f25
BLAKE2b-256 93d632a61997b9546086a3ff9a1386d263ac0fc64f2c2ec0f01ed92d7f7e397e

See more details on using hashes here.

File details

Details for the file docling_serve_sdk-1.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for docling_serve_sdk-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d41e04871385156237ce822438196cd9455b24023951c267d7e87dab80cc6b63
MD5 bdb09bd834d000f7b39a274e0a8ff488
BLAKE2b-256 9187cec4e7b5193871b25c4dd3a6d19a3c040845e2b053022dd0d52245fe64b4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page