Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -429,6 +430,28 @@ public class ConvertDocumentOptions {
@lombok.Singular("vlmPipelineCustomConfigEntry")
private Map<String, Object> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,28 @@
* Represents the output format of a converted-document artifact.
*
* <ul>
* <li>{@code JSON}: Serialized {@code DoclingDocument} JSON.</li>
* <li>{@code HTML}: HTML rendering of the document.</li>
* <li>{@code MARKDOWN}: Markdown rendering of the document.</li>
* <li>{@code TEXT}: Plain-text rendering of the document.</li>
* <li>{@code DOCTAGS}: DocTags rendering of the document.</li>
* <li>{@code RESOURCE_BUNDLE}: ZIP archive containing extracted images and supporting resources.</li>
* <li>{@code JSON}: Serialized {@code DoclingDocument} JSON.</li>
* <li>{@code HTML}: HTML rendering of the document.</li>
* <li>{@code MARKDOWN}: Markdown rendering of the document.</li>
* <li>{@code TEXT}: Plain-text rendering of the document.</li>
* <li>{@code DOCTAGS}: DocTags rendering of the document.</li>
* <li>{@code RESOURCE_BUNDLE}: ZIP archive containing extracted images and supporting resources.</li>
* <li>{@code CHUNKS}: Chunk records produced by convert-side chunking.</li>
* </ul>
*/
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
* the converted document content is delivered to the client:
*
* <ul>
* <li>{@link #IN_BODY} - Content is embedded directly in the response body
* ({@link InBodyConvertDocumentResponse})</li>
* <li>{@link #ZIP_ARCHIVE} - Content is packaged and returned as a ZIP archive
* ({@link ZipArchiveConvertDocumentResponse})</li>
* <li>{@link #PRE_SIGNED_URL} - Content is uploaded to a client-supplied remote target and
* only aggregate processing statistics are returned.
* ({@link PreSignedUrlConvertDocumentResponse})</li>
* <li>{@link #PRE_SIGNED_URL_RESPONSE} - Each output artifact is uploaded to the server's
* configured object storage and returned as a time-limited presigned URL grouped by source document.
* ({@link PreSignedUrlConvertResponse})</li>
* <li>{@link #IN_BODY} - Content is embedded directly in the response body
* ({@link InBodyConvertDocumentResponse})</li>
* <li>{@link #ZIP_ARCHIVE} - Content is packaged and returned as a ZIP archive
* ({@link ZipArchiveConvertDocumentResponse})</li>
* <li>{@link #PRE_SIGNED_URL} - Content is uploaded to a client-supplied remote target and
* only aggregate processing statistics are returned.
* ({@link PreSignedUrlConvertDocumentResponse})</li>
* <li>{@link #PRE_SIGNED_URL_RESPONSE} - Each output artifact is uploaded to the server's
* configured object storage and returned as a time-limited presigned URL grouped by source document.
* ({@link PreSignedUrlConvertResponse})</li>
* </ul>
*
* @see ConvertDocumentResponse
Expand All @@ -28,7 +28,7 @@
* @see PreSignedUrlConvertResponse
*/
public enum ResponseType {

/**
* Represents response type - {@link InBodyConvertDocumentResponse}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

import org.junit.jupiter.api.Test;

import ai.docling.serve.api.chunk.request.options.HybridChunkerOptions;

/**
* Unit tests for {@link ConvertDocumentOptions}.
*/
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,14 @@ void createResponseWithAllFields() {
.build();

List<ErrorItem> 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<String, Object> timings = Map.of(
"parse_time", 0.8,
"convert_time", 0.7
"parse_time", 0.8, "convert_time", 0.7
);

InBodyConvertDocumentResponse response = InBodyConvertDocumentResponse.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading