|
|
ADK with BigQuery as a Tool
Author: Venkata Sudhakar
BigQuery is the Google Cloud enterprise data warehouse holding petabytes of business data - sales history, customer records, product analytics, financial reports. By wrapping BigQuery queries as ADK tools, you give analysts a natural language interface. Instead of writing SQL, an analyst asks "What were our top 5 products last quarter?" and the agent translates the question to SQL, runs it on BigQuery, and returns a formatted answer. Natural language to SQL powered by ADK is one of the highest-value enterprise AI applications on GCP. The pattern: write Python tools that accept business-language parameters and internally construct and execute BigQuery SQL. The agent sees clean function signatures and decides which tool to call. Keep tools focused on specific business domains - a sales tool, an inventory tool, a customer tool - rather than one generic SQL tool that lets the agent write arbitrary SQL, which creates security and injection risks. Whitelist all user inputs against known values before building SQL strings. The below example builds a sales analytics agent that queries BigQuery to answer questions about revenue, top products, and regional performance in natural language - no SQL knowledge needed from the analyst.
Running natural language analytics queries answered from BigQuery,
It gives the following output with real BigQuery data in natural language,
[Tool: get_top_products(metric="revenue", period="this_quarter", limit=5)]
Top 5 Products by Revenue - Q2 2025:
1. Samsung 65-inch QLED TV Rs 2,34,50,000
2. iPhone 15 Pro Rs 1,87,20,000
3. Dell XPS 15 Laptop Rs 1,42,80,000
4. Sony WH-1000XM5 Headphones Rs 68,40,000
5. iPad Air M2 Rs 52,10,000
[Tool: get_regional_breakdown(region="South", period="this_month")]
South Region - April 2025:
Electronics: Rs 1,24,50,000 (847 units)
Appliances: Rs 68,20,000 (312 units)
Accessories: Rs 18,70,000 (1,243 units)
[Tool: get_category_trend(category="Electronics", months=6)]
Electronics Revenue Trend:
Nov: Rs 2.8Cr | Dec: Rs 4.1Cr | Jan: Rs 1.9Cr
Feb: Rs 1.7Cr | Mar: Rs 2.4Cr | Apr: Rs 2.9Cr
TREND: Recovering after Jan-Feb dip. April 20% above March - positive momentum.
# All queries used parameterised BigQuery SQL - no injection risk
# Analyst needed zero SQL knowledge to get all three answers
BigQuery tool security: always use parameterised queries (QueryJobConfig with query_parameters) for any user-supplied values like region names, category names, or date ranges - never concatenate user input directly into SQL. Grant the Agent Engine service account BigQuery Data Viewer on the analytics dataset only - never Data Editor. Point tools at BigQuery views that mask PII rather than raw tables with customer data. Log every BigQuery job_id alongside the session_id so you can trace exactly what SQL ran for any given conversation turn. For cost control, set a maximum bytes billed limit in QueryJobConfig to prevent accidental full table scans.
|
|