diff --git a/docling-serve/docling-serve-api/src/main/java/ai/docling/serve/api/convert/request/options/ConvertDocumentOptions.java b/docling-serve/docling-serve-api/src/main/java/ai/docling/serve/api/convert/request/options/ConvertDocumentOptions.java index 6e69600a..65980cfc 100644 --- a/docling-serve/docling-serve-api/src/main/java/ai/docling/serve/api/convert/request/options/ConvertDocumentOptions.java +++ b/docling-serve/docling-serve-api/src/main/java/ai/docling/serve/api/convert/request/options/ConvertDocumentOptions.java @@ -14,6 +14,7 @@ import com.fasterxml.jackson.annotation.Nulls; import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import ai.docling.serve.api.chunk.request.options.ChunkerOptions; import ai.docling.serve.api.serialization.DurationSerializationFormat; import ai.docling.serve.api.serialization.Jackson2DurationSerializer; import ai.docling.serve.api.serialization.Jackson3DurationSerializer; @@ -429,6 +430,28 @@ public class ConvertDocumentOptions { @lombok.Singular("vlmPipelineCustomConfigEntry") private Map vlmPipelineCustomConfig; + /** + * Preset id for the chunker, for example {@code granite_embedding_278m}, registered by the Docling Serve deployment's chunker manager. This is a deployment-specific identifier, + * not the {@code chunker} discriminator used by {@link ai.docling.serve.api.chunk.request.options.ChunkerOptions} (e.g. {@code hybrid}/{@code hierarchical}). Mutually exclusive + * with {@link #getChunkingOptions()}. Only takes effect when {@link #getToFormats()} includes {@link OutputFormat#CHUNKS}, and only with a presigned target or a target that + * produces a ZIP archive (a {@link ai.docling.serve.api.convert.request.target.ZipTarget}, or multiple source documents with an + * {@link ai.docling.serve.api.convert.request.target.InBodyTarget}) — Docling Serve silently omits chunk output when chunking is requested with a single-document in-body + * target. + */ + @JsonProperty("chunking_preset") + @Nullable + private String chunkingPreset; + + /** + * Chunker configuration for convert-side chunking, performed in the same pass as the conversion. Mutually exclusive with {@link #getChunkingPreset()}. Only takes effect when + * {@link #getToFormats()} includes {@link OutputFormat#CHUNKS}, and only with a presigned target or a target that produces a ZIP archive (a + * {@link ai.docling.serve.api.convert.request.target.ZipTarget}, or multiple source documents with an {@link ai.docling.serve.api.convert.request.target.InBodyTarget}) — + * Docling Serve silently omits chunk output when chunking is requested with a single-document in-body target. + */ + @JsonProperty("chunking_options") + @Nullable + private ChunkerOptions chunkingOptions; + /** * Whether heading hierarchy inference is enabled. Derived from {@link #getHeadingHierarchyOptions()} * — {@code true} when options are present, {@code false} otherwise. diff --git a/docling-serve/docling-serve-api/src/main/java/ai/docling/serve/api/convert/request/options/OutputFormat.java b/docling-serve/docling-serve-api/src/main/java/ai/docling/serve/api/convert/request/options/OutputFormat.java index b23bdfb9..32f7a2ca 100644 --- a/docling-serve/docling-serve-api/src/main/java/ai/docling/serve/api/convert/request/options/OutputFormat.java +++ b/docling-serve/docling-serve-api/src/main/java/ai/docling/serve/api/convert/request/options/OutputFormat.java @@ -7,27 +7,29 @@ */ public enum OutputFormat { - @JsonProperty("dclx") - DCLX, @JsonProperty("doctags") DOCTAGS, - @JsonProperty("doclang") - DOCLANG, @JsonProperty("html") HTML, @JsonProperty("html_split_page") HTML_SPLIT_PAGE, @JsonProperty("json") JSON, - @JsonProperty("latex") - LATEX, @JsonProperty("md") MARKDOWN, @JsonProperty("text") TEXT, + @JsonProperty("dclx") + DCLX, + @JsonProperty("doclang") + DOCLANG, + @JsonProperty("latex") + LATEX, @JsonProperty("vtt") VTT, @JsonProperty("yaml") - YAML + YAML, + @JsonProperty("chunks") + CHUNKS } diff --git a/docling-serve/docling-serve-api/src/main/java/ai/docling/serve/api/convert/response/ArtifactType.java b/docling-serve/docling-serve-api/src/main/java/ai/docling/serve/api/convert/response/ArtifactType.java index 7610d031..2977cb7f 100644 --- a/docling-serve/docling-serve-api/src/main/java/ai/docling/serve/api/convert/response/ArtifactType.java +++ b/docling-serve/docling-serve-api/src/main/java/ai/docling/serve/api/convert/response/ArtifactType.java @@ -6,19 +6,28 @@ * Represents the output format of a converted-document artifact. * * */ public enum ArtifactType { - @JsonProperty("json") JSON, - @JsonProperty("html") HTML, - @JsonProperty("markdown") MARKDOWN, - @JsonProperty("text") TEXT, - @JsonProperty("doctags") DOCTAGS, - @JsonProperty("resource_bundle") RESOURCE_BUNDLE; + @JsonProperty("json") + JSON, + @JsonProperty("html") + HTML, + @JsonProperty("markdown") + MARKDOWN, + @JsonProperty("text") + TEXT, + @JsonProperty("doctags") + DOCTAGS, + @JsonProperty("resource_bundle") + RESOURCE_BUNDLE, + @JsonProperty("chunks") + CHUNKS; } diff --git a/docling-serve/docling-serve-api/src/main/java/ai/docling/serve/api/convert/response/ResponseType.java b/docling-serve/docling-serve-api/src/main/java/ai/docling/serve/api/convert/response/ResponseType.java index 4e7ad254..cc0f7a5b 100644 --- a/docling-serve/docling-serve-api/src/main/java/ai/docling/serve/api/convert/response/ResponseType.java +++ b/docling-serve/docling-serve-api/src/main/java/ai/docling/serve/api/convert/response/ResponseType.java @@ -9,16 +9,16 @@ * the converted document content is delivered to the client: * * * * @see ConvertDocumentResponse @@ -28,7 +28,7 @@ * @see PreSignedUrlConvertResponse */ public enum ResponseType { - + /** * Represents response type - {@link InBodyConvertDocumentResponse} * diff --git a/docling-serve/docling-serve-api/src/main/java/ai/docling/serve/api/serialization/Jackson2ConvertDocumentResponseDeserializer.java b/docling-serve/docling-serve-api/src/main/java/ai/docling/serve/api/serialization/Jackson2ConvertDocumentResponseDeserializer.java index 1fcb2d99..064166aa 100644 --- a/docling-serve/docling-serve-api/src/main/java/ai/docling/serve/api/serialization/Jackson2ConvertDocumentResponseDeserializer.java +++ b/docling-serve/docling-serve-api/src/main/java/ai/docling/serve/api/serialization/Jackson2ConvertDocumentResponseDeserializer.java @@ -22,28 +22,29 @@ public class Jackson2ConvertDocumentResponseDeserializer extends JsonDeserialize public ConvertDocumentResponse deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { var codec = p.getCodec(); if (!(codec instanceof ObjectMapper mapper)) { - throw JsonMappingException.from(p, - "Expected ObjectMapper codec for ConvertDocumentResponse deserialization"); + throw JsonMappingException.from(p, "Expected ObjectMapper codec for ConvertDocumentResponse deserialization"); } JsonNode tree = mapper.readTree(p); if (!(tree instanceof ObjectNode node)) { - throw JsonMappingException.from(p, - "Expected a JSON object for ConvertDocumentResponse but got " + tree.getNodeType()); + throw JsonMappingException.from(p, "Expected a JSON object for ConvertDocumentResponse but got " + tree.getNodeType()); } if (node.has("documents")) { return mapper.treeToValue(node, PreSignedUrlConvertResponse.class); - } else if (node.has("document")) { + } + else if (node.has("document")) { return mapper.treeToValue(node, InBodyConvertDocumentResponse.class); - } else if (node.has("file_name")) { + } + else if (node.has("file_name")) { return mapper.treeToValue(node, ZipArchiveConvertDocumentResponse.class); - } else if (node.has("num_converted")) { + } + else if (node.has("num_converted")) { return mapper.treeToValue(node, PreSignedUrlConvertDocumentResponse.class); } - throw JsonMappingException.from(p, - "Cannot determine ConvertDocumentResponse subtype: none of the expected fields (documents, document, file_name, num_converted) found in JSON"); + throw JsonMappingException + .from(p, "Cannot determine ConvertDocumentResponse subtype: none of the expected fields (documents, document, file_name, num_converted) found in JSON"); } } diff --git a/docling-serve/docling-serve-api/src/main/java/ai/docling/serve/api/serialization/Jackson3ConvertDocumentResponseDeserializer.java b/docling-serve/docling-serve-api/src/main/java/ai/docling/serve/api/serialization/Jackson3ConvertDocumentResponseDeserializer.java index fc152ee8..efc732a1 100644 --- a/docling-serve/docling-serve-api/src/main/java/ai/docling/serve/api/serialization/Jackson3ConvertDocumentResponseDeserializer.java +++ b/docling-serve/docling-serve-api/src/main/java/ai/docling/serve/api/serialization/Jackson3ConvertDocumentResponseDeserializer.java @@ -21,21 +21,23 @@ public ConvertDocumentResponse deserialize(JsonParser p, DeserializationContext JsonNode tree = ctxt.readTree(p); if (!(tree instanceof ObjectNode node)) { - throw DatabindException.from(p, - "Expected a JSON object for ConvertDocumentResponse but got " + tree.getNodeType()); + throw DatabindException.from(p, "Expected a JSON object for ConvertDocumentResponse but got " + tree.getNodeType()); } if (node.has("documents")) { return ctxt.readTreeAsValue(node, PreSignedUrlConvertResponse.class); - } else if (node.has("document")) { + } + else if (node.has("document")) { return ctxt.readTreeAsValue(node, InBodyConvertDocumentResponse.class); - } else if (node.has("file_name")) { + } + else if (node.has("file_name")) { return ctxt.readTreeAsValue(node, ZipArchiveConvertDocumentResponse.class); - } else if (node.has("num_converted")) { + } + else if (node.has("num_converted")) { return ctxt.readTreeAsValue(node, PreSignedUrlConvertDocumentResponse.class); } - throw DatabindException.from(p, - "Cannot determine ConvertDocumentResponse subtype: none of the expected fields (documents, document, file_name, num_converted) found in JSON"); + throw DatabindException + .from(p, "Cannot determine ConvertDocumentResponse subtype: none of the expected fields (documents, document, file_name, num_converted) found in JSON"); } } diff --git a/docling-serve/docling-serve-api/src/test/java/ai/docling/serve/api/convert/request/options/ConvertDocumentOptionsTests.java b/docling-serve/docling-serve-api/src/test/java/ai/docling/serve/api/convert/request/options/ConvertDocumentOptionsTests.java index 1db3cc2a..ec828480 100644 --- a/docling-serve/docling-serve-api/src/test/java/ai/docling/serve/api/convert/request/options/ConvertDocumentOptionsTests.java +++ b/docling-serve/docling-serve-api/src/test/java/ai/docling/serve/api/convert/request/options/ConvertDocumentOptionsTests.java @@ -10,6 +10,8 @@ import org.junit.jupiter.api.Test; +import ai.docling.serve.api.chunk.request.options.HybridChunkerOptions; + /** * Unit tests for {@link ConvertDocumentOptions}. */ @@ -184,6 +186,61 @@ void convertDocumentOptionsToBuilderKeepsPresetsAndCustomConfigs() { .containsExactly("easyocr", Map.of("kind", "docling_layout_default"), true, true, true); } + @Test + void convertDocumentOptionsWithChunkingPreset() { + var options = ConvertDocumentOptions.builder() + .toFormat(OutputFormat.CHUNKS) + .chunkingPreset("granite_embedding_278m") + .build(); + + assertThat(options) + .extracting(ConvertDocumentOptions::getChunkingPreset, ConvertDocumentOptions::getChunkingOptions) + .containsExactly("granite_embedding_278m", null); + assertThat(options.getToFormats()).containsExactly(OutputFormat.CHUNKS); + } + + @Test + void convertDocumentOptionsWithChunkingOptions() { + var chunkerOptions = HybridChunkerOptions.builder() + .useMarkdownTables(true) + .maxTokens(512) + .build(); + + var options = ConvertDocumentOptions.builder() + .toFormat(OutputFormat.CHUNKS) + .chunkingOptions(chunkerOptions) + .build(); + + assertThat(options) + .extracting(ConvertDocumentOptions::getChunkingPreset, ConvertDocumentOptions::getChunkingOptions) + .containsExactly(null, chunkerOptions); + } + + @Test + void convertDocumentOptionsChunkingDefaultsToNull() { + var options = ConvertDocumentOptions.builder().build(); + + assertThat(options) + .extracting(ConvertDocumentOptions::getChunkingPreset, ConvertDocumentOptions::getChunkingOptions) + .containsOnlyNulls(); + } + + @Test + void convertDocumentOptionsToBuilderKeepsChunkingOptions() { + var chunkerOptions = HybridChunkerOptions.builder().maxTokens(256).build(); + + var original = ConvertDocumentOptions.builder() + .toFormat(OutputFormat.CHUNKS) + .chunkingOptions(chunkerOptions) + .build(); + + var rebuilt = original.toBuilder().build(); + + assertThat(rebuilt) + .extracting(ConvertDocumentOptions::getChunkingPreset, ConvertDocumentOptions::getChunkingOptions) + .containsExactly(null, chunkerOptions); + } + @Test void convertDocumentOptionsToBuilderWithHeadingHierarchy() { var hierarchyOptions = HeadingHierarchyOptions.builder() diff --git a/docling-serve/docling-serve-api/src/test/java/ai/docling/serve/api/convert/response/ArtifactRefTests.java b/docling-serve/docling-serve-api/src/test/java/ai/docling/serve/api/convert/response/ArtifactRefTests.java index add672c6..2b56b6f8 100644 --- a/docling-serve/docling-serve-api/src/test/java/ai/docling/serve/api/convert/response/ArtifactRefTests.java +++ b/docling-serve/docling-serve-api/src/test/java/ai/docling/serve/api/convert/response/ArtifactRefTests.java @@ -57,4 +57,10 @@ void toBuilderRoundtripsToEqualInstance() { ArtifactRef roundtripped = FULL_REF.toBuilder().build(); assertThat(roundtripped).usingRecursiveComparison().isEqualTo(FULL_REF); } + + @Test + void supportsChunksArtifactType() { + ArtifactRef ref = FULL_REF.toBuilder().artifactType(ArtifactType.CHUNKS).build(); + assertThat(ref.getArtifactType()).isEqualTo(ArtifactType.CHUNKS); + } } diff --git a/docling-serve/docling-serve-api/src/test/java/ai/docling/serve/api/convert/response/ConvertDocumentResponseTests.java b/docling-serve/docling-serve-api/src/test/java/ai/docling/serve/api/convert/response/ConvertDocumentResponseTests.java index 77ffd6b9..1f59c8bc 100644 --- a/docling-serve/docling-serve-api/src/test/java/ai/docling/serve/api/convert/response/ConvertDocumentResponseTests.java +++ b/docling-serve/docling-serve-api/src/test/java/ai/docling/serve/api/convert/response/ConvertDocumentResponseTests.java @@ -32,15 +32,14 @@ void createResponseWithAllFields() { .build(); List errors = List.of( - ErrorItem.builder().componentType("parser").errorMessage("Parse error").moduleName("pdf_module").build(), - ErrorItem.builder().componentType("converter").errorMessage("Conversion warning").moduleName("html_module").build() + ErrorItem.builder().componentType("parser").errorMessage("Parse error").moduleName("pdf_module").build(), ErrorItem.builder().componentType("converter") + .errorMessage("Conversion warning").moduleName("html_module").build() ); Double processingTime = 1.5; String status = "success"; Map timings = Map.of( - "parse_time", 0.8, - "convert_time", 0.7 + "parse_time", 0.8, "convert_time", 0.7 ); InBodyConvertDocumentResponse response = InBodyConvertDocumentResponse.builder() diff --git a/docling-serve/docling-serve-client/src/test/java/ai/docling/serve/client/AbstractDoclingServeClientTests.java b/docling-serve/docling-serve-client/src/test/java/ai/docling/serve/client/AbstractDoclingServeClientTests.java index 0d25354a..17b39ae8 100644 --- a/docling-serve/docling-serve-client/src/test/java/ai/docling/serve/client/AbstractDoclingServeClientTests.java +++ b/docling-serve/docling-serve-client/src/test/java/ai/docling/serve/client/AbstractDoclingServeClientTests.java @@ -1462,6 +1462,59 @@ void shouldConvertSourceWithPresignedUrlTargetAndMultipleDocuments() { ); } + @Test + void shouldConvertSourceWithInBodyTargetAndChunkingIgnoringChunks() { + // Docling Serve silently omits chunk output when "chunks" is requested alongside a + // single-document in-body target, returning a plain in-body document response instead. + var request = ConvertDocumentRequest.builder() + .source(HttpSource.builder().url(URI.create("https://arxiv.org/pdf/2408.09869")).build()) + .options( + ConvertDocumentOptions.builder() + .toFormat(OutputFormat.CHUNKS) + .chunkingPreset("granite_embedding_278m") + .build() + ) + .build(); + + var wireMockServer = getWiremockServer(); + + wireMockServer.stubFor( + post("/v1/convert/source") + .withRequestBody(equalToJson(writeValueAsString(request))) + .withHeader("Content-Type", equalTo("application/json")) + .withHeader("Accept", equalTo("application/json")) + .willReturn(okJson(""" + { + "document": { + "filename": "2408.09869", + "md_content": "# Example" + }, + "status": "success", + "processing_time": 2.31 + } + """)) + ); + + var response = getDoclingClient(false, true).convertSource(request); + assertThat(response).isNotNull(); + assertThat(response.getResponseType()).isEqualTo(ResponseType.IN_BODY); + assertThat(response).isInstanceOf(InBodyConvertDocumentResponse.class); + + var inBodyResponse = (InBodyConvertDocumentResponse) response; + assertThat(inBodyResponse.getProcessingTime()).isEqualTo(2.31); + assertThat(inBodyResponse.getStatus()).isEqualTo("success"); + assertThat(inBodyResponse.getDocument().getFilename()).isEqualTo("2408.09869"); + assertThat(inBodyResponse.getDocument().getMarkdownContent()).isEqualTo("# Example"); + + wireMockServer.verify( + postRequestedFor(urlPathEqualTo("/v1/convert/source")) + .withRequestBody( + matchingJsonPath("$.options.to_formats[0]", equalTo("chunks")) + .and(matchingJsonPath("$.options.chunking_preset", equalTo("granite_embedding_278m"))) + ) + ); + } + @Test void shouldConvertSingleHttpSourceWithDefaultTargetSuccessfully() { var request = ConvertDocumentRequest.builder() diff --git a/docs/src/doc/docs/whats-new.md b/docs/src/doc/docs/whats-new.md index 9819ab96..b9cb08ad 100644 --- a/docs/src/doc/docs/whats-new.md +++ b/docs/src/doc/docs/whats-new.md @@ -27,6 +27,7 @@ Docling Java {{ gradle.project_version }} includes important breaking changes, a ### {{ gradle.project_version }} * **`OutputFormat` gains `YAML`, `VTT`, `DOCLANG`, `DCLX` and `LATEX`** — These were missing from the enum, so Docling Serve rejected `toFormats`/`toFormat` requests for them even though the server supports them. `DocumentResponse` also gains a `doclangContent` accessor, mirroring `doctagsContent`/`htmlContent`/etc., since Docling Serve returns `doclang_content` inline the same way it does for the existing formats. `DCLX` has no inline content field but is exported as a file, reachable through file-based targets such as a zip archive or a presigned URL. `YAML`, `VTT`, and `LATEX` are accepted by the server's request validation but currently have no implemented export at all, inline or file — requesting them succeeds but produces nothing for those formats. +* **Convert-side chunking** — Docling Serve can chunk a document in the same pass as the conversion, instead of converting and then sending the result to `/chunk`. `OutputFormat` gains `CHUNKS`, which must be included in `toFormats`/`toFormat` to activate it, and `ConvertDocumentOptions` gains `chunkingPreset` (`String`, a deployment-registered preset id such as `granite_embedding_278m`) and `chunkingOptions` (the existing `ChunkerOptions` used by the `/chunk` endpoint), mutually exclusive, configuring the chunker. With a presigned target, the chunks file comes back as a `chunks` artifact on a `DocumentArtifactItem`, which required adding `ArtifactType.CHUNKS`. With a target that produces a ZIP archive (a `ZipTarget`, or multiple source documents with an `InBodyTarget`), the chunks come back as a `*.chunks.jsonl` file alongside the other per-document output files inside the archive. Docling Serve silently omits chunk output when `chunks` is requested together with a single-document in-body target, so this option has no effect in that one case. * **`docling-testcontainers` default image bumped to `v1.41.0`** — `DoclingServeContainerConfig.DOCLING_IMAGE` now pins `ghcr.io/docling-project/docling-serve:v1.41.0` (previously `v1.35.0`). `ConvertDocumentOptions` is unchanged across this range, but one behavioral difference will show up for consumers relying on the previous default: a single-file conversion with `ZipTarget` now names the archive after the source file (e.g. `2408.09869.zip`) instead of the generic `converted_docs.zip`; multi-file conversions are unaffected. Preset registration remains image-specific — see the warning in [Per-stage presets and custom configurations](docling-serve/serve-api.md#per-stage-presets-and-custom-configurations). * **Per-stage presets and custom configurations on `ConvertDocumentOptions`** — Eight pipeline stages (layout, OCR, table structure, picture description, picture classification, code & formula, chart extraction and the VLM pipeline) can now be tuned from Java, each through a `Preset` string or a `CustomConfig` map, for example `ocrPreset("easyocr")` or `layoutCustomConfigEntry("kind", "docling_layout_default")`. Presets are plain strings because the available ids are set by the Docling Serve deployment, so not even `"default"` is registered for every stage on every image. A custom config is a `Map` with a stage-specific schema, in one of two shapes: the kind-selected stages (layout, OCR, table structure, picture classification) pick an implementation through a `kind` entry, while the VLM-backed stages (picture description, code & formula, chart extraction, VLM pipeline) take `model_spec` and `engine_options` entries instead. The preset and the custom config are mutually exclusive per stage — a custom config replaces the preset rather than overriding it. These supersede `ocrEngine`, `pictureDescriptionLocal`, `pictureDescriptionApi` and the `vlmPipelineModel*` options, which Docling Serve marks as deprecated. * **The options Docling Serve deprecates are now `@Deprecated` in Java** — `ocrEngine`, `pictureDescriptionLocal`, `pictureDescriptionApi`, `vlmPipelineModel`, `vlmPipelineModelLocal` and `vlmPipelineModelApi` carry `@Deprecated(since = "0.7.3")`, which Lombok copies onto both the getter and the builder setter, so setting one produces a compiler warning pointing at the preset or custom-config replacement. They are deliberately **not** marked `forRemoval`: these options still work, and when they are removed is Docling Serve's decision rather than this library's. The four types that are only reachable through those options — `OcrEngine`, `VlmModelType`, `PictureDescriptionLocal` and `PictureDescriptionApi` — are marked `@Deprecated` as well, so the warning also appears at `PictureDescriptionApi.builder()` rather than only at the setter.