Open In App

How to Use Google Bard with Python

Last Updated : 04 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Google Bard (Currently known as Gemini) is an advanced AI chatbot developed by Google that can help with answering questions, summarizing content, writing creatively, and much more. However, as of now, Google has not released an official API for Bard. But using the open-source Python package bardapi, we can interact with Bard programmatically using a workaround based on session cookies.

Note: This method is unofficial and relies on browser session cookies. It may stop working if Google changes how Bard handles authentication.

Advantages of Google Bard

  • It is easy to use we can ask anything by writing any query as we type a message or we can also ask just by saying by clicking on the mic.
  • It personalizes the result of user queries according to the previous history of queries asked by the user.
  • It can make finding the information we need as we do not follow the procedure of using Google search.
  • It provides answers to user queries very quickly, usually within a second.

Limitations of Google Bard

  • As it is still in an experimental phase or Beta Phase it provides less accurate information such as writing codes but generally, Google Bard provides accurate information.
  • It is available in multiple languages but it may be able to provide answers in some specific languages only.
  • It is developed to answer specific types of queries and may not be able to answer some complex queries.

Using Google Bard with Python

We’ll create a simple command-line tool where you can enter a prompt, and Bard will respond in natural language.

Step 1: Install Required Packages

Before proceeding, ensure you have Python installed. Then, install the other required libraries using this command:

pip install bardapi python-dotenv

  • bardapi allows interaction with Bard.
  • python-dotenv helps securely load environment variables (like API keys) from a .env file.

Step 2: Get the Required Bard Cookie

To authenticate with Bard unofficially, you’ll need to extract the __Secure-1PSID cookie from your browser:

  1. Open Google Bard in Chrome.
  2. Press F12 or Ctrl + Shift + I to open Developer Tools.
  3. Go to the Application tab.
  4. Under Storage – Cookies, click on https://bard.google.com (currently its- https://gemini.google.com).
  5. Look for __Secure-1PSID, then copy its value.
bard-cookie

__Secure-1PSID will be at the area highlighted as red

As with any API key, it’s a good practise to avoid putting it directly into your code. Therefore we will create a .env file and add a line for the value. Note that the key name _BARD_API_KEY is the exact name required by the Python library:

bard-env

Saving key in .env file

Step 3: Create app.py file

Create a new python file named “app.py” (you can name it whatever you want), this will contain the main code of our app. Here is the code:

Python
from bardapi import Bard
from dotenv import load_dotenv
load_dotenv()
 
def call_bard(query):
    """Calls the Bard API to get an answer for the given query."""
    try:
        bard = Bard()  # Ensure API key is loaded
        answer = bard.get_answer(query)
        return answer['content']
    except Exception as e:
        return f"An error occurred while calling Bard: {e}"

def main():
        prompt = input("Enter Prompt: ")
        res = call_bard(prompt)
        print(res)

if __name__ == "__main__":
    main()

Code Explanation:

  • load_dotenv() – Loads the .env file so your API key is securely injected into the environment.
  • Bard() – The Bard object automatically looks for the _BARD_API_KEY variable and uses it to authenticate.
  • get_answer(query) – Sends the user prompt to Bard and retrieves the AI’s response.
  • main() function is a simple CLI loop where the user enters a prompt and gets the result printed.

Output:

Run the app.py file and to check whether the code is working or not give this prompt in the terminal – “Suggest me some of the best action movies”:

bard-terminal

Snapshot of the terminal output



Next Article
Article Tags :
Practice Tags :

Similar Reads