Skip to main content

Merge Schema Service

The MergeSchemaService provides functionality to combine multiple document schemas into a single unified schema. It intelligently merges field definitions, descriptions, and structures from different schemas to create a comprehensive schema that covers all input documents.

Quick Start

MergeSchemaService service = MergeSchemaService.builder()
.apiKey(WATSONX_API_KEY)
.projectId(WATSONX_PROJECT_ID)
.baseUrl(CloudRegion.DALLAS)
.build();

// Define schemas to merge
List<Schema> schemas = List.of(
Schema.builder()
.documentType("Passport")
.documentDescription("Passport document")
.fields(
KvpFields.builder()
.add("name", KvpField.of("Holder's name", "John"))
.build()
)
.build(),
Schema.builder()
.documentType("National ID Card")
.documentDescription("National ID Card document")
.fields(
KvpFields.builder()
.add("id", KvpField.of("ID number", "ABC123"))
.build()
)
.build()
);

// Merge the schemas
var result = service.mergeSchemaAndFetch(schemas);
System.out.println("Merged Type: " + result.schema().documentType());
System.out.println("Merged Description: " + result.schema().documentDescription());
// → Merged Type: Identification Document
// → Merged Description: Identification documents including Passports and
// National ID Cards, which are government-issued...

Overview

The MergeSchemaService enables you to:

  • Combine multiple schemas into a single unified schema
  • Merge field definitions from different document types
  • Create comprehensive descriptions that encompass all input schemas
  • Optimize schema structure for better extraction across document variants

Service Configuration

Basic Setup

MergeSchemaService service = MergeSchemaService.builder()
.apiKey(WATSONX_API_KEY)
.projectId(WATSONX_PROJECT_ID)
.baseUrl(CloudRegion.DALLAS)
.build();

Builder Parameters

ParameterTypeRequiredDescription
apiKeyStringConditionalAPI key for IBM Cloud authentication
authenticatorAuthenticatorConditionalCustom authentication (alternative to apiKey)
projectIdStringConditionalProject ID where schema merge will be performed
spaceIdStringConditionalSpace ID (alternative to projectId)
baseUrlString/CloudRegionYeswatsonx.ai service base URL
timeoutDurationNoRequest timeout (default: 60 seconds)
logRequestsBooleanNoEnable request logging (default: false)
logResponsesBooleanNoEnable response logging (default: false)
httpClientHttpClientNoCustom HTTP client
verifySslBooleanNoSSL certificate verification (default: true)
versionStringNoAPI version override

Either apiKey or authenticator must be provided. Either projectId or spaceId must be specified.


Examples

Basic Schema Merge

The simplest way to merge schemas is to use the mergeSchemaAndFetch method:

List<Schema> schemas = List.of(
Schema.builder()
.documentType("Invoice")
.documentDescription("Commercial invoice")
.fields(
KvpFields.builder()
.add("invoice_number", KvpField.of("Invoice number", "INV-001"))
.add("total", KvpField.of("Total amount", "1000.00"))
.build()
)
.build(),
Schema.builder()
.documentType("Receipt")
.documentDescription("Payment receipt")
.fields(
KvpFields.builder()
.add("receipt_number", KvpField.of("Receipt number", "REC-001"))
.add("amount", KvpField.of("Payment amount", "500.00"))
.build()
)
.build()
);

var result = service.mergeSchemaAndFetch(schemas);

System.out.println("Merged Schema:");
System.out.println("Document Type: " + result.schema().documentType());
System.out.println("Description: " + result.schema().documentDescription());
System.out.println("Fields: " + result.schema().fields().size());

With Custom Parameters

Configure the merge process with custom parameters:

MergeSchemaSemanticConfig semanticConfig = MergeSchemaSemanticConfig.builder()
.defaultModelName("mistralai/mistral-medium-2505")
.build();

MergeSchemaParameters parameters = MergeSchemaParameters.builder()
.semanticConfig(semanticConfig)
.timeout(Duration.ofMinutes(5))
.build();

var result = service.mergeSchemaAndFetch(schemas, parameters);

Managing Requests

Delete a merge job if needed:

MergeSchemaResponse response = service.startMergeSchema(schemas);

boolean deleted = service.deleteRequest(
response.metadata().id(),
MergeSchemaDeleteParameters.builder()
.hardDelete(true)
.build()
);

System.out.println("Deleted: " + deleted);
// → Deleted: true

Deleting a non-existent ID returns false.


Schema Merge Parameters

MergeSchemaParameters controls how schema merging is performed per request.

Builder Reference

ParameterTypeDescription
semanticConfigMergeSchemaSemanticConfigSemantic model configuration for merging
timeoutDurationOverride the service-level timeout for this request (synchronous only)
projectIdStringOverride the default Project ID
spaceIdStringOverride the default Space ID
transactionIdStringRequest tracking ID

Using a Custom Foundation Model

Override the default model with defaultModelName:

MergeSchemaSemanticConfig semanticConfig = MergeSchemaSemanticConfig.builder()
.defaultModelName("ibm/granite-4-h-small")
.build();

MergeSchemaParameters parameters = MergeSchemaParameters.builder()
.semanticConfig(semanticConfig)
.build();

var result = service.mergeSchemaAndFetch(schemas, parameters);

MergeSchemaResponse

Returned by startMergeSchema and fetchRequest.

FieldTypeDescription
metadata().id()StringUnique identifier for the schema merge request
metadata().createdAt()StringTimestamp when the request was created
metadata().modifiedAt()StringTimestamp of the last update
metadata().projectId()StringProject ID associated with the request
entity().results()MergeSchemaResultThe current schema merge result
entity().parameters()ParametersParameters used for this schema merge

MergeSchemaResult

FieldTypeDescription
status()StringCurrent status: submitted, running, completed, or failed
runningAt()StringTimestamp when processing started
completedAt()StringTimestamp when processing completed or failed
schema()SchemaThe merged document schema with unified field definitions
groundingHints()GroundingHintsField localization data (if available from original schemas)
error()ErrorError details if status is failed

Schema

FieldTypeDescription
documentType()StringThe merged document type (e.g., "Identification Document")
documentDescription()StringComprehensive description covering all input schemas
fields()Map<String, KvpField>Map of field names to merged field definitions
additionalPromptInstructions()StringAdditional instructions (if provided in input schemas)

KvpField

Each field in the merged schema contains:

FieldTypeDescription
description()StringUnified description of the field across all schemas
example()StringExample value for the field