Skip to content

fix(ocr): include underlying error when loading OCR agent fails - #4491

Merged
cragwolfe merged 3 commits into
Unstructured-IO:mainfrom
Anurag-M1:Anurag-M1/fix-ocr-agent-error-handling
Oct 16, 2026
Merged

cragwolfe merged 3 commits into
Unstructured-IO:mainfrom
Anurag-M1:Anurag-M1/fix-ocr-agent-error-handling

Conversation

@Anurag-M1

@Anurag-M1 Anurag-M1 commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #3276.

When OCRAgent.get_instance() fails to import or load a requested OCR agent module (e.g. missing dependencies or an AttributeError), it previously raised a generic RuntimeError without the original exception details or exception chaining (from e):

raise RuntimeError(
    "Could not get the OCRAgent instance. Please check the OCR package and the "
    "OCR_AGENT environment variable."
)

As reported in #3276 and discussed with @scanny, this masks the underlying error (such as ImportError: No module named '...'), making troubleshooting difficult.

Changes

  1. In unstructured/partition/utils/ocr_models/ocr_interface.py, include {e} in the raised RuntimeError message and chain the original exception with raise ... from e.
  2. In test_unstructured/partition/utils/ocr_models/test_ocr_interface.py, update and_it_raises_when_the_requested_agent_cannot_be_loaded to verify that the underlying exception message is present in the raised RuntimeError and that __cause__ references the original exception.
  3. Update CHANGELOG.md under ## 0.27.8-dev0 / ### Fixes.

Testing

  • Ran targeted unit tests:
    pytest test_unstructured/partition/utils/ocr_models/test_ocr_interface.py -v
    (All 10 tests passed)
  • Ran regression test suites:
    pytest test_unstructured/cleaners/test_core.py test_unstructured/partition/test_text.py
    (All 197 tests passed)
  • Linting and formatting checks:
    ruff check unstructured/partition/utils/ocr_models/ocr_interface.py test_unstructured/partition/utils/ocr_models/test_ocr_interface.py
    ruff format --check unstructured/partition/utils/ocr_models/ocr_interface.py test_unstructured/partition/utils/ocr_models/test_ocr_interface.py

Review in cubic

@cragwolfe
cragwolfe merged commit 1b02b4d into Unstructured-IO:main Oct 16, 2026
52 checks passed
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.

Minor ocr_interface.py Error Handling Improvement

2 participants