Skip to content

provider

Class info

Classes

Name Children Inherits
DoclingRemoteConverter
docler.converters.docling_remote_provider.provider
Document converter using remote Docling service.

    🛈 DocStrings

    Document converter using remote Docling service.

    DoclingRemoteConverter

    Bases: DocumentConverter[DoclingRemoteConfig]

    Document converter using remote Docling service.

    Source code in src/docler/converters/docling_remote_provider/provider.py
    class DoclingRemoteConverter(DocumentConverter[DoclingRemoteConfig]):
        """Document converter using remote Docling service."""
    
        Config = DoclingRemoteConfig
        NAME = "docling_remote"
        REQUIRED_PACKAGES: ClassVar = {"httpx2"}
        SUPPORTED_MIME_TYPES: ClassVar[set[str]] = {
            # PDF
            "application/pdf",
            # Office Documents
            "application/msword",  # .doc
            "application/vnd.openxmlformats-officedocument.wordprocessingml.document",  # .docx
            "application/vnd.ms-powerpoint",  # .ppt
            "application/vnd.openxmlformats-officedocument.presentationml.presentation",
            "application/vnd.ms-excel",  # .xls
            "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",  # .xlsx
            # Open/Libre Office
            "application/vnd.oasis.opendocument.text",  # .odt
            "application/vnd.oasis.opendocument.spreadsheet",  # .ods
            "application/vnd.oasis.opendocument.presentation",  # .odp
            # HTML/Text
            "text/html",
            "text/markdown",
            "text/asciidoc",
            # Images
            "image/jpeg",
            "image/png",
            "image/gif",
            "image/webp",
            "image/tiff",
        }
    
        def __init__(
            self,
            languages: list[SupportedLanguage] | None = None,
            *,
            page_range: PageRangeString | None = None,
            api_key: str | None = None,
            endpoint: str = DEFAULT_API_ENDPOINT,
            ocr_engine: DoclingEngine = "easy_ocr",
            pdf_backend: PDFBackend = "dlparse_v4",
            table_mode: TableMode = "fast",
            force_ocr: bool = False,
            image_scale: float = 2.0,
            do_table_structure: bool = True,
            do_code_enrichment: bool = False,
            do_formula_enrichment: bool = False,
            do_picture_classification: bool = False,
            do_picture_description: bool = False,
            timeout: float = 300.0,
        ) -> None:
            """Initialize the remote Docling converter.
    
            Args:
                languages: List of supported languages.
                page_range: Page range(s) to extract, like "1-5,7-10" (1-based; PDFs only)
                api_key: Optional API key for auth.
                endpoint: Base URL of the Docling service.
                ocr_engine: OCR engine to use (sent as docling-serve's `ocr_preset`).
                pdf_backend: PDF backend to use.
                table_mode: Table mode to use.
                force_ocr: Whether to force OCR even on digital documents.
                image_scale: Scale factor for image resolution.
                do_table_structure: Extract table structure.
                do_code_enrichment: Enable code extraction.
                do_formula_enrichment: Enable formula extraction.
                do_picture_classification: Enable picture classification.
                do_picture_description: Enable picture description.
                timeout: HTTP timeout in seconds for the conversion request.
    
            Raises:
                MissingConfigurationError: If endpoint cannot be used.
            """
            super().__init__(languages=languages, page_range=page_range)
            self.endpoint = endpoint.rstrip("/")
            self.api_key = api_key
            self.timeout = timeout
            # One multipart form field per option (lists repeat the field), as v1 expects.
            self.config: dict[str, Any] = {
                "from_formats": FROM_FORMATS,
                "to_formats": ["md"],
                "image_export_mode": "embedded",
                "do_ocr": True,
                "force_ocr": force_ocr,
                "ocr_preset": OCR_PRESETS[ocr_engine],
                "pdf_backend": pdf_backend,
                "table_mode": table_mode,
                "abort_on_error": False,
                "do_table_structure": do_table_structure,
                "do_code_enrichment": do_code_enrichment,
                "do_formula_enrichment": do_formula_enrichment,
                "do_picture_classification": do_picture_classification,
                "do_picture_description": do_picture_description,
                "images_scale": image_scale,
                "include_images": True,
            }
            if languages:
                self.config["ocr_lang"] = list(languages)
    
        async def _convert_async(self, data: BytesIO, mime_type: MimeType) -> ConverterResult:
            """Convert a document using remote Docling service.
    
            Args:
                data: File content as BytesIO.
                mime_type: MIME type of the file.
    
            Returns:
                Intermediate conversion result.
    
            Raises:
                ValueError: If conversion fails or response is malformed.
            """
            headers = {}
            if self.api_key:
                headers["Authorization"] = f"Bearer {self.api_key}"
    
            file_content = data.read()
            form = _form_fields(self.config)
            if self.page_range:
                if mime_type == "application/pdf":
                    from docler.converters.docling_provider.utils import to_docling_page_range
    
                    if span := to_docling_page_range(self.page_range):
                        form["page_range"] = [str(span[0]), str(span[1])]
                    else:
                        from docler.pdf_utils import extract_pdf_pages
    
                        # v1's `page_range` takes one contiguous range: cut the pages locally.
                        file_content = await anyenv.run_in_thread(
                            extract_pdf_pages, file_content, self.page_range
                        )
                else:
                    logger.warning("page_range only applies to PDFs; converting all of %s", mime_type)
            # Use a generic filename since we don't have path info
            ext = mime_type.split("/")[-1]
            filename = f"document.{ext}"
            url = f"{self.endpoint}{CONVERT_FILE}"
            async with httpx2.AsyncClient(timeout=self.timeout) as client:
                files = {"files": (filename, file_content, mime_type)}
                try:
                    response = await client.post(url, headers=headers, files=files, data=form)
                    response.raise_for_status()
                    result = response.json()
                except httpx2.HTTPError as e:
                    msg = f"Docling service error: {e}"
                    raise ValueError(msg) from e
                except Exception as e:
                    msg = f"Failed to convert document via Docling service: {e}"
                    self.logger.exception(msg)
                    raise ValueError(msg) from e
    
            document = result.get("document") or {}
            if not document.get("md_content"):
                if result.get("errors"):
                    msg = f"Conversion failed: {result['errors']}"
                else:
                    msg = "No markdown content found in response"
                raise ValueError(msg)
            content, images = process_response(document)
            return ConverterResult(
                content=content,
                images=images,
                metadata=result.get("timings", {}),
            )
    

    __init__

    __init__(
        languages: list[SupportedLanguage] | None = None,
        *,
        page_range: PageRangeString | None = None,
        api_key: str | None = None,
        endpoint: str = DEFAULT_API_ENDPOINT,
        ocr_engine: DoclingEngine = "easy_ocr",
        pdf_backend: PDFBackend = "dlparse_v4",
        table_mode: TableMode = "fast",
        force_ocr: bool = False,
        image_scale: float = 2.0,
        do_table_structure: bool = True,
        do_code_enrichment: bool = False,
        do_formula_enrichment: bool = False,
        do_picture_classification: bool = False,
        do_picture_description: bool = False,
        timeout: float = 300.0
    ) -> None
    

    Initialize the remote Docling converter.

    Parameters:

    Name Type Description Default
    languages list[SupportedLanguage] | None

    List of supported languages.

    None
    page_range PageRangeString | None

    Page range(s) to extract, like "1-5,7-10" (1-based; PDFs only)

    None
    api_key str | None

    Optional API key for auth.

    None
    endpoint str

    Base URL of the Docling service.

    DEFAULT_API_ENDPOINT
    ocr_engine DoclingEngine

    OCR engine to use (sent as docling-serve's ocr_preset).

    'easy_ocr'
    pdf_backend PDFBackend

    PDF backend to use.

    'dlparse_v4'
    table_mode TableMode

    Table mode to use.

    'fast'
    force_ocr bool

    Whether to force OCR even on digital documents.

    False
    image_scale float

    Scale factor for image resolution.

    2.0
    do_table_structure bool

    Extract table structure.

    True
    do_code_enrichment bool

    Enable code extraction.

    False
    do_formula_enrichment bool

    Enable formula extraction.

    False
    do_picture_classification bool

    Enable picture classification.

    False
    do_picture_description bool

    Enable picture description.

    False
    timeout float

    HTTP timeout in seconds for the conversion request.

    300.0

    Raises:

    Type Description
    MissingConfigurationError

    If endpoint cannot be used.

    Source code in src/docler/converters/docling_remote_provider/provider.py
    def __init__(
        self,
        languages: list[SupportedLanguage] | None = None,
        *,
        page_range: PageRangeString | None = None,
        api_key: str | None = None,
        endpoint: str = DEFAULT_API_ENDPOINT,
        ocr_engine: DoclingEngine = "easy_ocr",
        pdf_backend: PDFBackend = "dlparse_v4",
        table_mode: TableMode = "fast",
        force_ocr: bool = False,
        image_scale: float = 2.0,
        do_table_structure: bool = True,
        do_code_enrichment: bool = False,
        do_formula_enrichment: bool = False,
        do_picture_classification: bool = False,
        do_picture_description: bool = False,
        timeout: float = 300.0,
    ) -> None:
        """Initialize the remote Docling converter.
    
        Args:
            languages: List of supported languages.
            page_range: Page range(s) to extract, like "1-5,7-10" (1-based; PDFs only)
            api_key: Optional API key for auth.
            endpoint: Base URL of the Docling service.
            ocr_engine: OCR engine to use (sent as docling-serve's `ocr_preset`).
            pdf_backend: PDF backend to use.
            table_mode: Table mode to use.
            force_ocr: Whether to force OCR even on digital documents.
            image_scale: Scale factor for image resolution.
            do_table_structure: Extract table structure.
            do_code_enrichment: Enable code extraction.
            do_formula_enrichment: Enable formula extraction.
            do_picture_classification: Enable picture classification.
            do_picture_description: Enable picture description.
            timeout: HTTP timeout in seconds for the conversion request.
    
        Raises:
            MissingConfigurationError: If endpoint cannot be used.
        """
        super().__init__(languages=languages, page_range=page_range)
        self.endpoint = endpoint.rstrip("/")
        self.api_key = api_key
        self.timeout = timeout
        # One multipart form field per option (lists repeat the field), as v1 expects.
        self.config: dict[str, Any] = {
            "from_formats": FROM_FORMATS,
            "to_formats": ["md"],
            "image_export_mode": "embedded",
            "do_ocr": True,
            "force_ocr": force_ocr,
            "ocr_preset": OCR_PRESETS[ocr_engine],
            "pdf_backend": pdf_backend,
            "table_mode": table_mode,
            "abort_on_error": False,
            "do_table_structure": do_table_structure,
            "do_code_enrichment": do_code_enrichment,
            "do_formula_enrichment": do_formula_enrichment,
            "do_picture_classification": do_picture_classification,
            "do_picture_description": do_picture_description,
            "images_scale": image_scale,
            "include_images": True,
        }
        if languages:
            self.config["ocr_lang"] = list(languages)