Docling Document Reader
Spring AI provides a DocumentReader API to build an ingestion pipeline for loading, parsing, chunking, and storing documents to be used as context for scenarios such as Retrieval Augmented Generation (RAG) flows.
Arconia offers an implementation of the DocumentReader API (DoclingDocumentReader) that uses Docling as the engine for processing documents and making them ready to be stored in a vector database. It’s built on top of Arconia Docling, which provides seamless integration with Docling, a powerful AI-powered document conversion service that transforms documents into structured formats.
Quick Start
Let’s see how you can get started with the Docling Document Reader in your Spring AI application.
Dependencies
First, include the Docling Document Reader dependency in your project.
-
Gradle
-
Maven
dependencies {
implementation 'io.arconia:arconia-ai-docling-document-reader'
}
<dependency>
<groupId>io.arconia</groupId>
<artifactId>arconia-ai-docling-document-reader</artifactId>
</dependency>
Arconia publishes a BOM (Bill of Materials) that you can use to manage the version of the Arconia libraries. While not required, it is recommended to use the BOM to ensure that all dependencies are compatible.
-
Gradle
-
Maven
dependencyManagement {
imports {
mavenBom "io.arconia:arconia-bom:0.32.0"
}
}
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.arconia</groupId>
<artifactId>arconia-bom</artifactId>
<version>0.32.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Dev Services
Arconia Dev Services provide zero-code integrations for services your application depends on, both at development and test time, relying on the power of Testcontainers and Spring Boot.
When working with Docling Document Reader, you can use the Docling Dev Service to automatically start a Docling Serve instance during development and testing, giving you the possibility to parse documents without manually setting up a Docling Serve instance.
To enable the Docling Dev Service, add the following dependency to your project:
-
Gradle
-
Maven
dependencies {
testAndDevelopmentOnly "io.arconia:arconia-dev-services-docling"
}
<dependency>
<groupId>io.arconia</groupId>
<artifactId>arconia-dev-services-docling</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
By default, the Dev Service enables the Docling UI. The application logs will show you the URL where you can access that, together with the URLs of the OpenAPI, and Scalar documentation of the Docling Serve API.
...i.a.d.s.startup : Dev Service 'docling' links: Docling UI: http://localhost:<port>/ui, Docling OpenAPI: http://localhost:<port>/docs, Docling Scalar: http://localhost:<port>/scalar
Ingesting Documents with Docling
Given a story.pdf file, you can build a data ingestion pipeline as follows:
@Component
public class IngestionPipeline {
private final DoclingServeApi doclingServeApi;
private final VectorStore vectorStore;
public IngestionPipeline(DoclingServeApi doclingServeApi, VectorStore vectorStore) {
this.doclingServeApi = doclingServeApi;
this.vectorStore = vectorStore;
}
@PostConstruct
void run() {
Resource file = new ClassPathResource("story.pdf");
List<Document> documents = DoclingDocumentReader.builder()
.doclingServeApi(doclingServeApi)
.files(file)
.build()
.get();
vectorStore.add(documents);
}
}
The DoclingDocumentReader will use Docling to process the file, transform it into smaller chunks, and build a resulting collection of Spring AI’s Documents. Finally, you can use Spring AI’s VectorStore API to convert the documents into embeddings and store them into a vector database.
Docling Serve downloads the URL sources itself, from its own network. If the URLs come from the users of your application, validate them before passing them to the reader so that they can’t point Docling Serve to internal services.
All the files and URLs of a reader are sent to Docling Serve in a single request. If Docling Serve can’t convert one of them, the reader throws an IllegalStateException listing the sources that failed and the reasons, without returning the chunks of the other sources.
The reader waits for Docling Serve to process all the sources within the arconia.docling.read-timeout. For larger batches, split the sources across several readers.
| For more information on how to build a data ingestion pipeline in Spring AI, check out the dedicated documentation. |
Customizing the Reader
The reader accepts files as Spring Resource objects and documents to download as URLs, and you can combine them in the same reader. You can also customize how Docling converts and chunks them, and add metadata to every resulting document.
DoclingDocumentReader reader = DoclingDocumentReader.builder()
.doclingServeApi(doclingServeApi)
.files(new ClassPathResource("story.pdf"))
.urls("https://docs.arconia.io/arconia/latest/integrations/docling/")
.convertOptions(ConvertDocumentOptions.builder()
.doOcr(false)
.build())
.chunkerOptions(HybridChunkerOptions.builder()
.maxTokens(512)
.build())
.metadata(Map.of("collection", "stories"))
.build();
By default, the reader uses the hierarchical chunker, which creates one chunk for each element of the document structure, such as a paragraph or a list. The hybrid chunker refines those chunks to fit the token limit of your embedding model.
Each resulting document carries the following metadata, besides the one you provide. You can refer to the keys via the DocumentMetadata enumeration.
| Key | Description |
|---|---|
|
Name of the source file. |
|
Headings of the section the chunk belongs to, if any. |
|
Pages the chunk comes from, if any. |
|
Hash of the source file, which identifies the same file across ingestions. |
|
MIME type of the source file. |
|
URI of the source, when Docling Serve reports one. |
To build the documents differently, you can pass your own DoclingDocumentParser to the reader via documentParser().