Skip to content

fix: replace OCR truncation with quality checks to prevent data loss - #40

Merged
SamD merged 1 commit into
mainfrom
fix/ocr-oversized-output-quality-check
Jun 8, 2026
Merged

fix: replace OCR truncation with quality checks to prevent data loss#40
SamD merged 1 commit into
mainfrom
fix/ocr-oversized-output-quality-check

Conversation

@SamD

@SamD SamD commented Jun 8, 2026

Copy link
Copy Markdown
Owner

Problem

The previous OCR fallback code had a bug where one page returned over 20 million characters, which is impossible for a single PDF page. The root cause was uncommitted changes on top of revert ed51e00 that introduced 4 bugs:

  1. pdf_handler.py:54 — Garbled-text check (chars/word > 1000) was useless. A 20M char page with normal-looking words (e.g., 3M words) has a ratio of ~6.7, passing right through.
  2. ocr_utils.py:278TEXT_KEYS reordered to prefer "text" over "md", picking the wrong/larger field from docling-serve responses.
  3. ocr_utils.py:360-367 — Local Docling run_ocr() had no size cap at all.
  4. ocr_utils.py:315-317 — Oversized text was discarded entirely (lost whole page) instead of being handled gracefully.

Solution

Replace hard truncation with quality checks to ensure zero data loss:

  • Remove 50K char truncation in pdf_handler.py and ocr_utils.py (both remote and local paths)
  • Add is_bad_ocr() quality check on OCR output in pdf_handler.py to catch genuinely garbled text (gibberish, repetition, abnormal word lengths)
  • Restore TEXT_KEYS ordering in find_text() to prefer "md" over "text" (shorter, cleaner fields first)
  • Log warnings for oversized output (>50K chars) without data loss
  • Fix test mock in test_force_flags.py for new quality check behavior

Additional Fixes

  • Fix lint errors: unused imports in test_chat_session_service.py and test_ingestion_recovery.py
  • Remove unused clearChat() function in index.astro

Testing

  • All 142 tests pass
  • ruff check passes
  • Frontend builds cleanly

Impact

  • No data loss: Real OCR content is preserved regardless of size
  • Garbled output caught: 20M char pathological pages are rejected by quality checks, not truncation
  • Better field selection: find_text() now prefers markdown fields over raw text dumps

- Remove 50K char truncation in pdf_handler.py and ocr_utils.py
- Add is_bad_ocr() quality check on OCR output to catch garbled text
- Restore TEXT_KEYS ordering in find_text() to prefer 'md' over 'text'
- Log warnings for oversized output (>50K chars) without data loss
- Fix test_force_flags.py mock for new quality check behavior
- Fix lint errors: unused imports, unsorted imports, unused function
@SamD
SamD force-pushed the fix/ocr-oversized-output-quality-check branch from 57f4c46 to 8503283 Compare June 8, 2026 17:17
mock_preprocess.return_value = MagicMock()
mock_convert.return_value = [MagicMock()]
mock_send_ocr.return_value = ("ocr text", None, None, "docling", None, None)
mock_is_bad_ocr.return_value = True

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/oc verify

@opencode-agent

opencode-agent Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

APIError: Insufficient balance. Manage your billing here: https://opencode.ai/workspace/wrk_01KM1TM9SZGWE0MPG87G3MS3JK/billing

opencode session  |  github run

@SamD
SamD merged commit 686b246 into main Jun 8, 2026
1 check failed
@SamD
SamD deleted the fix/ocr-oversized-output-quality-check branch June 8, 2026 17:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant