Agentic NL-to-SQL Orchestration

NL2SQL Bot

5-agent orchestration on Google ADK converting natural language to validated SQL with loop-guarded retry, per-agent model routing, and automated 6-type chart generation across 3 SQL dialects.

Google ADK 5-Agent Pipeline 18 SQL Security Patterns 3 SQL Dialects

The Problem

Business users need answers from databases but can't write SQL. Existing chatbots either generate unsafe queries or produce results that are hard to interpret. NL2SQL Bot bridges this gap with a 5-agent pipeline that understands intent, generates validated SQL, and renders interactive visualizations — all in one conversation.

Security is non-negotiable: every query passes through 18 blocked SQL patterns (DELETE, DROP, INSERT, UPDATE, TRUNCATE, ALTER, etc.) before execution, with table allowlisting and dialect-aware validation ensuring cross-dialect portability across MySQL, PostgreSQL, and SQLite.

The system uses Google ADK (Agent Development Kit) to orchestrate 5 specialized agents with session state passing, loop-guarded retry (max 4 SQL regeneration attempts per query), and per-agent model routing via environment variable overrides for optimal cost-performance tuning.

Technical Highlights

  • 5-Agent Orchestration

    Root orchestrator, SQL task agent, SQL generator, plot config agent, and result interpreter — each with per-agent model routing via environment variable overrides for cost-performance tuning.

  • Security-First Execution

    Read-only execution enforced through 18 blocked SQL patterns, table allowlisting, and abstracted dialect rules. No DROP, DELETE, or UPDATE ever reaches the database.

  • 6-Type Chart Generation

    Results rendered as interactive Plotly.js visualizations: table, column, horizontal bar, line, pie, or none — selected automatically by the plot config agent based on query structure and data shape.

  • Loop-Guarded Retry

    When SQL validation fails, the system retries with corrective context — but with hard iteration limits to prevent infinite loops and cost runaway.

Under The Hood

Agent Pipeline

Google ADK orchestrates a linear pipeline: Intent Classification → SQL Generation → Validation → Execution → Visualization. Each agent receives structured context from the previous stage via session state, enabling precise handoffs.

Dialect Abstraction

The SQL generator produces dialect-aware queries using abstracted rules for MySQL, PostgreSQL, and SQLite. Schema introspection happens at startup, and table metadata is injected into the generation prompt for accurate column references.

Streaming Results

FastAPI endpoints stream SQL, reasoning traces, and chart-ready JSON as they're produced. The frontend receives real-time updates showing the bot's thought process alongside the final visualization.

Product Demo

Screenshots and interactive demo coming soon