Build Gmail AI Agent using LangChain Agents and OpenAI

Build Gmail AI Agent using LangChain Agents and OpenAI

The Gmail API allows developers to create applications that interact with user's emails. This capability enables developers to leverage the vast amounts of data and communication flows that occur daily, creating tools that enhance productivity, automate tasks, and introduce innovative ways to manage and organize information.

Now, if you’re dreaming of apps that are not just smart but genius-level 🧠, you’ve gotta check out LangChain Agents. It’s your golden ticket 🎫 to combining Google’s APIs with the brainpower of Large Language Models (LLMs). With LangChain Agents and Tools, you’re just a jump away from creating apps that are as clever as they come.

Want the inside scoop on LangChain and its integration with LLMs? Dive into our blogs Using Langchain and Open Source Vector DB Chroma for Semantic Search with OpenAI's LLM for all the deets. 📚

In this article, I’m going to walk you through a couple of nifty examples of how LangChain plays nice with Gmail API using LangChain Gmail Toolkit. You can create your own personalized Gmail AI Agent. Plus, I’ll spill the beans on some cool ways you can use these dynamic duos. Let’s get this started! 🎉


  1. To use Google's APIs, developers will need to create a Google Cloud account and set up a project Click to learn how to Create and Managing Projects in Google Cloud

  2. Authorize Google Credentials

If you don't know how to authorize Google credentials for your app, then let me give you a quick walkthrough

Authorize Google Credentials for Desktop App:

Step 1: Enable the API that you want to work on (Google Drive API, Gmail API, etc)

Search for the API name in the Google Cloud Console 👆

And Click on Enable 👇

Step 2: Configure the OAuth consent screen

  1. In the Google Cloud console, go to Menu menu > APIs & Services > OAuth consent screen.

  2. For User type select Internal, then click Create.

  3. Complete the app registration form, then click Save and Continue.

  4. For now, you can skip adding scopes and click Save and Continue.

Review your app registration summary. To make changes, click Edit. If the app registration looks OK, click Back to Dashboard.

Step 3: Download Credentials

  1. In the Google Cloud console, go to Menu menu > APIs & Services > Credentials.

  2. Click Create Credentials > OAuth client ID.

  1. Click Application type > Desktop app. In the Name field, type a name for the credential. This name is only shown in the Google Cloud console. Click Create.

  1. The OAuth client created screen appears, showing your new Client ID and Client secret. Click OK. The newly created credential appears under OAuth 2.0 Client IDs.

  2. Save the downloaded JSON file as credentials.json, and move the file to your working directory.

Working with Gmail using LangChain

  1. Let's Import libraries
pip install --upgrade --quiet  google-auth google-auth-oauthlib google-auth-httplib2 google-api-python-client langchain langchain-community langchain-openai langchainhub
  1. Create the Gmail Toolkit
from langchain_community.agent_toolkits import GmailToolkit
from import (

credentials = get_gmail_credentials(
api_resource = build_resource_service(credentials=credentials)
toolkit = GmailToolkit(api_resource=api_resource)

The first time you use get_gmail_credentials(), you will be displayed with the consent screen in your browser for user authentication. After authentication, 'token.json' will be created automatically at the provided or the default path. Also, if there is already a token.json at that path, then you will not be prompted for authentication. If you don’t know how to generate user access token, then follow this guide.

What are scopes?

Scopes are the level of access that you need or that is granted to an access token.For example, an access token issued to a client app may be granted READ and WRITE access to protected resources, or just READ access. So, if a client receives a token that has READ scope, and it tries to call an API endpoint that requires WRITE access, the call will fail.

You Can review scopes here 👇 (For instance, readonly scope is '' )

  1. List down all the tools
tools = toolkit.get_tools()
[GmailCreateDraft(api_resource=<googleapiclient.discovery.Resource object at 0x7d5cbc7d5e10>),
 GmailSendMessage(api_resource=<googleapiclient.discovery.Resource object at 0x7d5cbc7d5e10>),
 GmailSearch(api_resource=<googleapiclient.discovery.Resource object at 0x7d5cbc7d5e10>),
 GmailGetMessage(api_resource=<googleapiclient.discovery.Resource object at 0x7d5cbc7d5e10>),
 GmailGetThread(api_resource=<googleapiclient.discovery.Resource object at 0x7d5cbc7d5e10>)]
  1. Working with Gmails with OpenAI Tools Agent
import getpass
import os

os.environ["OPENAI_API_KEY"] = getpass.getpass()
from langchain import hub
from langchain.agents import AgentExecutor, create_openai_tools_agent
from langchain_openai import ChatOpenAI
instructions = """You are an assistant."""
base_prompt = hub.pull("langchain-ai/openai-functions-template")
prompt = base_prompt.partial(instructions=instructions)
llm = ChatOpenAI(temperature=0)

agent = create_openai_tools_agent(llm, toolkit.get_tools(), prompt)

agent_executor = AgentExecutor(
    verbose=True, #set it True to see what happens behind the scenes
    {"input": "Could you search in my drafts for the latest email? what is the title?"}
{'input': 'Could you search in my drafts for the latest email? what is the title?',
 'output': 'The latest email in your drafts is titled "Reschedule Meeting to Thursday".'}

Pass Custom Prompt:

Instead of providing base prompt from langchain hub, you can pass your custom prompt to the agent. In this way, you can expect more desirable output from the agent. Also, if you're making a ChatBot, then it is easier for you to provide previous conversations or chat history.

from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder

prompt = ChatPromptTemplate.from_messages(
        ("system", """
You are a helpful assistant.
        MessagesPlaceholder("chat_history", optional=True),
        ("human", "{input}"),

agent = create_openai_tools_agent(llm, tools, prompt)
agent_executor = AgentExecutor(

response = agent_executor.invoke(
                        "input": user_query,
                        "chat_history": chat_history,

Use Cases:

  • Email Automation: Automate sending and receiving emails, which can be particularly useful for customer support or marketing campaigns.

  • Message Retrieval: Search for and retrieve email messages or threads based on specific queries, aiding in information management and organization.

  • Draft Creation: Create email drafts automatically, allowing for quick follow-ups or scheduled correspondence.

  • Email Parsing: Extract and parse information from emails, which can be used for lead generation or data entry tasks.


In this article, we delved into the powerful capabilities of LangChain tools, specifically their integration with Gmail. By harnessing LangChain’s innovative solutions, users can seamlessly access and manipulate their documents, transforming mundane tasks into efficient workflows. Whether it’s extracting structured answers, building chatbots, or analyzing queries, LangChain empowers developers to create intelligent applications that bridge the gap between language and data.

Remember, the journey doesn’t end here. Dive deeper, explore further, and let LangChain be your guide to a more streamlined and intelligent digital experience. Happy coding! 🚀🔍📚

Resources and Further Readings: