docling-project / docling-project/docling
KeyError: -1
- Dominant language
- Python
- Stars
- 66.4k
- Forks
- 4.8k
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 84
Description
### Bug
There is closed issue raise by me about the same error. It was fixed, but now I am getting the same error with the following files (attached)
...
### Steps to reproduce
```python
converter =DocumentConverter(
format_options={
InputFormat.HTML: HTMLFormatOption(
pipeline_options=PipelineOptions(
accelerator=AcceleratorOptions(
device=AcceleratorDevice.AUTO,
num_threads=4,
)
),
backend=HTMLDocumentBackend,
)
}
)
converter.convert(f)
```
### Docling version
...
### Python version
docling==2.34.0
docling-core==2.31.2
docling-ibm-models==3.4.3
docling-parse==4.0.1
python==3.12.9
## Error
```python
Error processing child from tag div:
Traceback (most recent call last):
File "/home/mahmoud/miniconda3/envs/rag/lib/python3.12/site-packages/docling/backend/html_backend.py", line 138, in walk
self.analyze_tag(cast(Tag, element), doc)
File "/home/mahmoud/miniconda3/envs/rag/lib/python3.12/site-packages/docling/backend/html_backend.py", line 187, in analyze_tag
self.walk(tag, doc)
File "/home/mahmoud/miniconda3/envs/rag/lib/python3.12/site-packages/docling/backend/html_backend.py", line 158, in walk
parent=self.parents[self.level],
~~~~~~~~~~~~^^^^^^^^^^^^
KeyError: -1
Error processing child from tag div:
Traceback (most recent call last):
File "/home/mahmoud/miniconda3/envs/rag/lib/python3.12/site-packages/docling/backend/html_backend.py", line 138, in walk
self.analyze_tag(cast(Tag, element), doc)
File "/home/mahmoud/miniconda3/envs/rag/lib/python3.12/site-packages/docling/backend/html_backend.py", line 187, in analyze_tag
self.walk(tag, doc)
File "/home/mahmoud/miniconda3/envs/rag/lib/python3.12/site-packages/docling/backend/html_backend.py", line 143, in walk
raise exc_child
File "/home/mahmoud/miniconda3/envs/rag/lib/python3.12/site-packages/docling/backend/html_backend.py", line 138, in walk
self.analyze_tag(cast(Tag, element), doc)
File "/home/mahmoud/miniconda3/envs/rag/lib/python3.12/site-packages/docling/backend/html_backend.py", line 187, in analyze_tag
self.walk(tag, doc)
File "/home/mahmoud/miniconda3/envs/rag/lib/python3.12/site-packages/docling/backend/html_backend.py", line 158, in walk
parent=self.parents[self.level],
...
parent=self.parents[self.level],
~~~~~~~~~~~~^^^^^^^^^^^^
KeyError: -1
KeyError Traceback (most recent call last)
Cell In[46], line 2
1 f = files[0]
----> 2 converter.convert(f)
File ~/miniconda3/envs/rag/lib/python3.12/site-packages/pydantic/_internal/_validate_call.py:38, in update_wrapper_attributes..wrapper_function(*args, **kwargs)
36 @functools.wraps(wrapped)
37 def wrapper_function(*args, **kwargs):
---> 38 return wrapper(*args, **kwargs)
File ~/miniconda3/envs/rag/lib/python3.12/site-packages/pydantic/_internal/_validate_call.py:111, in ValidateCallWrapper.__call__(self, *args, **kwargs)
110 def __call__(self, *args: Any, **kwargs: Any) -> Any:
--> 111 res = self.__pydantic_validator__.validate_python(pydantic_core.ArgsKwargs(args, kwargs))
112 if self.__return_pydantic_validator__:
113 return self.__return_pydantic_validator__(res)
File ~/miniconda3/envs/rag/lib/python3.12/site-packages/docling/document_converter.py:222, in DocumentConverter.convert(self, source, headers, raises_on_error, max_num_pages, max_file_size, page_range)
204 @validate_call(config=ConfigDict(strict=True))
205 def convert(
206 self,
(...) 212 page_range: PageRange = DEFAULT_PAGE_RANGE,
213 ) -> ConversionResult:
214 all_res = self.convert_all(
215 source=[source],
216 raises_on_error=raises_on_error,
(...) 220 page_range=page_range,
221 )
--> 222 return next(all_res)
File ~/miniconda3/envs/rag/lib/python3.12/site-packages/docling/document_converter.py:245, in DocumentConverter.convert_all(self, source, headers, raises_on_error, max_num_pages, max_file_size, page_range)
242 conv_res_iter = self._convert(conv_input, raises_on_error=raises_on_error)
244 had_result = False
--> 245 for conv_res in conv_res_iter:
246 had_result = True
247 if raises_on_error and conv_res.status not in {
248 ConversionStatus.SUCCESS,
249 ConversionStatus.PARTIAL_SUCCESS,
250 }:
File ~/miniconda3/envs/rag/lib/python3.12/site-packages/docling/document_converter.py:280, in DocumentConverter._convert(self, conv_input, raises_on_error)
271 _log.info("Going to convert document batch...")
273 # parallel processing only within input_batch
274 # with ThreadPoolExecutor(
275 # max_workers=settings.perf.doc_batch_concurrency
276 # ) as pool:
277 # yield from pool.map(self.process_document, input_batch)
278 # Note: PDF backends are not thread-safe, thread pool usage was disabled.
--> 280 for item in map(
281 partial(self._process_document, raises_on_error=raises_on_error),
282 input_batch,
283 ):
284 elapsed = time.monotonic() - start_time
285 start_time = time.monotonic()
File ~/miniconda3/envs/rag/lib/python3.12/site-packages/docling/document_converter.py:326, in DocumentConverter._process_document(self, in_doc, raises_on_error)
322 valid = (
323 self.allowed_formats is not None and in_doc.format in self.allowed_formats
324 )
325 if valid:
--> 326 conv_res = self._execute_pipeline(in_doc, raises_on_error=raises_on_error)
327 else:
328 error_message = f"File format not allowed: {in_doc.file}"
File ~/miniconda3/envs/rag/lib/python3.12/site-packages/docling/document_converter.py:349, in DocumentConverter._execute_pipeline(self, in_doc, raises_on_error)
347 pipeline = self._get_pipeline(in_doc.format)
348 if pipeline is not None:
--> 349 conv_res = pipeline.execute(in_doc, raises_on_error=raises_on_error)
350 else:
351 if raises_on_error:
File ~/miniconda3/envs/rag/lib/python3.12/site-packages/docling/pipeline/base_pipeline.py:54, in BasePipeline.execute(self, in_doc, raises_on_error)
52 conv_res.status = ConversionStatus.FAILURE
53 if raises_on_error:
---> 54 raise e
55 finally:
56 self._unload(conv_res)
File ~/miniconda3/envs/rag/lib/python3.12/site-packages/docling/pipeline/base_pipeline.py:46, in BasePipeline.execute(self, in_doc, raises_on_error)
40 try:
41 with TimeRecorder(
42 conv_res, "pipeline_total", scope=ProfilingScope.DOCUMENT
43 ):
44 # These steps are building and assembling the structure of the
45 # output DoclingDocument.
---> 46 conv_res = self._build_document(conv_res)
47 conv_res = self._assemble_document(conv_res)
48 # From this stage, all operations should rely only on conv_res.output
File ~/miniconda3/envs/rag/lib/python3.12/site-packages/docling/pipeline/simple_pipeline.py:40, in SimplePipeline._build_document(self, conv_res)
33 # conv_res.status = ConversionStatus.FAILURE
34 # return conv_res
35
36 # Instead of running a page-level pipeline to build up the document structure,
37 # the backend is expected to be of type DeclarativeDocumentBackend, which can output
38 # a DoclingDocument straight.
39 with TimeRecorder(conv_res, "doc_build", scope=ProfilingScope.DOCUMENT):
---> 40 conv_res.document = conv_res.input._backend.convert()
41 return conv_res
File ~/miniconda3/envs/rag/lib/python3.12/site-packages/docling/backend/html_backend.py:124, in HTMLDocumentBackend.convert(self)
120 headers = content.find(["h1", "h2", "h3", "h4", "h5", "h6"])
121 self.content_layer = (
122 ContentLayer.BODY if headers is None else ContentLayer.FURNITURE
123 )
--> 124 self.walk(content, doc)
125 else:
126 raise RuntimeError(
127 f"Cannot convert doc with {self.document_hash} because the backend "
128 "failed to init."
129 )
File ~/miniconda3/envs/rag/lib/python3.12/site-packages/docling/backend/html_backend.py:143, in HTMLDocumentBackend.walk(self, tag, doc)
139 except Exception as exc_child:
140 _log.error(
141 f"Error processing child from tag {tag.name}:\n{traceback.format_exc()}"
142 )
--> 143 raise exc_child
144 elif isinstance(element, NavigableString) and not isinstance(
145 element, PreformattedString
146 ):
147 # Floating text outside paragraphs or analyzed tags
148 text += element
File ~/miniconda3/envs/rag/lib/python3.12/site-packages/docling/backend/html_backend.py:138, in HTMLDocumentBackend.walk(self, tag, doc)
136 if isinstance(element, Tag):
137 try:
--> 138 self.analyze_tag(cast(Tag, element), doc)
139 except Exception as exc_child:
140 _log.error(
141 f"Error processing child from tag {tag.name}:\n{traceback.format_exc()}"
142 )
File ~/miniconda3/envs/rag/lib/python3.12/site-packages/docling/backend/html_backend.py:187, in HTMLDocumentBackend.analyze_tag(self, tag, doc)
185 self.handle_details(tag, doc)
186 else:
--> 187 self.walk(tag, doc)
File ~/miniconda3/envs/rag/lib/python3.12/site-packages/docling/backend/html_backend.py:143, in HTMLDocumentBackend.walk(self, tag, doc)
139 except Exception as exc_child:
140 _log.error(
141 f"Error processing child from tag {tag.name}:\n{traceback.format_exc()}"
142 )
--> 143 raise exc_child
144 elif isinstance(element, NavigableString) and not isinstance(
145 element, PreformattedString
146 ):
147 # Floating text outside paragraphs or analyzed tags
148 text += element
File ~/miniconda3/envs/rag/lib/python3.12/site-packages/docling/backend/html_backend.py:138, in HTMLDocumentBackend.walk(self, tag, doc)
136 if isinstance(element, Tag):
137 try:
--> 138 self.analyze_tag(cast(Tag, element), doc)
139 except Exception as exc_child:
140 _log.error(
141 f"Error processing child from tag {tag.name}:\n{traceback.format_exc()}"
142 )
File ~/miniconda3/envs/rag/lib/python3.12/site-packages/docling/backend/html_backend.py:187, in HTMLDocumentBackend.analyze_tag(self, tag, doc)
185 self.handle_details(tag, doc)
186 else:
--> 187 self.walk(tag, doc)
File ~/miniconda3/envs/rag/lib/python3.12/site-packages/docling/backend/html_backend.py:143, in HTMLDocumentBackend.walk(self, tag, doc)
139 except Exception as exc_child:
140 _log.error(
141 f"Error processing child from tag {tag.name}:\n{traceback.format_exc()}"
142 )
--> 143 raise exc_child
144 elif isinstance(element, NavigableString) and not isinstance(
145 element, PreformattedString
146 ):
147 # Floating text outside paragraphs or analyzed tags
148 text += element
File ~/miniconda3/envs/rag/lib/python3.12/site-packages/docling/backend/html_backend.py:138, in HTMLDocumentBackend.walk(self, tag, doc)
136 if isinstance(element, Tag):
137 try:
--> 138 self.analyze_tag(cast(Tag, element), doc)
139 except Exception as exc_child:
140 _log.error(
141 f"Error processing child from tag {tag.name}:\n{traceback.format_exc()}"
142 )
File ~/miniconda3/envs/rag/lib/python3.12/site-packages/docling/backend/html_backend.py:187, in HTMLDocumentBackend.analyze_tag(self, tag, doc)
185 self.handle_details(tag, doc)
186 else:
--> 187 self.walk(tag, doc)
File ~/miniconda3/envs/rag/lib/python3.12/site-packages/docling/backend/html_backend.py:158, in HTMLDocumentBackend.walk(self, tag, doc)
155 text = text.strip()
156 if text and tag.name in ["div"]:
157 doc.add_text(
--> 158 parent=self.parents[self.level],
159 label=DocItemLabel.TEXT,
160 text=text,
161 content_layer=self.content_layer,
162 )
163 text = ""
165 return
KeyError: -1
```
[test.txt](https://github.com/user-attachments/files/20617146/test.txt)
[test2.txt](https://github.com/user-attachments/files/20617439/test2.txt)
Contributor guide
Assessment
This issue has not been assessed yet.