Defog / SQLCoder: Self-Hosted Text-to-SQL Models
What it is: SQLCoder is Defog.ai's family of open-weights text-to-SQL models published on Hugging Face β LLMs fine-tuned specifically to turn natural-language questions plus a schema into SQL. Defog sells a commercial platform (agents, UI, deployment) built around the same idea, aimed at teams whose data can't leave their infrastructure.
Quick answer: SQLCoder models are open-weights, SQL-specialized LLMs from Defog you can download from Hugging Face (e.g. defog/sqlcoder-7b-2) and run entirely on your own hardware β questions, schema, and data never leave your network. At release, Defog reported them beating much larger general models on the sql-eval benchmark. Defog's commercial platform adds agents, a UI, and fine-tuning on your schema.
Why self-host text-to-SQL at all?
Because a text-to-SQL prompt necessarily contains your schema β table names, columns, sometimes sample values β and results may contain regulated data. For healthcare, finance, or defense teams, sending that to a third-party API is a non-starter. A self-hosted model keeps everything inside your VPC, works in air-gapped environments, and has zero per-query API cost. The trade-off: you manage GPUs and model updates yourself.
How do you run SQLCoder from Hugging Face?
Download the weights and serve them with transformers, vLLM, or Ollama, then prompt with your question, DDL, and instructions in the format Defog documents in the repo. The 7B model runs on a single consumer GPU or Apple Silicon; larger variants (15B, 70B) trade hardware for accuracy. Model sizes and licenses vary β some are Apache-2.0, some CC-BY-SA β so check the model card for the version you deploy.
# quickest local test
ollama run sqlcoder
# or with transformers
from transformers import AutoModelForCausalLM, AutoTokenizer
tok = AutoTokenizer.from_pretrained("defog/sqlcoder-7b-2")
model = AutoModelForCausalLM.from_pretrained("defog/sqlcoder-7b-2",
device_map="auto")
prompt = f"""### Task
Generate a SQL query to answer: {question}
### Database Schema
{ddl}
### Answer
"""
Can you fine-tune it on your own schema?
Yes β and that's where self-hosting compounds. Because the weights are open, you can fine-tune (full or LoRA) on questionβSQL pairs from your own warehouse, teaching the model your naming conventions, business definitions, and dialect quirks. Defog's platform productizes this loop commercially. Even without fine-tuning, most accuracy gains come from prompt-side work: clean DDL, column comments, and a few in-prompt examples covering your common query shapes.
Defog/SQLCoder vs Vanna vs Wren AI: which approach fits?
They solve the problem at different layers. SQLCoder is the model: bring your own retrieval, UI, and execution. Vanna AI is a framework: RAG over your schema and golden queries, using any LLM β including a self-hosted SQLCoder as the generator, a genuinely strong combination. Wren AI is a product: a full UI and semantic layer for business users. Maximum-control, air-gapped teams start with SQLCoder; most others start with Vanna or Wren and swap in local models later.
How accurate are self-hosted text-to-SQL models?
Honest expectations: on standard benchmarks the SQLCoder releases outperformed the general-purpose models of their day at a fraction of the size, but frontier API models have since improved, and any static benchmark number ages quickly. On your schema, retrieval quality and examples matter more than raw model choice. Whatever you deploy, show the generated SQL to users and treat it as a reviewed draft, not an oracle. Build a small eval set of 30β50 real questions with known-correct SQL from your warehouse and rerun it whenever you change models or prompts β it turns "feels more accurate" into a measurable number.
Author & links
Author: Defog.ai
Repo: github.com/defog-ai/sqlcoder Β· huggingface.co/defog
License: Apache-2.0 / CC-BY-SA-4.0 depending on model β check each model card
Related skills
Pair a local SQLCoder with the RAG training loop of Vanna AI, or compare the full-UI approach of Wren AI.
β Back to Data Agents