Repository navigation
Description
Bug
When an EBCDIC layout declares more than one record schema and the schemas don't set name, every table in the converted document gets every schema's rows. The conversion reports ConversionStatus.SUCCESS, so nothing signals that the data is wrong.
EbcdicRecordLayout.name defaults to "record", and EbcdicLayout only requires that the selectors are unique. A copybook with two schemas, unique selectors and no names is therefore a valid layout, and it silently produces mixed tables.
Steps to reproduce
Self-contained; writes an 84-byte cp037 file holding two customer records tagged C and two transaction records tagged T, which is the ordinary reason a copybook declares more than one schema.
from io import BytesIO
from docling.datamodel.backend_options import (
EbcdicBackendOptions, EbcdicField, EbcdicFieldType,
EbcdicLayout, EbcdicRecordLayout,
)
from docling.datamodel.base_models import DocumentStream, InputFormat
from docling.document_converter import DocumentConverter, EbcdicFormatOption
E = "cp037"
text = lambda v, n: v.ljust(n).encode(E)
packed = lambda v, n: bytes.fromhex(f"{abs(v):0{n * 2 - 1}d}" + ("d" if v < 0 else "c"))
data = (
text("C", 1) + text("ACME SUPPLIES", 16) + text("0041", 4)
+ text("C", 1) + text("BOREALIS TRADING", 16) + text("0042", 4)
+ text("T", 1) + text("INVOICE 88213", 15) + packed(125050, 5)
+ text("T", 1) + text("REFUND 88240", 15) + packed(-4999, 5)
)
# Two schemas, unique selectors as EbcdicLayout requires, `name` left at default.
layout = EbcdicLayout(
description="nightly bank extract",
record_type_field=EbcdicField(name="rectype", size=1),
records=[
EbcdicRecordLayout(selector="C", fields=[
EbcdicField(name="customer_name", size=16),
EbcdicField(name="account_no", size=4),
]),
EbcdicRecordLayout(selector="T", fields=[
EbcdicField(name="memo", size=15),
EbcdicField(name="amount", size=5,
type=EbcdicFieldType.PACKED_DECIMAL, scale=2),
]),
],
)
converter = DocumentConverter(format_options={
InputFormat.EBCDIC: EbcdicFormatOption(
backend_options=EbcdicBackendOptions(layout=layout))
})
result = converter.convert(DocumentStream(name="nightly.ebc", stream=BytesIO(data)))
print(result.status)
print(result.document.export_to_markdown())Observed — ConversionStatus.SUCCESS, and both tables hold all four records:
## record
| customer_name | account_no |
|------------------|--------------|
| ACME SUPPLIES | 0041 |
| BOREALIS TRADING | 0042 |
| INVOICE 88213 | 1250.50 |
| REFUND 88240 | -49.99 |
## record
| memo | amount |
|------------------|----------|
| ACME SUPPLIES | 0041 |
| BOREALIS TRADING | 0042 |
| INVOICE 88213 | 1250.50 |
| REFUND 88240 | -49.99 |A transaction amount of 1250.50 is presented as a customer's account_no, and the account number 0041 is presented as a monetary amount.
Expected — the two tables each hold their own two records. Adding name="customer" and name="transaction" to the two schemas, changing nothing else, produces exactly that:
## customer
| customer_name | account_no |
|------------------|--------------|
| ACME SUPPLIES | 0041 |
| BOREALIS TRADING | 0042 |
## transaction
| memo | amount |
|---------------|----------|
| INVOICE 88213 | 1250.50 |
| REFUND 88240 | -49.99 |Where it comes from
_RecordParser.parse buckets the decoded rows by schema name:
rows = {record.name: [] for record in self._layout.records}Records are selected by selector but stored by name, so two schemas sharing a name share a bucket. convert() then iterates the schemas and hands each one rows[record.name], which is the combined list, while _build_table writes that schema's own header over it.
EbcdicLayout._validate_records already checks that selectors are unique and that each record has one; it just doesn't check names, even though name is what the parser keys on and what add_heading prints.
The existing tests don't catch this because every multi-schema test sets explicit distinct names.
Fix
Two options that both look reasonable:
- Extend
_validate_recordsto require unique names when a layout has several schemas, mirroring the selector check right above it. This also fixes the two identical## recordheadings. - Key
rowsby the schema's index instead of its name, so the buckets can't collide regardless of naming.
Happy to open a PR for whichever you prefer.
Docling version
Docling version: 2.123.1
Docling Core version: 2.92.0
Also reproduced on main @ 97bdf30.
Python version
Python 3.12.14
Activity