Prompting GPT-3.5 for Text-to-SQL with De-semanticization and Skeleton Retrieval

Chunxi Guo, Zhiliang Tian, Jintao Tang, Pancheng Wang, Zhihua Wen, Kang Yang, Ting Wang

TL;DR

This paper introduces a novel prompting framework that significantly boosts GPT-3.5's performance on Text-to-SQL tasks. By stripping domain-specific details from natural language questions to create abstract "skeletons," the framework retrieves structurally similar few-shot demonstrations to guide the LLM. Combined with adaptive schema filtering and an execution-based fallback mechanism, this approach outperforms state-of-the-art fine-tuned models on major cross-domain benchmarks.

What Problem Does This Paper Address?

Converting natural language questions (NLQ) into structured SQL queries—known as Text-to-SQL—is crucial for making databases accessible to non-experts. While Large Language Models (LLMs) excel at general text generation, they often stumble on the strict syntactic and semantic requirements of complex SQL commands (like nested clauses, GROUP BY, or JOIN).

A common way to help LLMs is few-shot prompting (providing a few example pairs of questions and their corresponding SQL queries). However, standard retrieval methods look for examples with similar vocabulary. In Text-to-SQL, two questions might use identical words because they query the same database, yet require completely different SQL structures. Conversely, two questions from entirely different domains might share the exact same structural intent (e.g., "Show me the highest-paid employee" vs. "Show me the most expensive book"). The authors address this gap by focusing retrieval on structural intent rather than surface-level text similarity.

Methods

The authors propose a two-module framework: Question De-semanticization and LLM-Based Adjustable Prompting.

Overview of the proposed Text-to-SQL framework, detailing the de-semanticization, retrieval, filtering, and revision pipeline.

1. Question De-semanticization

To uncover the structural "skeleton" of a question, the framework filters out domain-specific information (like specific table names, column names, or cell values) using three parallel strategies:

  • Schema-Related Detection: The question and database schema are concatenated and fed into a language model. By masking tokens one by one and measuring the shift in the resulting vector representations, the model identifies which words are highly correlated with the database domain. Crucially, these representations are projected into a hyperbolic space (Poincaré ball), which is mathematically better suited for capturing hierarchical semantic structures than standard Euclidean space.
  • Token Matching: Explicit lexical and value-based matching identifies direct overlaps between the question text and the database schema items.
  • Part-of-Speech (POS) Tagging: Nouns and numbers are flagged, as they are highly likely to represent specific database entities or values.

Tokens deemed highly domain-specific are replaced with a [MASK] token, leaving behind a clean structural skeleton.

2. LLM-Based Adjustable Prompting

Once the skeleton is extracted, the framework optimizes the prompt via three steps:

  • KNN-Based Skeleton Retrieval: The framework uses cosine similarity to find $k$-nearest neighbor examples in the training set that match the skeleton of the input question, ensuring the LLM sees demonstrations with the exact same query logic.
  • Schema-Relevance Filtering: To prevent the LLM from getting overwhelmed by massive database schemas, a relevance threshold filters out tables and columns unrelated to the current question.
  • Fallback Revision: If the LLM's generated SQL fails database execution, a fallback mechanism triggers, providing the full database schema to the LLM so it can intelligently revise its mistake.

Datasets

The framework was evaluated on three widely recognized cross-domain Text-to-SQL benchmarks:

  1. Spider: A large-scale dataset covering 138 distinct database domains.
  2. Spider-Syn: A challenging variant of Spider where explicit schema terms in the questions are replaced with synonyms.
  3. Spider-DK: An extension of Spider that incorporates complex, artificial domain knowledge to test model adaptability.

Key Insights and Ideas

Comparison of full questions vs. de-semanticized question skeletons for similarity matching.
  • Intent Over Semantics: As illustrated in Figure 1, raw text similarity can be highly deceptive. Question 1 and Question 3 share a high textual similarity score (0.8243) due to shared vocabulary (themes, exhibitions, ticket price), but their SQL structures are completely different. By masking these domain terms, the skeleton similarity correctly pairs Question 1 and Question 2 (0.9724), which both require an ORDER BY ... DESC structure.
  • The Power of Hyperbolic Geometry: Modeling the question-schema relationships in a Poincaré ball allows the framework to capture long-term dependencies and semantic hierarchies far more effectively than traditional Euclidean distance matrices.
  • Prompt Pruning and Fallbacks: LLMs perform better when given a concise, highly relevant schema rather than a massive, noisy database dump. However, because aggressive filtering can occasionally cut out a necessary column, the execution-led fallback loop acts as a vital safety net.

Key Results

The proposed framework achieved outstanding results, outperforming both SOTA full-data fine-tuned models and standard LLM baselines across all three datasets.

On the standard Spider dataset, the framework achieved an Execution Accuracy (EX) of 87.8% and a Test-Suite Accuracy (TS) of 84.8%, comfortably beating the top fine-tuned baseline, RESDSQL-3B (84.1% EX / 73.5% TS), and obliterating zero-shot ChatGPT (70.1% EX).

The framework demonstrated even stronger generalization capabilities on the robust robustness benchmarks:

  • Spider-Syn: Achieved 79.4% EX, compared to RESDSQL-3B's 76.9% and ChatGPT's 58.6%.
  • Spider-DK: Achieved 74.2% EX, outperforming RESDSQL-3B's 66.0% and ChatGPT's 62.6%.

An ablation study confirmed that using Poincaré distance instead of Euclidean distance consistently yielded higher accuracy across the board.

Limitations and Future Work

The authors note that their framework still heavily relies on the underlying LLM's baseline capability to generate code. If the LLM inherently struggles with basic syntax, the framework's performance gains are bottlenecked.

For future work, the researchers plan to explore integrating external knowledge reasoning capabilities and optimizing the framework's efficiency when interacting with exceptionally large enterprise databases.

Summary updated

BibTeX
@inproceedings{guo2023prompting,
  author = {Chunxi Guo and Zhiliang Tian and Jintao Tang and Pancheng Wang and Zhihua Wen and Kang Yang and Ting Wang},
  title = {{Prompting GPT-3.5 for Text-to-SQL with De-semanticization and Skeleton Retrieval}},
  booktitle = {PRICAI},
  year = {2023},
  doi = {10.1007/978-981-99-7022-3\_23},
}
Figure