GPT Actions library - BigQuery

Jul 9, 2024
Open in Github

This particular GPT Action provides an overview of how to connect to Google BigQuery, Google Cloud's Analytical Data Warehouse. This Action takes a user’s question, scans the relevant tables to gather the data schema, then writes a SQL query to answer the user’s question.

Note: these instructions return back a functioning SQL statement, rather than the result itself. Currently middleware is required to return back a CSV file – we’ll be posting instructions on an example of that soon

Before you get started, make sure you go through the following steps in your application environment:

  • Set up a GCP project
  • Set up a BQ dataset in that GCP project
  • Ensure that the user authenticating into BigQuery via ChatGPT has access to that BQ dataset

Once you've created a Custom GPT, copy the text below in the Instructions panel. Have questions? Check out Getting Started Example to see how this step works in more detail.

**Context**: You are an expert at writing BigQuery SQL queries. A user is going to ask you a question. 

1. No matter the user's question, start by running `runQuery` operation using this query: "SELECT column_name, table_name, data_type, description FROM `{project}.{dataset}.INFORMATION_SCHEMA.COLUMN_FIELD_PATHS`" 
-- Assume project = "<insert your default project here>", dataset = "<insert your default dataset here>", unless the user provides different values 
-- Remember to include useLegacySql:false in the json output
2. Convert the user's question into a SQL statement that leverages the step above and run the `runQuery` operation on that SQL statement to confirm the query works. Add a limit of 100 rows
3. Now remove the limit of 100 rows and return back the query for the user to see

**Additional Notes**: If the user says "Let's get started", explain that the user can provide a project or dataset, along with a question they want answered. If the user has no ideas, suggest that we have a sample flights dataset they can query - ask if they want you to query that

Once you've created a Custom GPT, copy the text below in the Actions panel. Have questions? Check out Getting Started Example to see how this step works in more detail.

openapi: 3.1.0
  title: BigQuery API
  description: API for querying a BigQuery table.
  version: 1.0.0
  - url:
    description: Google BigQuery API server
      operationId: runQuery
      summary: Executes a query on a specified BigQuery table.
      description: Submits a query to BigQuery and returns the results.
        - name: projectId
          in: path
          required: true
          description: The ID of the Google Cloud project.
            type: string
        required: true
              type: object
                  type: string
                  description: The SQL query string.
                  type: boolean
                  description: Whether to use legacy SQL.
                  default: false
          description: Successful query execution.
                type: object
                    type: string
                    example: "bigquery#queryResponse"
                    type: object
                    description: The schema of the results.
                    type: object
                        type: string
                        type: string
                    type: array
                      type: object
                          type: array
                            type: object
                                type: string
                    type: string
                    description: Total number of rows in the query result.
                    type: string
                    description: Token for pagination of query results.
          description: Bad request. The request was invalid.
          description: Unauthorized. Authentication is required.
          description: Forbidden. The request is not allowed.
          description: Not found. The specified resource was not found.
          description: Internal server error. An error occurred while processing the request.

Below are instructions on setting up authentication with this 3rd party application. Have questions? Check out Getting Started Example to see how this step works in more detail.

Before you set up authentication in ChatGPT, please take the following steps in the application.

  • Go to the Google Cloud Console
  • Navigate to API & Services > Credentials
  • Create new OAuth credentials (or use an existing one)
  • Locate your OAuth Client ID & Client Secret and store both values securely (see screenshot below)


In ChatGPT, click on "Authentication" and choose "OAuth". Enter in the information below.

Once you've set up authentication in ChatGPT, follow the steps below in the application to finalize the Action.

  • Copy the callback URL from the GPT Action
  • In the “Authorized redirect URIs” (see screenshot above), add your callback URL
  • Callback URL Error: If you get a callback URL error in ChatGPT, pay close attention to the screenshot above. You need to add the callback URL directly into GCP for the action to authenticate correctly
  • Schema calls the wrong project or dataset: If ChatGPT calls the wrong project or dataset, consider updating your instructions to make it more explicit either (a) which project / dataset should be called or (b) to require the user provide those exact details before it runs the query

Are there integrations that you’d like us to prioritize? Are there errors in our integrations? File a PR or issue in our github, and we’ll take a look.