Spaces:
Sleeping
Download README.md from Akshayaitis/sql-debug-env: direct link, hf CLI and curl.
- Browser
- Download file 6.69 kB
-
https://huggingface.co/spaces/Akshayaitis/sql-debug-env/resolve/main/README.md
- Command line
-
hf download hf://spaces/Akshayaitis/sql-debug-env/README.md
-
curl -L -o README.md https://huggingface.co/spaces/Akshayaitis/sql-debug-env/resolve/main/README.md
title: SQL Debug Env
emoji: π οΈ
colorFrom: blue
colorTo: purple
sdk: docker
app_port: 7860
pinned: false
sql-debug-env
Teaching AI agents to debug SQL the way we all had to learn β by staring at cryptic error messages and wondering why our JOINs return 47x more rows than expected.
What is this?
An OpenEnv environment for reinforcement learning. The agent gets a bugged SQL query, a database schema, and some sample data. It tries to fix the query, gets real feedback (actual error messages, result previews, row counts), and iterates until it either nails it or runs out of steps.
Think of it like a SQL homework grader, but the student is an LLM.
Why SQL?
Honestly, debugging SQL felt like a natural fit for this kind of iterative RL setup:
- Deterministic grading β run the query against SQLite, compare results mathematically. No fuzzy evaluation needed.
- Everyone encounters it β data engineers, backend devs, analysts... we've all been there.
- Genuine feedback loops β not a one-shot problem. The agent can read error messages and actually learn from them mid-episode.
- Rich partial credit β an almost-right query that returns 90% of the correct rows isn't the same as a completely wrong one.
Tasks
We have 3 tasks right now, ordered by difficulty:
| ID | What it does | Difficulty |
|---|---|---|
fix_syntax |
Fix misspelled keywords, wrong punctuation, etc. | Easy |
fix_join |
Fix JOIN that runs fine but gives wrong results (the sneaky kind) | Medium |
optimize_query |
Rewrite a correlated subquery that's technically correct but painfully slow | Hard |
How the agent interacts
Action space
Pretty simple β the agent just submits a SQL string:
{ "query": "SELECT name, salary FROM employees WHERE department = 'Engineering' ORDER BY salary DESC" }
What it gets back (observation)
| Field | Type | What it is |
|---|---|---|
task_id |
string |
Which task we're on |
broken_query |
string |
The original bugged query |
schema_description |
string |
Human-readable schema info |
error_message |
string? |
SQL error from last attempt (if any) |
result_preview |
list[dict]? |
First 10 rows of what the query returned |
expected_row_count |
int |
How many rows the correct answer has |
current_row_count |
int? |
How many rows the agent's query returned |
step_number |
int |
Where we are in the episode |
max_steps |
int |
Hard cap (8 steps) |
hint |
string? |
We give hints at certain steps if the agent is struggling |
Scoring
Reward is β [0, 1], broken into 4 components:
| Component | Weight | What earns it |
|---|---|---|
| Syntax valid | 0.15 | Query parses without blowing up |
| Row-level F1 | 0.55 | How many correct rows did it get? (precision Γ recall) |
| Exact match | 0.20 | Bonus for getting it 100% right |
| Efficiency | 0.10 | Task 3 only β did it actually get rid of the correlated subquery? |
Episode ends when the agent hits β₯0.89 total reward or exhausts all 8 steps.
Database
Task 1 (employees)
employees(id, name, department, salary, hire_date)
departments(id, name, budget, manager_id)
Tasks 2 & 3 (e-commerce)
users(user_id, name, email, region)
orders(order_id, user_id, created_at, status)
order_items(item_id, order_id, product_id, quantity, price)
products(product_id, name, category, unit_price)
Getting started
Prerequisites
- Python 3.10+
- Docker (if you want to deploy it properly)
Running locally
# install everything
pip install -e ".[server,inference,dev]"
# fire up the server
python -m uvicorn server.app:app --host 0.0.0.0 --port 7860
# in another terminal β run the baseline agent
export API_BASE_URL="https://integrate.api.nvidia.com/v1" # or any OpenAI-compatible endpoint
export MODEL_NAME="meta/llama-3.3-70b-instruct" # NVIDIA NIM model name
export HF_TOKEN="your-nvidia-or-hf-api-key-here" # required β no default
export ENV_URL="http://localhost:7860"
python inference.py
Docker
docker build -t sql-debug-env -f server/Dockerfile .
docker run -p 7860:7860 sql-debug-env
# quick sanity check
curl http://localhost:7860/health
Using the Python client
from sql_debug_env import SQLDebugEnv, SQLAction
with SQLDebugEnv(base_url="http://localhost:7860").sync() as env:
result = env.reset(task_id="fix_syntax")
print(result.observation.error_message) # see what's wrong
result = env.step(SQLAction(query="SELECT name, salary FROM employees WHERE department = 'Engineering' ORDER BY salary DESC"))
print(f"reward={result.reward}, done={result.done}")
REST API (if you prefer curl)
curl -s -X POST http://localhost:7860/reset \
-H "Content-Type: application/json" \
-d '{"task_id": "fix_syntax"}' | python -m json.tool
Project layout
sql-debug-env/
βββ openenv.yaml # OpenEnv manifest
βββ README.md
βββ inference.py # Baseline LLM agent
βββ pyproject.toml
βββ sql_debug_env/ # Client-side package
β βββ __init__.py
β βββ models.py # Action / Observation / State types
β βββ client.py # WebSocket client
βββ server/
βββ Dockerfile
βββ requirements.txt
βββ app.py # FastAPI + OpenEnv wiring
βββ environment.py # Core env logic (reset/step/state)
βββ tasks.py # Task definitions & broken queries
βββ grader.py # Reward computation
βββ seed_db.py # Populates the SQLite DB
Baseline scores
Evaluated using NVIDIA NIM (OpenAI-compatible API) with LLaMA 3.3 70B Instruct. Scores are computed as best reward across all steps per task (0.0β1.0 scale):
Scores are computed as best reward across all steps per task (0.0β1.0 scale):
| Model | fix_syntax | fix_join | optimize_query | Avg |
|---|---|---|---|---|
| meta/llama-3.3-70b-instruct (NVIDIA NIM) | 0.90 | 0.90 | 1.00 | 0.93 |
| meta/llama-3.1-70b-instruct (NVIDIA NIM) | 0.90 | 0.90 | 0.90 | 0.90 |
| meta/llama-3.1-8b-instruct (NVIDIA NIM) | 0.90 | 0.15 | 0.90 | 0.65 |
Scores reflect correct queries submitted by the model.
fix_syntaxandfix_joinmax out at 0.90 (no efficiency bonus).optimize_queryearns 1.0 only if the model eliminates the correlated subquery.
License
MIT