# docling-project/docling-serve architecture

> Architecture diagram of the GitHub repository docling-project/docling-serve, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/docling-project/docling-serve
- Repository: https://github.com/docling-project/docling-serve

## Overview

##### Project Overview and Purpose
- The project is a backend API service (with an optional UI) built to run Docling, an AI-driven document conversion toolkit.
- It exposes REST endpoints (with API docs at “/docs” and UI at “/ui”) that accept document conversion requests (via file uploads or URLs) and respond with converted outputs.
- The service is containerizable (as seen by Containerfile, Docker and Podman instructions) and supports different flavors (CPU, CUDA, etc.).

##### Key Components and Their Placement
- API Server:
- Represented by files like docling_serve/app.py and __main__.py. This is the core service entry point that handles HTTP requests.
- HTTP endpoint exposure (e.g., “/v1alpha/convert/source”), swagger documentation, and maybe the optional UI endpoint.
- Data Modeling and Conversion Logic:
- The datamodel directory contains definitions and helper functions for conversion requests and responses (e.g. convert.py, requests.py, responses.py).
- There’s also a specific module (docling_conversion.py) that likely coordinates the conversion workflow.
- Conversion Engines:
- Two distinct engine submodules exist within docling_serve/engines:
- async_local – likely handles asynchronous conversion tasks. Contains components such as an orchestrator (orchestrator.py) and worker functions (worker.py).
- block_local – probably deals with synchronous or blocking processing of conversions.
- There is also a base orchestrator (engines/base_orchestrator.py) that may provide common orchestration logic.
- UI Layer:
- The optional UI integration is implied by the gradio_ui.py file and referenced in the README. It provides a demonstration web interface (likely built with Gradio) when enabled.
- Configuration and Deployment:
- The presence of configuration files (e.g. .env.example, settings.py, various GitHub workflows) and documentation files (in docs/) should be noted, as they help set up deployment options and runtime behavior.

##### Relationships and Data Flow
- Clients (or users) send HTTP requests (via curl, Podman, etc.) to the API server.
- The API server (app.py) routes these requests to internal modules:
- It first uses datamodel modules to validate/parse the request data.
- Depending on the conversion type or selected configuration, it delegates the conversion task to one of the conversion engines (async_local or block_local).
- The orchestrator in async_local (or, alternatively, the block_local engine) processes the conversion using worker routines.
- Once processed, the conversion results are prepared and then returned through the API’s response mechanism.
- If UI functionality is enabled, an additional route (handled by gradio_ui.py) serves a user interface at the “/ui” endpoint.

##### Architectural Patterns and Key Design Principles
- Modularity: The system is neatly divided into submodules handling HTTP serving, data modeling, conversion orchestration, and engine-specific processing.
- Separation of Concerns: API routing is independent from conversion logic and engine interactions, allowing flexibility (for example, switching between async and block processing).
- Extensibility: Using separate “engines” subdirectories allows further extension or replacement of conversion techniques.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Clients
    Clients["External Clients (HTTP Requests)"]:::external

    %% Configuration & Deployment subgraph
    subgraph "Configuration & Deployment"
        Env[".env.example"]:::config
        Settings["settings.py"]:::config
        Container["Containerfile"]:::config
        Docs["Documentation (docs/)"]:::config
    end

    %% API Server Module subgraph
    subgraph "API Server Module"
        App["API Server: app.py"]:::api
        Main["API Server: __main__.py"]:::api
    end

    %% Data Modeling & Conversion subgraph
    subgraph "Data Modeling & Conversion"
        DataModel["Datamodel Folder"]:::conversion
        Controller["Conversion Controller (docling_conversion.py)"]:::conversion
    end

    %% Decision node for engine delegation
    Decision["Conversion Type?"]:::conversion

    %% Async Conversion Engine subgraph
    subgraph "Async Conversion Engine"
        AsyncOrch["Async Orchestrator"]:::engine
        Worker["Worker"]:::engine
    end

    %% Blocking Conversion Engine subgraph
    subgraph "Blocking Conversion Engine"
        Block["Blocking Engine"]:::engine
    end

    %% Base Orchestrator node
    BaseOrch["Base Orchestrator"]:::engine

    %% Optional UI Service
    UI["Optional UI Service (gradio_ui.py)"]:::ui

    %% Flow connections
    Env --> App
    Settings --> App
    Container --> App
    Docs --> App

    Clients --> App
    App --> DataModel
    App --> Controller
    Controller --> Decision

    Decision -- "async" --> AsyncOrch
    Decision -- "block" --> Block

    AsyncOrch --> Worker
    AsyncOrch --- BaseOrch
    Block --- BaseOrch

    AsyncOrch --> Controller
    Worker --> Controller
    Block --> Controller

    Controller --> App
    App --> Clients

    App --> UI
    Clients --> UI

    %% Click Events for API Server Module
    click App "https://github.com/docling-project/docling-serve/blob/main/docling_serve/app.py"
    click Main "https://github.com/docling-project/docling-serve/blob/main/docling_serve/__main__.py"

    %% Click Events for Data Modeling & Conversion
    click DataModel "https://github.com/docling-project/docling-serve/tree/main/docling_serve/datamodel"
    click Controller "https://github.com/docling-project/docling-serve/blob/main/docling_serve/docling_conversion.py"

    %% Click Events for Async Conversion Engine
    click AsyncOrch "https://github.com/docling-project/docling-serve/blob/main/docling_serve/engines/async_local/orchestrator.py"
    click Worker "https://github.com/docling-project/docling-serve/blob/main/docling_serve/engines/async_local/worker.py"

    %% Click Event for Blocking Conversion Engine
    click Block "https://github.com/docling-project/docling-serve/tree/main/docling_serve/engines/block_local"

    %% Click Event for Base Orchestrator
    click BaseOrch "https://github.com/docling-project/docling-serve/blob/main/docling_serve/engines/base_orchestrator.py"

    %% Click Event for Optional UI Service
    click UI "https://github.com/docling-project/docling-serve/blob/main/docling_serve/gradio_ui.py"

    %% Click Events for Configuration & Deployment
    click Env "https://github.com/docling-project/docling-serve/blob/main/.env.example"
    click Settings "https://github.com/docling-project/docling-serve/blob/main/docling_serve/settings.py"
    click Container "https://github.com/docling-project/docling-serve/tree/main/Containerfile"
    click Docs "https://github.com/docling-project/docling-serve/tree/main/docs/"

    %% Styles
    classDef external fill:#AED6F1,stroke:#2471A3,stroke-width:2px,color:#000;
    classDef api fill:#A3E4D7,stroke:#117864,stroke-width:2px,color:#000;
    classDef conversion fill:#F9E79F,stroke:#B7950B,stroke-width:2px,color:#000;
    classDef engine fill:#FAD7A0,stroke:#E67E22,stroke-width:2px,color:#000;
    classDef ui fill:#F5B7B1,stroke:#C0392B,stroke-width:2px,color:#000;
    classDef config fill:#D5DBDB,stroke:#7B7D7D,stroke-width:2px,color:#000;
```
