Text-to-SQL Agent with Execution-Guided Self-Correction

Can a small local language model answer questions over a database it has never seen, and does it help to run the query and show the model what went wrong? This project builds a text-to-SQL agent on the LangChain SQL toolkit: it lists the tables, reads the schema with sample rows, writes SQL, passes a read-only guardrail and executes the query; on an error or an empty result it receives the error text plus a schema or value hint and corrects itself (at most 4 attempts). The chat model is a LiteLLM router (qwen2.5-coder:1.5b (fallback qwen2.5:3b)) served by Ollama on a second laptop (RTX 2050 4 GB) over Wi-Fi; the code ran on a laptop with RTX 2050 4 GB (Ryzen 7, 8 GB RAM).

NOTE: Research / teaching demo: read-only SELECT queries on the public Spider SQLite databases; the agent never sees a connection string and no write tool exists.

Category: Department of Computer Science and Engineering / Agent Development

Agent Development Project AG13 from ProTechSC Academy, with graphical abstract, video demonstration and project guidance.