Skip to main content

pydantic-bq

A Pydantic-based BigQuery client for type-safe schema definition and data operations.

CI PyPI version codecov Python 3.12+

Installation

pip install pydantic-bq

Or with uv:

uv add pydantic-bq

Quick Start

1. Configure Credentials

Create a .env file with your BigQuery credentials:

# Option 1: Base64-encoded service account JSON (recommended)
BIGQUERY_CREDENTIALS=eyJ0eXBlIjoic2VydmljZV9hY2NvdW50Ii...

# Option 2: Individual fields
G_PROJECT_ID=your-project-id
G_CLIENT_EMAIL=service-account@your-project.iam.gserviceaccount.com
G_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----
...your key here...
-----END PRIVATE KEY-----"

2. Define a Model

from datetime import datetime
from typing import Optional

from pydantic_bq import BQBaseModel


class UserEvent(BQBaseModel):
    user_id: int
    event_type: str
    timestamp: datetime
    metadata: Optional[str] = None

    class Meta:
        table_id = 'user_events'
        table_description = 'User activity events'

3. Connect and Query

from pydantic_bq import DatasetClient

# Uses credentials from environment/.env automatically
client = DatasetClient('my_dataset')

# Query rows as Pydantic objects
events = client.table(UserEvent).get_rows(
    where="event_type = 'login'",
    limit=100
)

for event in events:
    print(f"User {event.user_id} logged in at {event.timestamp}")

4. Insert Data

from datetime import datetime

# Create new events
new_events = [
    UserEvent(user_id=1, event_type='signup', timestamp=datetime.now()),
    UserEvent(user_id=2, event_type='login', timestamp=datetime.now()),
]

# Insert into BigQuery
client.table(UserEvent).add_rows(*new_events)

5. Table Management

# Create a table from model schema
client.create_table(UserEvent)

# Delete and recreate (useful for schema changes)
client.recreate_table(UserEvent)

# Delete a table
client.delete_table(UserEvent)

6. Raw SQL Queries

results = client.query("""
    SELECT user_id, COUNT(*) as event_count
    FROM my_dataset.user_events
    GROUP BY user_id
    ORDER BY event_count DESC
    LIMIT 10
""")

for row in results:
    print(f"User {row['user_id']}: {row['event_count']} events")

Authentication

Credentials are loaded from environment variables (or .env file) in this priority:

  1. Base64-encoded JSON (simplest for deployment):

    BIGQUERY_CREDENTIALS=eyJ0eXBlIjoic2VydmljZV9hY2NvdW50Ii...
    

    Generate with:

    cat service-account.json | base64
    
  2. Individual fields (useful for local development):

    G_PROJECT_ID=your-project-id
    G_CLIENT_EMAIL=sa@your-project.iam.gserviceaccount.com
    G_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----
    ...
    -----END PRIVATE KEY-----"
    

API Reference

BQBaseModel

Base class for defining BigQuery table schemas using Pydantic.

class MyTable(BQBaseModel):
    # Define fields with type hints
    name: str
    count: int
    price: float
    is_active: bool
    created_at: datetime
    birth_date: date
    tags: list[str] = []
    description: Optional[str] = None

    class Meta:
        table_id = 'my_table'
        table_description = 'Optional description'

Type mappings:

Python Type BigQuery Type Mode
str STRING REQUIRED
int INTEGER REQUIRED
float FLOAT REQUIRED
bool BOOL REQUIRED
datetime TIMESTAMP REQUIRED
date DATE REQUIRED
Enum STRING REQUIRED
Optional[T] T NULLABLE
list[T] T REPEATED

DatasetClient

Main client for BigQuery operations.

client = DatasetClient('my_dataset')

Methods:

Method Description
table(Model) Get BQTable wrapper for CRUD operations
view(Model) Get BQView wrapper for read-only operations
query(sql) Execute raw SQL, returns list[dict]
add_rows(*objs) Insert model instances (infers table from type)
create_table(Model) Create table from model schema
delete_table(Model) Delete a table
recreate_table(Model) Drop and recreate table

BQTable

Table wrapper with full CRUD support.

table = client.table(UserEvent)

Methods:

Method Description
get_rows(fields=None, where=None, limit=None, as_objects=True) Fetch rows
count_rows(where=None) Count rows
add_rows(*objs, send_as_file=True) Insert rows
delete_rows(where) Delete matching rows
create() Create the table
delete() Delete the table
recreate() Drop and recreate

BQView

View wrapper with read-only operations. Same query methods as BQTable.

Development

# Clone and install
git clone https://github.com/tutorcruncher/pydantic-bq.git
cd pydantic-bq
uv sync --all-extras

# Run tests (mocked)
uv run pytest tests/ -m "not e2e"

# Run E2E tests (requires credentials in .env)
uv run pytest tests/ -m e2e

# Lint and format
uv run ruff check .
uv run ruff format .

License

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydantic_bq-0.1.6.tar.gz (19.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydantic_bq-0.1.6-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_bq-0.1.6.tar.gz.

File metadata

  • Download URL: pydantic_bq-0.1.6.tar.gz
  • Upload date:
  • Size: 19.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for pydantic_bq-0.1.6.tar.gz
Algorithm Hash digest
SHA256 09dcf6e650fc616d2a4ee5da4bab02f2c1bfc300cfb3c82ed1bc5273420beab5
MD5 a1d3d1f39c28929927d95a5f210c781c
BLAKE2b-256 0ea1d4b9f82c782b7dd591d7e41efe75941c7b06106ffc4e2a302e8a4cfd9a11

See more details on using hashes here.

Provenance

The following attestation bundles were made for pydantic_bq-0.1.6.tar.gz:

Publisher: release.yml on tutorcruncher/pydantic-bq

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pydantic_bq-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: pydantic_bq-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for pydantic_bq-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 f7a0b59698fa2c5ec413e1a34a951e2b798700e9dbd68546677c0a1fc8a27724
MD5 7e24289de33ead89ef095a34eb891cc1
BLAKE2b-256 b2cd44b7355a0cdb7ec5e3b2216e9506c058699c9bac572461f1664e04d5a36b

See more details on using hashes here.

Provenance

The following attestation bundles were made for pydantic_bq-0.1.6-py3-none-any.whl:

Publisher: release.yml on tutorcruncher/pydantic-bq

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.1.6 This release

2 files

0.1.5

2 files

0.1.4

2 files

0.1.3

2 files

0.1.0

2 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