Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of PurePosixPath in PDFReader #16612

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Anitha6g
Copy link

Description

Solution to bug - #16602 - S3Reader: Failed to load files other than .txt using S3Reader

ISSUE:
Failed to load file test-llamaindex/file1.docx with error: Attempt to open non key-like path: test-llamaindex\file1.docx. Skipping...
Failed to load file test-llamaindex/file2.pdf with error: RetryError[<Future at 0x1122b0b42b0 state=finished raised ValueError>]. Skipping...

Version
llama_index Version: 0.11.18, llama-index-readers-s3 Version: 0.2.0

Steps to Reproduce
This is the code used

`from llama_index.readers.s3 import S3Reader
from llama_index.readers.file import PDFReader, DocxReader

reader = S3Reader(
bucket="test-llamaindex",
aws_access_id=AWS_ACCESS_KEY_ID,
aws_access_secret=AWS_SECRET_ACCESS_KEY,
aws_session_token=AWS_SESSION_TOKEN,
recursive=True,
required_exts=[".pdf", ".docx", ".txt"],
file_extractor={".pdf": PDFReader(), ".docx": DocxReader()}
)
documents = reader.load_data()
documents`

DEBUGGED:
The issue is in the class PDFReader, in the below condition-
"""Parse file."""
if not isinstance(file, Path):
file = Path(file)
Since my file was of type <class 'pathlib.PurePosixPath'>, the IF condition passed and the Path(file) changes the "/" in the file path to "" which is not supported by s3fs and gives this error -
Attempt to open non key-like path: test-llamaindex\ShortStartStopMove.pdf
in line with fs.open(str(file), "rb") as fp:

SOLUTION:
Converting the file into posix path.
"""Parse file."""
if not isinstance(file, Path) and not isinstance(file, PurePosixPath):
file = PurePosixPath(file)

Fixes # (issue)
#16602

New Package?

PurePosixPath

Did I fill in the tool.llamahub section in the pyproject.toml and provide a detailed README.md for my new integration or package?

  • Yes
  • [ Y] No

Version Bump?

No

Did I bump the version in the pyproject.toml file of the package I am updating? (Except for the llama-index-core package)

  • Yes
  • [ Y] No

Type of Change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

Your pull-request will likely not be merged unless it is covered by some form of impactful unit testing.

  • I added new unit tests to cover this change
  • [Y ] I believe this change is already covered by existing unit tests

Suggested Checklist:

  • [Y] I have performed a self-review of my own code
  • [Y] I have commented my code, particularly in hard-to-understand areas
  • [ Y] I have made corresponding changes to the documentation
  • I have added Google Colab support for the newly added notebooks.
  • [ Y] My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • [ Y] New and existing unit tests pass locally with my changes
  • [ Y] I ran make format; make lint to appease the lint gods

@dosubot dosubot bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Oct 19, 2024
if not isinstance(file, Path):
file = Path(file)
if not isinstance(file, Path) and not isinstance(file, PurePosixPath):
file = PurePosixPath(file)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we should only be using PurePosixPath when fs is some remote file system -- and probably only in simple directory reader?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size:XS This PR changes 0-9 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants