Access Google Bard with Python Package Bard-API

Marc Bolle
5 min readMay 19, 2023

In the realm of conversational AI, Google Bard stands out as an impressive text generation model that responds intelligently to user input and interactions.

Leveraging the capabilities of Google’s PaLM 2 large language model (LLM), Bard proves to be a valuable tool for brainstorming ideas, enhancing productivity, and accomplishing a range of tasks.

Google doesn’t offer access to the Bard API yet. In the meanwhile, Python developers can take advantage of the recently released open-source Bard-API Python package.

In this article, I’ll guide you through installing the Bard-API package and retrieving the response from Bard in your notebook, step by step.

Table of Contents

◦ Bard-API package
◦ Install Bard-API package
◦ Set your API key
◦ Send an API request to Bard and get a Response
◦ Create a Pandas dataframe with Bard’s responses
◦ Continue the conversation with Bard

Bard-API package

Bard-API is a convenient Python package developed by Daniel Park specifically designed to return Google Bard’s response via an API.

By utilizing Bard-API, users can easily incorporate Bard’s natural language responses into their Python environment, and integrate them in various applications.

The setup process of Bard-API is straightforward.

Install Bard-API package

You can install the latest stable release of Bard-API from PyPI as follow:

pip install bardapi

You can also install the development version from GitHub:

pip install git+https://github.com/dsdanielpark/Bard-API.git

Set your API key

After successfully installing the Bard-API package, the next step is to proceed with authentication. To authenticate, you will need to utilize your __Secure-1PSIDvalue, which can be located in the cookies while navigating through Bard.

It’s important to note that although the __Secure-1PSID is commonly referred to as an API KEY for convenience, it is not officially designated as such. Additionally, it is crucial to ensure that you do not expose or share your value to maintain the security of your access.

  1. Visit https://bard.google.com/
  2. Press F12 or right-click and ‘Inspect’:

3. Navigate to Application → Cookies → and copy the value of your __Secure-1PSID cookie:

4. Replace YOUR_KEY by your __Secure-1PSID value to authenticate:

from bardapi import Bard
import os

#Replace XXXX with the values you get from __Secure-1PSID
os.environ['_BARD_API_KEY']="YOUR_KEY"

You are now connected to Bard and can send requests and receive responses via the API.

Send an API request to Bard and get a Response

Sending a request to Bard and getting a response with Bard-API is as simple as that:

# Set your input text
input_text = "what is google bard?"

# Send an API request and get a respons
bard_output = Bard().get_answer(input_text)['content']
print(bard_output)

Create a Pandas dataframe with Bard’s responses

Having Bard’s responses in a Python environment can be advantageous for various reasons, with one notable use case being the ability to store them and analyze them.

One approach to accomplish this is by storing the responses in a Pandas dataframe. This particular format will enable you to perform natural language processing (NLP) analyses and even develop machine learning models based on Bard responses.

You can store Bard responses in a Pandas dataframe as follow:

import pandas as pd 

# Generate another output from Bard
input_text_2 = "what is chatgpt?"
bard_output_2 = Bard().get_answer(input_text)['content']

# Create a dataframe with Input and Output columns
df = pd.DataFrame(columns=["Input", "Output"])

# Create new rows as dataframes with the reponses
new_row = pd.DataFrame({"Input": [input_text], "Output": [bard_output]})
new_row1 = pd.DataFrame({"Input": [input_text_2], "Output": [bard_output_2]})

# Append the new rows to the existing dataframe
df = pd.concat([df, new_row], ignore_index=True)
df = pd.concat([df, new_row1], ignore_index=True)

# Print the dataframe
print(df)

Continue the conversation with Bard

In the previous examples, Bard responded to each request individually. Additionally, you have the option to engage in a conversation with Bard by utilizing a reusable session.

from bardapi import Bard
import os
import requests

# Set the Bard API key in the environment variable
os.environ['_BARD_API_KEY'] = 'YOUR_KEY'

# Create a session object using the requests library
session = requests.Session()

# Set the headers for the session
session.headers = {
"Host": "bard.google.com",
"X-Same-Domain": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
"Origin": "https://bard.google.com",
"Referer": "https://bard.google.com/",
}

# Set the "__Secure-1PSID" cookie with the Bard API key
session.cookies.set("__Secure-1PSID", os.getenv("_BARD_API_KEY"))

# Create a Bard object with the session and a timeout of 30 seconds
bard = Bard(session=session, timeout=30)

# Send an API request and get a response
bard.get_answer("Hi Bard! Who is the President of France?")['content']

You can now continue the conversation:

# Send another API request and get a response
bard.get_answer("Are you sure?")['content']

Conclusion

In conclusion, the Bard-API package offers a convenient and efficient way to interact with Google Bard’s response API within your Python environment until Google releases Bard API.

You can now explore the full potential of Bard-API, experiment with different queries, and unlock the valuable insights that Bard can provide!

--

--