Repository navigation
fix: OCR.detect() returns truthy None-tuple causing NoneType subscript crash - #13951
Conversation
📝 WalkthroughWalkthroughThe Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
deepdoc/vision/ocr.py (1)
673-682: Fix correctly addresses the truthy-tuple bug.Returning
Noneon failure paths (lines 674, 679) ensures the caller'sif not bxs:guard properly catches failures. The zip iterator on success (lines 681-682) maintains compatibility with the caller's iteration pattern[(line[0], line[1][0]) for line in bxs].Consider adding debug logging on failure paths for observability, as failures here are silent. As per coding guidelines, "Add logging for new flows".
💡 Optional: Add logging for failure paths
if img is None: + logging.debug("OCR.detect called with None image") return None dt_boxes, _ = self.text_detector[device_id](img) if dt_boxes is None: + logging.debug("OCR.detect: text detector returned None") return None,
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@deepdoc/vision/ocr.py` around lines 673 - 682, The failure paths currently return None silently when img is None or when dt_boxes is None; add logging calls before each return to record context (e.g., self.logger.debug or self.logger.error) including why it failed (e.g., "ocr: input image is None" and "ocr: text_detector returned no boxes for device_id=%s") and keep the existing return None and the successful zip return unchanged; reference the existing symbols self.text_detector, device_id, img, dt_boxes, and self.sorted_boxes when adding the log messages.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@deepdoc/vision/ocr.py`:
- Around line 673-682: The failure paths currently return None silently when img
is None or when dt_boxes is None; add logging calls before each return to record
context (e.g., self.logger.debug or self.logger.error) including why it failed
(e.g., "ocr: input image is None" and "ocr: text_detector returned no boxes for
device_id=%s") and keep the existing return None and the successful zip return
unchanged; reference the existing symbols self.text_detector, device_id, img,
dt_boxes, and self.sorted_boxes when adding the log messages.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 2ed2c6f6-1f16-4c02-b882-0677cc565c83
📒 Files selected for processing (1)
deepdoc/vision/ocr.py
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #13951 +/- ##
==========================================
- Coverage 98.11% 96.52% -1.60%
==========================================
Files 10 10
Lines 690 690
Branches 108 108
==========================================
- Hits 677 666 -11
- Misses 4 8 +4
- Partials 9 16 +7 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
xugangqiang
left a comment
There was a problem hiding this comment.
Appreciate for raising this PR.
On first sight, your fix should be working.
However, I am thinking on why "time_dict" is there - this value may be helpful in future.
How about this approach:
In detect method of ocr.py
A. Change the total count of return values from 3 to 2
B. If img or dt_boxes is None, return "None, time_dict"
C. Else "end=...., ret_value=zip(xxx), time_dict['all'] = end -start, return ret_value, time_dict"
In the caller (__ocr in pdf_parser.py),
bxs, time_dict = self.ocr.detect(np.array(img), device_id)
...other code follows....
Appreicate if you can add unit test for this bug under test/unit_test/deepdoc/parser
|
@xugangqiang Thanks for the review — your approach makes sense, and keeping
Will push the changes in a follow-up commit. |
…t crash (infiniflow#13951) Fixes infiniflow#13851 ## Problem `OCR.detect()` in `deepdoc/vision/ocr.py` returns `None, None, time_dict` (a truthy 3-tuple) when the text detector fails or receives a `None` image. However, the caller in `pdf_parser.py:__ocr()` checks: ```python bxs = self.ocr.detect(np.array(img), device_id) if not bxs: # False! (None, None, time_dict) is a non-empty tuple → truthy self.boxes.append([]) return bxs = [(line[0], line[1][0]) for line in bxs] # iterates (None, None, time_dict) # line = None → None[0] → TypeError: 'NoneType' object is not subscriptable ``` This causes the `NoneType object is not subscriptable` error that appears after "OCR started" in the chunking pipeline when using PDF + General parser. ## Solution Simplified `OCR.detect()` to return `None` (falsy) instead of `None, None, time_dict` on failure. The `time_dict` was unused by the only caller of this method. The early-return guard `if not bxs:` in `pdf_parser.py` then correctly catches it. ## Testing - The method's only caller (`pdf_parser.py:__ocr`) already has a `if not bxs:` guard that handles the `None` return correctly. - No other callers of `OCR.detect()` exist in the codebase. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Refactor** * Modified OCR detection function return behavior to streamline output. The function now returns detection results only, without timing metadata. Error cases now return `None` instead of empty tuple values. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
…t crash (infiniflow#13951) Fixes infiniflow#13851 ## Problem `OCR.detect()` in `deepdoc/vision/ocr.py` returns `None, None, time_dict` (a truthy 3-tuple) when the text detector fails or receives a `None` image. However, the caller in `pdf_parser.py:__ocr()` checks: ```python bxs = self.ocr.detect(np.array(img), device_id) if not bxs: # False! (None, None, time_dict) is a non-empty tuple → truthy self.boxes.append([]) return bxs = [(line[0], line[1][0]) for line in bxs] # iterates (None, None, time_dict) # line = None → None[0] → TypeError: 'NoneType' object is not subscriptable ``` This causes the `NoneType object is not subscriptable` error that appears after "OCR started" in the chunking pipeline when using PDF + General parser. ## Solution Simplified `OCR.detect()` to return `None` (falsy) instead of `None, None, time_dict` on failure. The `time_dict` was unused by the only caller of this method. The early-return guard `if not bxs:` in `pdf_parser.py` then correctly catches it. ## Testing - The method's only caller (`pdf_parser.py:__ocr`) already has a `if not bxs:` guard that handles the `None` return correctly. - No other callers of `OCR.detect()` exist in the codebase. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Refactor** * Modified OCR detection function return behavior to streamline output. The function now returns detection results only, without timing metadata. Error cases now return `None` instead of empty tuple values. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
Fixes #13851
Problem
OCR.detect()indeepdoc/vision/ocr.pyreturnsNone, None, time_dict(a truthy 3-tuple) when the text detector fails or receives aNoneimage. However, the caller inpdf_parser.py:__ocr()checks:This causes the
NoneType object is not subscriptableerror that appears after "OCR started" in the chunking pipeline when using PDF + General parser.Solution
Simplified
OCR.detect()to returnNone(falsy) instead ofNone, None, time_dicton failure. Thetime_dictwas unused by the only caller of this method. The early-return guardif not bxs:inpdf_parser.pythen correctly catches it.Testing
pdf_parser.py:__ocr) already has aif not bxs:guard that handles theNonereturn correctly.OCR.detect()exist in the codebase.Summary by CodeRabbit
Noneinstead of empty tuple values.