Getting the Most Out of Open-Source Models and Cooking Up Text2SQL

Dzhal Antonov20 Jan 2026

Problem

Business users at a large retailer keep asking analysts for small data pulls ("gum sales for yesterday"), and analysts become a bottleneck. X5 Tech built a chat bot that turns a question into SQL, runs it in the warehouse and answers within 15 seconds. The first version worked on one data mart. Adding a table took about two weeks of analyst work, the prompt grew to 20,000 tokens, the model invented columns, and complex queries reached only about 70% quality.

Example from the article: "Sales in Pyaterochka in Rostov" returned nothing because the model wrote city = 'Rostov' while the column stores "Rostov-na-Donu".

Solution in brief

The pipeline rephrases the dialogue into one question, filters out non-data questions, asks for missing parameters and generates SQL with up to three retries. A Value Retriever matches filter values against real column values, and a Column Retriever puts only relevant columns into the prompt. Quality is tracked offline with execution accuracy, an LLM judge and an error taxonomy.

Questions this case answers
  • How to let non-technical users query structured data in natural language with an answer in seconds and without hallucinated columns or values
  • How to keep an LLM prompt small and accurate when the schema has hundreds of fields and the examples must match the structure of the question
  • How to let business users get data from a warehouse in a chat without routing every question through an analyst?
  • How to stop a text-to-SQL service from inventing columns and filter values that do not exist in the table?

Tools and methods

  • Qwen3 32B — the generation model; bigger models gave no big gain
  • Value Retriever — maps question entities to real column values or asks the user
  • Column Retriever — vector search that keeps only relevant columns in the prompt
  • Masked few-shot retrieval — finds examples by question structure, not shared city names
  • Execution Accuracy, AST similarity, LLM as a judge — offline metrics plus an error taxonomy
Continue reading — it's free

Google or a one-time email code. No password.