Skip to main content

CI pipeline status PyPI PyPI - Python Version codecov Checked with mypy Ruff

GBQ

Python wrapper for interacting Google BigQuery.

This package provides an interface by wrapping Google's low level library. It exposes options to provide input as json objects which can be used for various CI/CD tools.

Usage

Basic Usage

from gbq import BigQuery

# BigQuery project id as listed in the Google Developers Console.
project_id = 'project_id'

# BigQuery dataset id as listed in the Google Developers Console.
dataset_id = 'dataset_id'

# BigQuery table/view id as listed in the Google Developers Console.
structure_id = 'structure_id'

# BigQuery structure definition as defined in the Google Developers Console.
json_schema = {
    "type": "table",
    "schema": [{"name": "id", "type": "INTEGER", "mode": "REQUIRED"}],
}

# Authenticates with Application Default Credentials: Workload Identity, an attached
# service account, GOOGLE_APPLICATION_CREDENTIALS, or `gcloud auth application-default login`.
bq = BigQuery(project=project_id)

bq.create_or_update_structure(project_id, dataset_id, structure_id, json_schema)

Updating Existing Tables

create_or_update_structure creates the structure if it doesn't exist. Otherwise it updates it to match the definition, and skips the API call when nothing changed:

  • The definition is the source of truth. Labels, description and clustering that the definition omits are removed from the existing table or view.
  • Schema fields keep every attribute in the definition, such as policyTags, precision and maxLength.
  • BigQuery cannot change an existing table's partitioning, except a time partition's expiration. If the definition's partitioning differs in any other way, InvalidDefinitionException is raised and the table is left unchanged; recreate the table to change it.

Stored Procedures

A definition with a body creates or updates a SQL stored procedure. The body can be a string or a list of lines, and each argument's data_type can be any BigQuery type, including ARRAY<...> and STRUCT<...>:

procedure = {
    "body": [
        "INSERT INTO `project_id.dataset_id.events` (user_id, tags)",
        "SELECT user.id, tags FROM UNNEST([user])",
    ],
    "arguments": [
        {"name": "user", "data_type": "STRUCT<id INT64, name STRING>"},
        {"name": "tags", "data_type": "ARRAY<STRING>"},
    ],
    "description": "Records an event for a user",
}

bq.create_or_update_structure(project_id, dataset_id, "record_event", procedure)

Authentication

BigQuery uses Application Default Credentials unless you pass credentials explicitly. Prefer this, or a google.auth credentials object, over long-lived service account keys:

import google.auth
from google.auth import impersonated_credentials

source, _ = google.auth.default()
credentials = impersonated_credentials.Credentials(
    source_credentials=source,
    target_principal="deployer@project_id.iam.gserviceaccount.com",
    target_scopes=["https://www.googleapis.com/auth/bigquery"],
)
bq = BigQuery(project=project_id, credentials=credentials)

# A service account key as a JSON string is still supported
bq = BigQuery(svc_account=key_json, project=project_id)

Running Queries

Pass values as query parameters instead of formatting them into the SQL string, so they can't change the query (SQL injection):

from google.cloud import bigquery

job = bq.execute(
    "SELECT * FROM `project_id.dataset_id.users` WHERE name = @name",
    query_parameters=[bigquery.ScalarQueryParameter("name", "STRING", user_input)],
)
rows = list(job.result())

Documentation

Check out the project documentation

Metadata

Release files for gbq 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gbq 2.0.0
File Size Uploaded
gbq-2.0.0.tar.gz 15.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for gbq 2.0.0
File Interpreter ABI Platform
gbq-2.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 32.0 kB

Release files / gbq-2.0.0.tar.gz

Download URL gbq-2.0.0.tar.gz
Size 15.6 kB
Tags Source
SHA-256 checksum
How to use checksums
f59b660044b1c78da5814a8e4234adae23eb7d964ebba8ad8de595c9cb241e8f
BLAKE2b-256 checksum
How to use checksums
635d5d8120d169c3d34ec7c964424e091599c5ada7061681b0aafbd5392f1a1b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 18, 2026.

Transparency log

Release files / gbq-2.0.0-py3-none-any.whl

Download URL gbq-2.0.0-py3-none-any.whl
Size 16.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9bfd978cf1bc88b84526936ac790ce12bce7c42ad3bc4b2074d90e371363295e
BLAKE2b-256 checksum
How to use checksums
4cb45f7c7f3b4634fa37bfe3d6d0e5091e97845012a9e062d7014cd827bba4dd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 18, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files

1.1.0

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page