[GH-ISSUE #294] Error when extracting images with PyMuPDFLoader and PyPDFLoader #2

Closed
opened 2026-02-17 17:18:57 -05:00 by yindo · 16 comments
Owner

Originally created by @BennisonDevadoss on GitHub (Sep 9, 2024).
Original GitHub issue: https://github.com/langchain-ai/docs/issues/294

Originally assigned to: @eugeneo17 on GitHub.

TRANSFERED BY @mdrxy
DOCS NOTE: SEE ATTACHED CLOSED PR THAT SHOULD BE APPLIED HERE

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

  1. Use the following code to load a PDF with image extraction enabled with PyMuPDFLoader:
########################################
# PyMuPDFLoader
########################################
from langchain_community.document_loaders import PyMuPDFLoader

loader = PyMuPDFLoader("google-2024-environmental-report.pdf", extract_images=True)
pages = loader.load()

for page in pages:
    print(page.page_content)
  1. Download the PDF located at: Google 2024 Environmental Report.
  2. Additionally, I also tried using PyPDFLoader with the same PDF, and I encountered the same issue.

Error Message and Stack Trace (if applicable)

Traceback (most recent call last):
  File "/mnt/c/Users/BennisonJ/Yavar/projects/zypher-2.0/backend/apps/rag/main.py", line 811, in store_doc
    data = loader.load()
           ^^^^^^^^^^^^^
  File "/home/bennison/miniconda3/envs/open-webui/lib/python3.11/site-packages/langchain_community/document_loaders/pdf.py", line 387, in load
    return list(self._lazy_load(**kwargs))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/bennison/miniconda3/envs/open-webui/lib/python3.11/site-packages/langchain_community/document_loaders/pdf.py", line 384, in _lazy_load
    yield from parser.lazy_parse(blob)
  File "/home/bennison/miniconda3/envs/open-webui/lib/python3.11/site-packages/langchain_community/document_loaders/parsers/pdf.py", line 244, in lazy_parse
    yield from [
               ^
  File "/home/bennison/miniconda3/envs/open-webui/lib/python3.11/site-packages/langchain_community/document_loaders/parsers/pdf.py", line 247, in <listcomp>
    + self._extract_images_from_page(doc, page),
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/bennison/miniconda3/envs/open-webui/lib/python3.11/site-packages/langchain_community/document_loaders/parsers/pdf.py", line 283, in _extract_images_from_page
    return extract_from_images_with_rapidocr(imgs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/bennison/miniconda3/envs/open-webui/lib/python3.11/site-packages/langchain_community/document_loaders/parsers/pdf.py", line 74, in extract_from_images_with_rapidocr
    result, _ = ocr(img)
                ^^^^^^^^
  File "/home/bennison/miniconda3/envs/open-webui/lib/python3.11/site-packages/rapidocr_onnxruntime/rapid_ocr_api.py", line 80, in __call__
    dt_boxes, det_elapse = self.text_detector(img)
                           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/bennison/miniconda3/envs/open-webui/lib/python3.11/site-packages/rapidocr_onnxruntime/ch_ppocr_v3_det/text_detect.py", line 66, in __call__
    data = transform(data, self.preprocess_op)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/bennison/miniconda3/envs/open-webui/lib/python3.11/site-packages/rapidocr_onnxruntime/ch_ppocr_v3_det/utils.py", line 220, in transform
    data = op(data)
           ^^^^^^^^
  File "/home/bennison/miniconda3/envs/open-webui/lib/python3.11/site-packages/rapidocr_onnxruntime/ch_ppocr_v3_det/utils.py", line 75, in __call__
    data['image'] = (img * self.scale - self.mean) / self.std
                     ~~~~~~~~~~~~~~~~~^~~~~~~~~~~
ValueError: operands could not be broadcast together with shapes (896,800) (1,1,3)

Description

I am encountering a ValueError when using both PyMuPDFLoader and PyPDFLoader to extract images from certain PDFs. The error message indicates that operands could not be broadcast together with shapes (896,800) (1,1,3). This occurs specifically when the extract_images parameter is set to True.

Expected Behavior
The code should successfully extract text and images from the PDF without errors.

Additional Information
This issue seems to occur with specific PDFs that may have unique formatting or image properties. I would appreciate any guidance on how to resolve this issue or if there are any workarounds available.

System Info

langchain==0.1.16
langchain-chroma==0.1.0
langchain-community==0.0.34
langchain-core==0.1.52
langchain-text-splitters==0.0.2

PyMuPDF Version: 1.24.10
PyPDF Version: 4.2.0
Operating System: Ubuntu 22 LTS
Originally created by @BennisonDevadoss on GitHub (Sep 9, 2024). Original GitHub issue: https://github.com/langchain-ai/docs/issues/294 Originally assigned to: @eugeneo17 on GitHub. TRANSFERED BY @mdrxy DOCS NOTE: SEE ATTACHED CLOSED PR THAT SHOULD BE APPLIED HERE ### Checked other resources - [X] I added a very descriptive title to this issue. - [x] I searched the LangChain documentation with the integrated search. - [X] I used the GitHub search to find a similar question and didn't find it. - [X] I am sure that this is a bug in LangChain rather than my code. - [X] The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package). ### Example Code 1. Use the following code to load a PDF with image extraction enabled with PyMuPDFLoader: ```python ######################################## # PyMuPDFLoader ######################################## from langchain_community.document_loaders import PyMuPDFLoader loader = PyMuPDFLoader("google-2024-environmental-report.pdf", extract_images=True) pages = loader.load() for page in pages: print(page.page_content) ``` 2. Download the PDF located at: [Google 2024 Environmental Report](https://www.gstatic.com/gumdrop/sustainability/google-2024-environmental-report.pdf). 3. Additionally, I also tried using PyPDFLoader with the same PDF, and I encountered the same issue. ### Error Message and Stack Trace (if applicable) ``` Traceback (most recent call last): File "/mnt/c/Users/BennisonJ/Yavar/projects/zypher-2.0/backend/apps/rag/main.py", line 811, in store_doc data = loader.load() ^^^^^^^^^^^^^ File "/home/bennison/miniconda3/envs/open-webui/lib/python3.11/site-packages/langchain_community/document_loaders/pdf.py", line 387, in load return list(self._lazy_load(**kwargs)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/bennison/miniconda3/envs/open-webui/lib/python3.11/site-packages/langchain_community/document_loaders/pdf.py", line 384, in _lazy_load yield from parser.lazy_parse(blob) File "/home/bennison/miniconda3/envs/open-webui/lib/python3.11/site-packages/langchain_community/document_loaders/parsers/pdf.py", line 244, in lazy_parse yield from [ ^ File "/home/bennison/miniconda3/envs/open-webui/lib/python3.11/site-packages/langchain_community/document_loaders/parsers/pdf.py", line 247, in <listcomp> + self._extract_images_from_page(doc, page), ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/bennison/miniconda3/envs/open-webui/lib/python3.11/site-packages/langchain_community/document_loaders/parsers/pdf.py", line 283, in _extract_images_from_page return extract_from_images_with_rapidocr(imgs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/bennison/miniconda3/envs/open-webui/lib/python3.11/site-packages/langchain_community/document_loaders/parsers/pdf.py", line 74, in extract_from_images_with_rapidocr result, _ = ocr(img) ^^^^^^^^ File "/home/bennison/miniconda3/envs/open-webui/lib/python3.11/site-packages/rapidocr_onnxruntime/rapid_ocr_api.py", line 80, in __call__ dt_boxes, det_elapse = self.text_detector(img) ^^^^^^^^^^^^^^^^^^^^^^^ File "/home/bennison/miniconda3/envs/open-webui/lib/python3.11/site-packages/rapidocr_onnxruntime/ch_ppocr_v3_det/text_detect.py", line 66, in __call__ data = transform(data, self.preprocess_op) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/bennison/miniconda3/envs/open-webui/lib/python3.11/site-packages/rapidocr_onnxruntime/ch_ppocr_v3_det/utils.py", line 220, in transform data = op(data) ^^^^^^^^ File "/home/bennison/miniconda3/envs/open-webui/lib/python3.11/site-packages/rapidocr_onnxruntime/ch_ppocr_v3_det/utils.py", line 75, in __call__ data['image'] = (img * self.scale - self.mean) / self.std ~~~~~~~~~~~~~~~~~^~~~~~~~~~~ ValueError: operands could not be broadcast together with shapes (896,800) (1,1,3) ``` ### Description I am encountering a ValueError when using both PyMuPDFLoader and PyPDFLoader to extract images from certain PDFs. The error message indicates that operands could not be broadcast together with shapes (896,800) (1,1,3). This occurs specifically when the extract_images parameter is set to True. **Expected Behavior** The code should successfully extract text and images from the PDF without errors. **Additional Information** This issue seems to occur with specific PDFs that may have unique formatting or image properties. I would appreciate any guidance on how to resolve this issue or if there are any workarounds available. ### System Info ``` langchain==0.1.16 langchain-chroma==0.1.0 langchain-community==0.0.34 langchain-core==0.1.52 langchain-text-splitters==0.0.2 PyMuPDF Version: 1.24.10 PyPDF Version: 4.2.0 Operating System: Ubuntu 22 LTS ```
yindo added the langchainbugintegration labels 2026-02-17 17:18:57 -05:00
yindo closed this issue 2026-02-17 17:18:57 -05:00
Author
Owner

@venkatram-dev commented on GitHub (Sep 9, 2024):

Looks like you might be using a old version of rapidocr_onnxruntime.
Please upgrade to latest or 1.3.24.

I did not get the error using this version.

pip show rapidocr_onnxruntime
Name: rapidocr-onnxruntime
Version: 1.3.24

https://github.com/RapidAI/RapidOCR/blob/50ce26671ab4a78955f8bc773348f82ad9fe0024/python/rapidocr_onnxruntime/ch_ppocr_v3_det/utils.py#L44](https://github.com/RapidAI/RapidOCR/commit/1de4db02497f832536ed1578e9bce5195850db8f)

@venkatram-dev commented on GitHub (Sep 9, 2024): Looks like you might be using a old version of rapidocr_onnxruntime. Please upgrade to latest or 1.3.24. I did not get the error using this version. pip show rapidocr_onnxruntime Name: rapidocr-onnxruntime Version: 1.3.24 https://github.com/RapidAI/RapidOCR/blob/50ce26671ab4a78955f8bc773348f82ad9fe0024/python/rapidocr_onnxruntime/ch_ppocr_v3_det/utils.py#L44](https://github.com/RapidAI/RapidOCR/commit/1de4db02497f832536ed1578e9bce5195850db8f)
Author
Owner

@kartheekyakkala commented on GitHub (Sep 9, 2024):

I think rapidocr-onnxruntime is a required package for extracting information from images. Can you please provide the versions of the rapidocr-onnxruntime?.

I have used all the latest versions of the packages. It worked fine.

@kartheekyakkala commented on GitHub (Sep 9, 2024): I think rapidocr-onnxruntime is a required package for extracting information from images. Can you please provide the versions of the rapidocr-onnxruntime?. I have used all the latest versions of the packages. It worked fine.
Author
Owner

@BennisonDevadoss commented on GitHub (Sep 10, 2024):

@kartheekyakkala, Thank you for addressing the issue!

After upgrading to the latest versions of the packages, the solution now works correctly for most scanned documents, which is a significant improvement.

However, I encountered a new issue when testing with the following PDF:

Scan Sample PDF

Unfortunately, the updated solution does not work for this particular PDF. The problem persists in a similar manner, where the image extraction and processing fail.

Details

  1. RapidOCR ONNXRuntime Version: 1.3.24
  2. Python Version: 3.11

Here I have attached the screenshot of the error
image

@BennisonDevadoss commented on GitHub (Sep 10, 2024): @kartheekyakkala, Thank you for addressing the issue! After upgrading to the latest versions of the packages, the solution now works correctly for most scanned documents, which is a significant improvement. However, I encountered a new issue when testing with the following PDF: [Scan Sample PDF](https://solutions.weblite.ca/pdfocrx/scansmpl.pdf) Unfortunately, the updated solution does not work for this particular PDF. The problem persists in a similar manner, where the image extraction and processing fail. **Details** 1. RapidOCR ONNXRuntime Version: 1.3.24 2. Python Version: 3.11 Here I have attached the screenshot of the error ![image](https://github.com/user-attachments/assets/9d0d21ea-d838-4701-89a2-cb40e2fb2cbf)
Author
Owner

@venkatram-dev commented on GitHub (Sep 10, 2024):

@BennisonDevadoss ,
This new error is coming from langchain-community package.
Looks like you are using a older version of it.

Please upgrade to latest version of langchain-community. Using the latest version worked for above file without any errors.

You might also want to update all the other langchain packages also to latest.

@venkatram-dev commented on GitHub (Sep 10, 2024): @BennisonDevadoss , This new error is coming from langchain-community package. Looks like you are using a older version of it. Please upgrade to latest version of langchain-community. Using the latest version worked for above file without any errors. You might also want to update all the other langchain packages also to latest.
Author
Owner

@BennisonDevadoss commented on GitHub (Sep 10, 2024):

@venkatram-dev, I updated the plugin versions to the latest, but I'm still encountering the same issue.

Below are my updated package versions:

langchain-community: 0.2.16
langchain-core:  0.2.38
langchain :0.2.16
@BennisonDevadoss commented on GitHub (Sep 10, 2024): @venkatram-dev, I updated the plugin versions to the latest, but I'm still encountering the same issue. Below are my updated package versions: ```requirements.txt langchain-community: 0.2.16 langchain-core: 0.2.38 langchain :0.2.16 ```
Author
Owner

@venkatram-dev commented on GitHub (Sep 10, 2024):

I am not able to reproduce the error. If possible, please share a google colab.

@venkatram-dev commented on GitHub (Sep 10, 2024): I am not able to reproduce the error. If possible, please share a google colab.
Author
Owner

@BennisonDevadoss commented on GitHub (Sep 17, 2024):

@venkatram-dev , as I mentioned earlier, I'm still encountering the issue we discussed previously, even with the latest version of the LangChain modules. I've shared the reproducible code on Google Colab, which you can access via the link below: Colab Link

Additionally, here's the PDF link I used to read the text from: PDF link

Please let me know if you find any breakthroughs!

@BennisonDevadoss commented on GitHub (Sep 17, 2024): @venkatram-dev , as I mentioned earlier, I'm still encountering the issue we discussed previously, even with the latest version of the LangChain modules. I've shared the reproducible code on Google Colab, which you can access via the link below: [Colab Link](https://colab.research.google.com/drive/12NMHOT6aYQVRQBsiJegCpdwtCUtIjttq?usp=drive_link) Additionally, here's the PDF link I used to read the text from: [PDF link](https://solutions.weblite.ca/pdfocrx/scansmpl.pdf) Please let me know if you find any breakthroughs!
Author
Owner

@jamesrooti commented on GitHub (Nov 18, 2024):

Is there progress on this? Experiencing the same issue. Thanks

@jamesrooti commented on GitHub (Nov 18, 2024): Is there progress on this? Experiencing the same issue. Thanks
Author
Owner

@BennisonDevadoss commented on GitHub (Feb 12, 2025):

@jamesrooti, Yes I am having same issue.

@BennisonDevadoss commented on GitHub (Feb 12, 2025): @jamesrooti, Yes I am having same issue.
Author
Owner

@dosubot[bot] commented on GitHub (May 14, 2025):

Hi, @BennisonDevadoss. I'm Dosu, and I'm helping the LangChain team manage their backlog. I'm marking this issue as stale.

Issue Summary:

  • You reported difficulties extracting images from PDFs using PyMuPDFLoader and PyPDFLoader.
  • The issue persists with specific PDFs, even after updating related packages.
  • Venkatram-dev suggested updating to the latest versions, but the problem remains.
  • You provided a Google Colab link for reproducibility.
  • Jamesrooti also reported experiencing the same issue, indicating a broader problem.

Next Steps:

  • Please confirm if this issue is still relevant with the latest version of LangChain.
  • If the issue is still present, you can keep the discussion open by commenting here; otherwise, it will be automatically closed in 7 days.

Thank you for your understanding and contribution!

@dosubot[bot] commented on GitHub (May 14, 2025): Hi, @BennisonDevadoss. I'm [Dosu](https://dosu.dev), and I'm helping the LangChain team manage their backlog. I'm marking this issue as stale. **Issue Summary:** - You reported difficulties extracting images from PDFs using PyMuPDFLoader and PyPDFLoader. - The issue persists with specific PDFs, even after updating related packages. - Venkatram-dev suggested updating to the latest versions, but the problem remains. - You provided a Google Colab link for reproducibility. - Jamesrooti also reported experiencing the same issue, indicating a broader problem. **Next Steps:** - Please confirm if this issue is still relevant with the latest version of LangChain. - If the issue is still present, you can keep the discussion open by commenting here; otherwise, it will be automatically closed in 7 days. Thank you for your understanding and contribution!
Author
Owner

@BennisonDevadoss commented on GitHub (May 14, 2025):

Yes, this issue still exists.

@BennisonDevadoss commented on GitHub (May 14, 2025): Yes, this issue still exists.
Author
Owner

@dosubot[bot] commented on GitHub (May 14, 2025):

@eyurtsev, the user @BennisonDevadoss has confirmed that the issue with extracting images from PDFs using PyMuPDFLoader and PyPDFLoader is still relevant. Could you please assist them with this problem?

@dosubot[bot] commented on GitHub (May 14, 2025): @eyurtsev, the user @BennisonDevadoss has confirmed that the issue with extracting images from PDFs using PyMuPDFLoader and PyPDFLoader is still relevant. Could you please assist them with this problem?
Author
Owner

@skyking363 commented on GitHub (Jun 17, 2025):

Me too

@skyking363 commented on GitHub (Jun 17, 2025): Me too
Author
Owner

@john01kpeazu commented on GitHub (Jul 1, 2025):

Same here. Not able to extract OCR information from PDFs using PyMuPDFLoader or PyPDFLoader.

@john01kpeazu commented on GitHub (Jul 1, 2025): Same here. Not able to extract OCR information from PDFs using PyMuPDFLoader or PyPDFLoader.
Author
Owner

@eugeneo17 commented on GitHub (Jul 17, 2025):

I'd like to take this issue and submit a fix. Let me know if anyone is already working on it!

@eugeneo17 commented on GitHub (Jul 17, 2025): I'd like to take this issue and submit a fix. Let me know if anyone is already working on it!
Author
Owner

@mdrxy commented on GitHub (Jul 21, 2025):

@eugeneo17 you got it

@mdrxy commented on GitHub (Jul 21, 2025): @eugeneo17 you got it
yindo changed title from Error when extracting images with PyMuPDFLoader and PyPDFLoader to [GH-ISSUE #294] Error when extracting images with PyMuPDFLoader and PyPDFLoader 2026-06-05 17:24:30 -04:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/docs#2