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

GoogleDriveReader support file extensions #17620

Merged

Conversation

cmiller01
Copy link
Contributor

Description

Previously GoogleDrive folders that had file extensions besides Google Sheets/Docs/Presentation would have their extensions stripped because the filename was the Google Drive file ID which doesn't have an extension. However this meant that reading other types of file formats (like PDFs) wouldn't work and the "file_extractor" capability wouldn't be used, instead everything would just be reading bytes. That works ok for .txt or .md docs, but for a PDF it's generates a bunch of garbage.

This change adds the file extension if it can be returned, otherwise it'll just add empty string and shouldn't create any issues

It's possible in GoogleDrive to also remove the extension of a file or upload a file without an extension. In that case this should make no change.

Note, depending on how you use this reader it could be a breaking change if some files were previously being parsed as bytes/text but would now be getting parsed by the file_extractor, but it seems like this may be the expected behavior already.

Fixes # (issue)

Version Bump?

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

  • Yes (not sure if a minor version)
  • No

Type of Change

Depending on usage, I suppose it could be any of these... 😄

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

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
  • I believe this change is already covered by existing unit tests

I also ran a script against my Google Drive where I uploaded some some PDFs:

# drive.py
from llama_index.readers.google import GoogleDriveReader

reader = GoogleDriveReader(
        folder_id="1Iv9Gb0wPKWznXFbnse0PSK87wMbkivC-" # https://drive.google.com/drive/u/0/folders/1Iv9Gb0wPKWznXFbnse0PSK87wMbkivC-
)

docs = reader.load_data()
for d in docs:
    print("============ document ==========\n")
    print(f"file id {d.metadata["file id"]}, file path {d.metadata["file path"]}")
    print(d.text)
(env) ➜  llama_index git:(google-drive-reader-extensions) ✗ python3 drive.py
============ document ==========

file id 1OLBPt8_N4ygsPyIE5xc56WE4QPhROQLyE7N8APOn03c, file path llama-index-google-drive/test
Test docs
============ document ==========

file id 1j2_WFOB1S1ZMwaEsl7EjrQYbLN279lz5, file path llama-index-google-drive/hello world.pdf
hello world!
============ document ==========

file id 1ltDc3o0nXEK7V2iESTzwpfAKzBjOUCEk, file path llama-index-google-drive/hello world again
%PDF-1.7
%
4 0 obj
<< /Length 5 0 R
   /Filter /FlateDecode
>>
stream
x-
@DK#{V
... 
etc etc

Suggested Checklist:

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

Previously GoogleDrives folders that had file extentions besides Google Sheets/Docs/Presentation
would have their extensions stripped because the filename was the Google Drive file ID which
doesn't have an extension. However this meant that reading other types of file formats (like PDFs)
wouldn't work and the "file_extractor" capability wouldn't be used, instead everything would just be reading bytes.
That works ok for .txt or .md docs, but for a PDF it's generates a bunch of garbage.

This change adds the file extension if it can be returned, otherwise it'll just add empty string and shouldn't create any issues

It's possible in GoogleDrive to also remove the extension, but that should then fall back to the prior behavior

This could be a breaking change if some files were previously being parsed as bytes/text but would now be getting parsed by the file_extractor
@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Jan 25, 2025
@cmiller01
Copy link
Contributor Author

I had searched open issues for this Google Drive issue, but it turns out there was already a PR with a very similar change too! #17294 huzzah! We made the change in slightly different places, 🤷

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Jan 25, 2025
@logan-markewich logan-markewich merged commit 957bde1 into run-llama:main Jan 25, 2025
10 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm This PR has been approved by a maintainer size:M This PR changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants