Skip to main content

Google Drive

This notebook covers how to retrieve documents from Google Drive.


  1. Create a Google Cloud project or use an existing project
  2. Enable the Google Drive API
  3. Authorize credentials for desktop app
  4. pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib

Retrieve the Google Docs

By default, the GoogleDriveRetriever expects the credentials.json file to be ~/.credentials/credentials.json, but this is configurable using the GOOGLE_ACCOUNT_FILE environment variable. The location of token.json uses the same directory (or use the parameter token_path). Note that token.json will be created automatically the first time you use the retriever.

GoogleDriveRetriever can retrieve a selection of files with some requests.

By default, If you use a folder_id, all the files inside this folder can be retrieved to Document.

You can obtain your folder and document id from the URL:

The special value root is for your personal home.

from langchain_googledrive.retrievers import GoogleDriveRetriever

folder_id = "root"
# folder_id='1yucgL9WGgWZdM1TOuKkeghlPizuzMYb5'

retriever = GoogleDriveRetriever(

By default, all files with these MIME types can be converted to Document.

  • text/text
  • text/plain
  • text/html
  • text/csv
  • text/markdown
  • image/png
  • image/jpeg
  • application/epub+zip
  • application/pdf
  • application/rtf
  • application/ (GDoc)
  • application/ (GSlide)
  • application/ (GSheet)
  • application/ (Notebook colab)
  • application/vnd.openxmlformats-officedocument.presentationml.presentation (PPTX)
  • application/vnd.openxmlformats-officedocument.wordprocessingml.document (DOCX)

It's possible to update or customize this. See the documentation of GoogleDriveRetriever.

But, the corresponding packages must be installed.

%pip install --upgrade --quiet  unstructured
retriever.invoke("machine learning")

You can customize the criteria to select the files. A set of predefined filter are proposed:

gdrive-all-in-folderReturn all compatible files from a folder_id
gdrive-querySearch query in all drives
gdrive-by-nameSearch file with name query
gdrive-query-in-folderSearch query in folder_id (and sub-folders in _recursive=true)
gdrive-mime-typeSearch a specific mime_type
gdrive-mime-type-in-folderSearch a specific mime_type in folder_id
gdrive-query-with-mime-typeSearch query with a specific mime_type
gdrive-query-with-mime-type-and-folderSearch query with a specific mime_type and in folder_id
retriever = GoogleDriveRetriever(
template="gdrive-query", # Search everywhere
num_results=2, # But take only 2 documents
for doc in retriever.invoke("machine learning"):
print(doc.page_content.strip()[:60] + "...")

Else, you can customize the prompt with a specialized PromptTemplate

from langchain_core.prompts import PromptTemplate

retriever = GoogleDriveRetriever(
# See
template="(fullText contains '{query}') "
"and mimeType='application/' "
"and modifiedTime > '2000-01-01T00:00:00' "
"and trashed=false",
# See
for doc in retriever.invoke("machine learning"):
print(doc.page_content.strip()[:60] + "...")
API Reference:PromptTemplate

Use Google Drive 'description' metadata

Each Google Drive has a description field in metadata (see the details of a file). Use the snippets mode to return the description of selected files.

retriever = GoogleDriveRetriever(
mime_type="application/", # Only Google Docs
retriever.invoke("machine learning")

Was this page helpful?

You can also leave detailed feedback on GitHub.