Skip to main content

A CLI SQL agent that converts natural language to optimized BigQuery SQL using GPT-4

Project description

๐Ÿš€ Datanaut โ€” BigQuery SQL Agent

PyPI version Python 3.10+ License: MIT

A CLI agent that converts plain English questions into optimized BigQuery SQL using GPT-4. Built for data teams who want to query BigQuery without writing SQL.

  โœฆ ยท โœง ยท โœฆ ยท โœง ยท โœฆ ยท โœง ยท โœฆ ยท โœง ยท โœฆ ยท โœง ยท โœฆ ยท โœง ยท โœฆ ยท โœง

  DATANAUT  >>  SQL Explorer v1.0
  Navigating the BigQuery galaxy...

  โœฆ ยท โœง ยท โœฆ ยท โœง ยท โœฆ ยท โœง ยท โœฆ ยท โœง ยท โœฆ ยท โœง ยท โœฆ ยท โœง ยท โœฆ ยท โœง

Installation

pip install datanaut

Quick start

# 1. Scaffold config in your project folder
datanaut init

# 2. Fill in your keys in .env
#    OPENAI_API_KEY=sk-...
#    BIGQUERY_PROJECT_ID=your-project

# 3. Edit schema.json with your BigQuery tables / table functions

# 4. Authenticate with GCP
gcloud auth application-default login

# 5. Launch
datanaut

Usage

datanaut                              # interactive REPL
datanaut ask "total revenue today"    # one-shot query
datanaut ask "..." --dry-run          # validate SQL, skip execution
datanaut test                         # run accuracy test suite
datanaut init                         # scaffold .env + schema.json
datanaut --version

Interactive REPL example

  ๐Ÿง‘โ€๐Ÿš€  You: show me total revenue by region last month

  ๐Ÿ—บ๏ธ   Retrieving relevant schema...
  ๐Ÿค–   Generating SQL with GPT-4...
  โš—๏ธ   Running BigQuery dry run...

  โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—
  โ•‘  >>  MISSION QUERY                           โ•‘
  โ• โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ฃ
  โ•‘  WITH last_month AS (                        โ•‘
  โ•‘    SELECT region,                            โ•‘
  โ•‘           SUM(total_revenue) AS revenue      โ•‘
  โ•‘    FROM `project.dataset.orders`             โ•‘
  โ•‘    WHERE order_date >= ...                   โ•‘
  โ•‘  )                                           โ•‘
  โ•‘  SELECT * FROM last_month                    โ•‘
  โ•‘  ORDER BY revenue DESC LIMIT 1000            โ•‘
  โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

  โœ…  Estimated scan: 84.2 MB
  ๐Ÿš€  Launch this query? (y/n):

Configuration

.env

OPENAI_API_KEY=sk-...
BIGQUERY_PROJECT_ID=your-gcp-project-id
BQ_COST_THRESHOLD_BYTES=5368709120   # 5 GB โ€” queries above this are rejected
CACHE_ENABLED=true
CACHE_TTL_SECONDS=3600
GOOGLE_APPLICATION_CREDENTIALS=/path/to/key.json   # optional

schema.json

Describe your BigQuery tables or table functions. The richer the descriptions, the more accurate GPT-4's SQL will be.

[
  {
    "dataset": "your_dataset",
    "table_name": "orders",
    "description": "Customer orders with revenue and status",
    "partition_column": "order_date",
    "tags": ["sales", "revenue", "orders"],
    "columns": [
      { "name": "order_id",      "type": "STRING",  "description": "Unique order ID" },
      { "name": "order_date",    "type": "DATE",    "description": "Order date โ€” partition column" },
      { "name": "total_revenue", "type": "FLOAT64", "description": "Revenue in USD" }
    ]
  }
]

For BigQuery table functions (TVFs), use:

{
  "function_name": "healing_sessions",
  "description": "Session data filtered by date range",
  "call_syntax": "`project`.`dataset`.healing_sessions(start_date_param, end_date_param)",
  "parameters": [
    { "name": "start_date_param", "type": "DATE" },
    { "name": "end_date_param",   "type": "DATE" }
  ],
  "columns": [ ... ]
}

GCP Authentication

# Option A โ€” personal credentials (easiest)
gcloud auth application-default login

# Option B โ€” service account key
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/key.json

Features

  • GPT-4 with function calling for structured, reliable SQL output
  • BigQuery dry run before every execution โ€” validates SQL and estimates cost
  • Configurable cost threshold โ€” rejects expensive queries automatically
  • Self-correcting retry loop โ€” passes BigQuery errors back to GPT-4
  • In-memory result cache with TTL (Redis-ready interface)
  • Multi-turn conversation memory โ€” refine queries across turns
  • Keyword-based schema retrieval (swap for vector search in production)
  • Batch test runner for accuracy regression testing

License

MIT

Project details


Download files

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

Source Distribution

datanaut-0.1.1.tar.gz (24.5 kB view details)

Uploaded Source

Built Distribution

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

datanaut-0.1.1-py3-none-any.whl (24.2 kB view details)

Uploaded Python 3

File details

Details for the file datanaut-0.1.1.tar.gz.

File metadata

  • Download URL: datanaut-0.1.1.tar.gz
  • Upload date:
  • Size: 24.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for datanaut-0.1.1.tar.gz
Algorithm Hash digest
SHA256 6f7075ce8945c1555c11a92e2f8eee514febb1dc2077a4224ccf149f1b19cfe7
MD5 58bb3d4f657d237980f7b01abcf6ec88
BLAKE2b-256 831214eac963ad97a02fece7fabbc2a972c7249f1064f02fabb433553e2b1fb7

See more details on using hashes here.

File details

Details for the file datanaut-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: datanaut-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 24.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for datanaut-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c7f28dcf2fba92cf8077ff90f3945f65c56cdd061e744c823266b493306eb7c5
MD5 e4cf5ce51da1e7e294a6a04f84621cb2
BLAKE2b-256 2fe25b58d57fe2e0383cb74f2aa5a56bd7551cf41b3035220a8d300fc409bf75

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page