Skip to main content

Google Drive

This notebook walks through connecting a LangChain to the Google Drive API.


  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

Instructions for retrieving your Google Docs data​

By default, the GoogleDriveTools and GoogleDriveWrapper 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 use the same directory (or use the parameter token_path). Note that token.json will be created automatically the first time you use the tool.

GoogleDriveSearchTool 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, if the name match the query.

#!pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib

You can obtain your folder and document id from the URL: * Folder: -> folder id is "1yucgL9WGgWZdM1TOuKkeghlPizuzMYb5" * Document: -> document id is "1bfaMQ18_i56204VaQDVeAFpqEijJTgvurupdEDiaUQw"

The special value root is for your personal home.

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

By default, all files with these mime-type 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 GoogleDriveAPIWrapper.

But, the corresponding packages must installed.

#!pip install unstructured
from import GoogleDriveSearchTool
from langchain.utilities.google_drive import GoogleDriveAPIWrapper

# By default, search only in the filename.
tool = GoogleDriveSearchTool(
template="gdrive-query-in-folder", # Search in the body of documents
import logging

logging.basicConfig(level=logging.INFO)"machine learning")
from langchain.agents import load_tools

tools = load_tools(

Use within an Agent​

from langchain.agents import AgentType, initialize_agent
from langchain.llms import OpenAI

llm = OpenAI(temperature=0)
agent = initialize_agent(
)"Search in google drive, who is 'Yann LeCun' ?")