docling icon indicating copy to clipboard operation
docling copied to clipboard

Unexpected References Appearing in Docling OCR JSON Results

Open manikrishna-m opened this issue 1 year ago • 10 comments

Hi Everyone,

I'm using Docling's basic OCR to convert a PDF into text, and I save the output as a JSON file. In one of the PDFs I'm processing, the JSON result includes a group (Group 4), which lists its children as references to texts 61 through 77. Here's a simplified snippet of what the raw JSON looks like:

{
  "children": [
    {"$ref": "#/texts/61"},
    {"$ref": "#/texts/62"},
    {"$ref": "#/texts/63"},
    {"$ref": "#/texts/64"},
    {"$ref": "#/texts/65"},
    {"$ref": "#/texts/66"},
    {"$ref": "#/texts/67"},
    {"$ref": "#/texts/68"},
    {"$ref": "#/texts/69"},
    {"$ref": "#/texts/70"},
    {"$ref": "#/texts/71"},
    {"$ref": "#/texts/72"},
    {"$ref": "#/texts/73"},
    {"$ref": "#/texts/74"},
    {"$ref": "#/texts/75"},
    {"$ref": "#/texts/76"},
    {"$ref": "#/texts/77"}
  ]
}

However, after loading this JSON using:

self.docling_doc = DoclingDocument.model_validate(doc_dict)

The children list in Group 4 unexpectedly changes. Here's what I get:

[
  RefItem(cref='#/texts/61'),
  RefItem(cref='#/texts/62'),
  RefItem(cref='#/texts/63'),
  RefItem(cref='#/texts/64'),
  RefItem(cref='#/texts/65'),
  RefItem(cref='#/texts/66'),
  RefItem(cref='#/texts/67'),
  RefItem(cref='#/texts/68'),
  RefItem(cref='#/groups/45'),  # Unexpected
  RefItem(cref='#/texts/69'),
  RefItem(cref='#/groups/44'),  # Unexpected
  RefItem(cref='#/texts/70'),
  RefItem(cref='#/texts/71'),
  RefItem(cref='#/texts/72'),
  RefItem(cref='#/texts/73'),
  RefItem(cref='#/texts/74'),
  RefItem(cref='#/texts/75')
]

As you can see, there are now references to #/groups/44 and #/groups/45, which were not present in the original JSON. Also, texts/76 and texts/77 are missing from the parsed result.

Can anyone help me understand why this is happening? Is it a parsing issue with model_validate, or could the input JSON be getting altered during validation?

Happy to provide more details if needed.

Thanks in advance!

manikrishna-m avatar Jul 23 '25 13:07 manikrishna-m

Hi @manikrishna-m! I'm Dosu and I’m helping the docling team.

This behavior is due to Docling's automatic normalization during validation. When you load your JSON with DoclingDocument.model_validate, Docling checks for structural rules—specifically, all ListItem entries must be inside a ListGroup. If your input JSON has ListItems directly under a group (not inside a ListGroup), Docling will auto-insert new groups (like #/groups/44 and #/groups/45) and rewrite the children list to enforce this rule. As a result, some references (like texts/76 and texts/77) may be dropped, reparented, or reordered if they don't fit the expected structure. This is by design to maintain document integrity, but it can be surprising if your input is borderline or not fully normalized before validation. You can avoid this by ensuring your input JSON nests all ListItems inside a ListGroup before passing it to model_validate—that way, Docling won't need to auto-correct the structure for you.
Reference: docling-core/document.py

To reply, just mention @dosu.


Docs are dead. Just use Dosu.

Leave Feedback Ask Dosu about docling Share Dosu with your team

dosubot[bot] avatar Jul 23 '25 13:07 dosubot[bot]

@dosu , Thank you so much for the reference, Could you please provide me example what you are trying to explain

manikrishna-m avatar Jul 23 '25 13:07 manikrishna-m

Sure! Here’s a concrete example to show how Docling expects list items to be nested, and what happens if they aren’t.

Problematic input (what causes auto-insertion of groups):

{
  "groups": [
    {
      "id": 4,
      "label": "section",
      "children": [
        {"$ref": "#/texts/61"},
        {"$ref": "#/texts/62"},
        {"$ref": "#/texts/63"}
      ]
    }
  ],
  "texts": [
    {"id": 61, "label": "list_item", "text": "First"},
    {"id": 62, "label": "list_item", "text": "Second"},
    {"id": 63, "label": "list_item", "text": "Third"}
  ]
}

Here, the children of group 4 directly reference list_items. When you load this with model_validate, Docling will automatically insert a ListGroup to contain those list items, and rewrite the children list to reference the new group(s) instead of the items directly. This can cause unexpected group references and missing items if the structure is ambiguous.

Corrected input (what Docling expects):

{
  "groups": [
    {
      "id": 4,
      "label": "section",
      "children": [
        {"$ref": "#/groups/10"}
      ]
    },
    {
      "id": 10,
      "label": "list",
      "children": [
        {"$ref": "#/texts/61"},
        {"$ref": "#/texts/62"},
        {"$ref": "#/texts/63"}
      ]
    }
  ],
  "texts": [
    {"id": 61, "label": "list_item", "text": "First"},
    {"id": 62, "label": "list_item", "text": "Second"},
    {"id": 63, "label": "list_item", "text": "Third"}
  ]
}

Now, the section group’s children points to a ListGroup (with label "list"), and that group contains the list items. This matches Docling’s schema and avoids any auto-insertion or rewriting during validation.

