Leveraging Code to Improve In-Context Learning for Semantic Parsing

Ben Bogin, Shivanshu Gupta, Peter Clark, Ashish Sabharwal

Abstract

In-context learning (ICL) is an appealing approach for semantic parsing due to its few-shot nature and improved generalization. However, learning to parse to rare domain-specific languages (DSLs) from just a few demonstrations is challenging, limiting the performance of even the most capable LLMs. In this work, we show how pre-existing coding abilities of LLMs can be leveraged for semantic parsing by (1) using general-purpose programming languages such as Python instead of DSLs and (2) augmenting prompts with a structured domain description that includes, e.g., the available classes and functions. We show that both these changes significantly improve accuracy across three popular datasets; combined, they lead to dramatic improvements (e.g., 7.9% to 66.5% on SMCalFlow compositional split) and can substantially improve compositional generalization, nearly closing the performance gap between easier i.i.d. and harder compositional splits. Finally, comparisons across multiple PLs and DSL variations suggest that the similarity of a target language to general-purpose code is more important than prevalence in pretraining corpora. Our findings provide an improved methodology for building semantic parsers in the modern context of ICL with LLMs.

Introduction

Semantic parsing—the task of translating natural language (e.g., "What is the longest river in the US?") into a structured format a computer can execute—is a cornerstone of virtual assistants. Traditionally, this has relied on Domain-Specific Languages (DSLs) like FunQL or $\lambda$-DCS.

However, DSLs present a major hurdle for modern Large Language Models (LLMs) using in-context learning (ICL). Because these languages are rare in the models' training data, the LLM has to "learn" a whole new syntax from just a handful of examples in a prompt. This often leads to failures in compositional generalization, where a model understands individual components but fails to combine them into complex, novel structures.

Methodology

The authors propose two key shifts in how we prompt LLMs for semantic parsing:

  1. Targeting General-Purpose Code: Instead of asking the model to output a niche DSL, they map the task to popular programming languages (PLs) like Python. Since LLMs are pre-trained on massive amounts of code, they already "know" how to use standard operators like max(), len(), and list comprehensions.
  2. Domain Descriptions (DD): They augment the prompt with a structured "header" that defines the available classes, methods, and type signatures for the specific task. This mimics a common coding pattern: defining an API before using it.
Comparison of semantic parsing using a Domain-Specific Language (DSL) versus Python with Domain Descriptions (DD).

The team also built executable Python environments for classic benchmarks to ensure they could measure execution-based accuracy rather than just string matching. Allowing the model to write multi-line code with intermediate variables significantly boosted performance, as it aligns with how humans write clean code.

Key Results

The study evaluated three complex semantic parsing benchmarks: GeoQuery, Overnight, and SMCalFlow. They tested these on both standard (i.i.d.) splits and "hard" compositional splits designed to test if the model can handle unseen combinations.

Accuracy across three benchmarks as a function of the number of demonstrations.

The improvements were dramatic across the board:

  • GeoQuery (Length split): Accuracy jumped from 31.0% (standard DSL) to 80.7% (Python + DD).
  • SMCalFlow: On the difficult compositional split, performance soared from a measly 7.9% to 66.5%.
  • Efficiency: In many cases, a single demonstration using Python + DD outperformed a model given 25 demonstrations in a traditional DSL.
  • Generalization: By using Python and Domain Descriptions, the researchers almost entirely closed the performance gap between easy and hard dataset splits.

Discussion and Future Work

Surprisingly, the "popularity" of a language in the training data isn't the only factor. Scala, which is much rarer than Python, performed nearly as well. The key is that the language looks like general-purpose code, allowing the model to apply its broad understanding of programming logic.

While the results are impressive, the authors note that creating executable Python environments and Domain Descriptions for new tasks still requires manual engineering effort. Future directions include exploring automated ways to generate Domain Descriptions and investigating how to design "LLM-amenable" programming environments from the ground up to make semantic parsing even more seamless.

Summary updated

BibTeX
@inproceedings{bogin2024leveraging,
  author = {Ben Bogin and Shivanshu Gupta and Peter Clark and Ashish Sabharwal},
  title = {{Leveraging Code to Improve In-Context Learning for Semantic Parsing}},
  booktitle = {Conference of the North American Chapter of the Association for Computational Linguistics},
  year = {2024},
  url = {https://openreview.net/forum?id=Fm8oqe2Kt2},
}
Figure