Summary:
Always wrap your list items in a group with "label": "list" and reference that group from your parent group. This way, Docling won’t need to “fix” your structure and you’ll keep full control over your document tree.
Reference: docling-core/document.py

To reply, just mention @dosu.


Share context across your team and agents. Try Dosu.

Leave Feedback Learn about docling with Dosu Add Dosu to your team

dosubot[bot] avatar Jul 23 '25 14:07 dosubot[bot]

@dosu , All the labels in my case are form related and the above example doesn't help me.. Please provide detailed steps and example

manikrishna-m avatar Jul 23 '25 14:07 manikrishna-m

For form-related content, Docling expects you to use a FormItem object, not groups of text or list items. There’s no normalization or auto-grouping for form fields—everything goes inside a FormItem’s graph as cells. Here’s a minimal, valid example for a form with two fields (“Name” and “Age”):

{
  "form_items": [
    {
      "label": "form",
      "self_ref": "#/form_items/0",
      "graph": {
        "cells": [
          {
            "label": "key",
            "cell_id": 0,
            "text": "Name"
          },
          {
            "label": "value",
            "cell_id": 1,
            "text": "Alice"
          },
          {
            "label": "key",
            "cell_id": 2,
            "text": "Age"
          },
          {
            "label": "value",
            "cell_id": 3,
            "text": "30"
          }
        ],
        "links": [
          {
            "label": "to_value",
            "source_cell_id": 0,
            "target_cell_id": 1
          },
          {
            "label": "to_key",
            "source_cell_id": 1,
            "target_cell_id": 0
          },
          {
            "label": "to_value",
            "source_cell_id": 2,
            "target_cell_id": 3
          },
          {
            "label": "to_key",
            "source_cell_id": 3,
            "target_cell_id": 2
          }
        ]
      }
    }
  ]
}

Each field is a pair of cells: one with "label": "key" (the field/question), one with "label": "value" (the answer). The links connect each key to its value. You can add as many fields as you want by adding more key/value cell pairs and links.

You don’t need to wrap these in groups or worry about Docling rewriting your structure—just put your form fields in the form_items array as shown. This is the canonical, validated approach for forms in Docling, and it won’t trigger any normalization or reference rewriting during validation.
Reference: docling-core/document.py
Reference: test_docling_doc.py

To reply, just mention @dosu.


Share context across your team and agents. Try Dosu.

Leave Feedback Learn about docling with Dosu Add Dosu to your team

dosubot[bot] avatar Jul 23 '25 14:07 dosubot[bot]

Just to clarify:

The group tag label is form_area

The text label is text

@dosu, the example you provided isn’t quite clear. Could you please elaborate or provide a more detailed version?

manikrishna-m avatar Jul 23 '25 18:07 manikrishna-m

You can safely use a group with "label": "form_area" and have its children reference text items with "label": "text"—this structure is valid and will not trigger Docling's normalization or rewriting. Here’s a detailed example:

{
  "groups": [
    {
      "self_ref": "#/groups/4",
      "label": "form_area",
      "children": [
        {"$ref": "#/texts/61"},
        {"$ref": "#/texts/62"},
        {"$ref": "#/texts/63"}
      ]
    }
  ],
  "texts": [
    {
      "self_ref": "#/texts/61",
      "label": "text",
      "orig": "Name",
      "text": "Name"
    },
    {
      "self_ref": "#/texts/62",
      "label": "text",
      "orig": "Alice",
      "text": "Alice"
    },
    {
      "self_ref": "#/texts/63",
      "label": "text",
      "orig": "Age",
      "text": "Age"
    }
  ]
}
  • The group’s children array contains references to text items.
  • Each text item uses "label": "text" and has its own self_ref.
  • No normalization or auto-grouping will occur, because only "list_item" texts require special nesting under a "list" group—your structure avoids this rule entirely.

If you want to add more fields, just add more text items and reference them in the children array. If you need to represent key-value pairs or more complex form logic, consider using the canonical form_items/graph structure, but for simple grouping of text fields, this approach is correct and stable.
See schema: GroupLabel and DocItemLabel
See schema: GroupItem and TextItem

To reply, just mention @dosu.


Share context across your team and agents. Try Dosu.

Leave Feedback Learn about docling with Dosu Add Dosu to your team

dosubot[bot] avatar Jul 23 '25 18:07 dosubot[bot]

@dosu, my question why I'm getting the unexpected groups

manikrishna-m avatar Jul 23 '25 18:07 manikrishna-m

The unexpected group references (like #/groups/44 and #/groups/45) appear when Docling's validation detects items labeled as "list_item" that aren't inside a "list" group. In that case, Docling auto-inserts new groups to enforce its schema rules—even if your group is labeled "form_area" and your texts are labeled "text", a stray "list_item" label or structural ambiguity in your input can trigger this normalization logic.

To fix this, double-check your input JSON for any items with the label "list_item" or for mixed/incorrect labels among your texts. Only "list_item" triggers this auto-grouping—"form_area" and "text" alone will not cause it, per the code and schema (see implementation). If you still see the issue, try reducing your input to a minimal example that triggers the problem, or share a sanitized snippet for more targeted help.

To reply, just mention @dosu.


Share context across your team and agents. Try Dosu.

Leave Feedback Learn about docling with Dosu Add Dosu to your team

dosubot[bot] avatar Jul 23 '25 18:07 dosubot[bot]

Classification labels added: bug, priority:medium, pdf.

These labels are advisory. Maintainers can correct them. The workflow preserves these corrections.

github-actions[bot] avatar Nov 16 '26 16:11 github-actions[bot]