NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Code-Style In-Context Learning for Knowledge-Based Question Answering

Zhijie Nie, Richong Zhang, Zhongyuan Wang, Xudong Liu

Abstract

Knowledge-Based Question Answering (KBQA) involves translating natural language questions into structured logical forms for database execution. Despite their power, Large Language Models (LLMs) often fail at this task due to the "unfamiliar grammar" of symbolic logic, resulting in high Format Error Rates (FER). We introduce KB-Coder, a training-free framework that reformulates KBQA as a code-generation task. By prompting LLMs to generate sequences of Python function calls—a format they are highly proficient in—KB-Coder drastically improves accuracy and generalization. Our method achieves new state-of-the-art results on GrailQA, WebQSP, and GraphQ, reducing FER from 20% to 3% in few-shot settings.

Introduction: The Syntax Barrier in KBQA

The primary goal of KBQA is to bridge the gap between human language and machine-readable queries like SPARQL or S-Expressions. While In-Context Learning (ICL) allows LLMs to perform tasks with minimal examples, it falters in KBQA for two main reasons. First, LLMs are "code-native"; they are trained on massive corpora of Python, Java, and C++, but rarely encounter the niche, rigid grammars of symbolic logic. This leads to frequent syntax errors. Second, zero-shot generalization is difficult because LLMs cannot easily guess specific database schema relations (e.g., location.country.languages_spoken) without external guidance.

Comparison of standard ICL versus the proposed code-style ICL method.

As illustrated in the comparison above, standard ICL methods force the LLM to work with unfamiliar logic forms, leading to high error rates. KB-Coder shifts the paradigm by using a "familiar grammar"—Python—to represent logical operations, leveraging the LLM's pre-existing fluency in programming.

Methodology: KB-Coder Framework

KB-Coder operates by defining a set of seven Python "meta-functions" (such as START, JOIN, AND, and COUNT) that mirror the atomic operations of a KBQA query. The framework follows a multi-stage pipeline to transform a question into an answer.

1. Code-Style Prompting

Instead of raw logical forms, the LLM is provided with Python implementations of the meta-functions and a few "demo examples" mapping questions to Python code blocks. This "code-style" prompt aligns the task with the LLM's pre-training distribution.

2. Retrieval-Augmented Generation

To address the challenge of unfamiliar schema relations, KB-Coder employs a dense retriever. For a given question, it identifies the most relevant relations within the Knowledge Base (KB) and includes them as "hints" in the prompt. This provides a semantic anchor for the LLM to generate the correct function calls.

3. Execution and Entity Linking

The LLM generates a sequence of Python calls. Since the LLM might generate natural language "mentions" instead of exact KB identifiers, a post-processing step uses SimCSE embeddings and Faiss indexing to link these mentions to actual entities and relations in the KB.

The end-to-end workflow of the KB-Coder framework.

4. Interpretation

Finally, a Python interpreter executes the generated code to produce a valid S-Expression. This is then converted to SPARQL and executed against the KB to retrieve the final answer.

Experimental Results

We evaluated KB-Coder on three major benchmarks: WebQSP (i.i.d. questions), GrailQA (testing compositional and zero-shot generalization), and GraphQ (complex queries).

Key Findings:

  • Drastic Error Reduction: On GrailQA, KB-Coder reduced the Format Error Rate (FER) from 20.0% to just 3.0% in a 1-shot setting compared to the previous state-of-the-art, KB-BINDER.
  • Superior Generalization: The retrieval-augmentation strategy boosted zero-shot F1 scores on GrailQA by nearly 10 points. By providing a single relevant relation hint, the model could navigate entirely new domains effectively.
  • Outperforming Supervised Models: Remarkably, the training-free KB-Coder outperformed several fully supervised models on WebQSP and GraphQ, demonstrating the efficiency of well-structured in-context learning.

Discussion and Future Work

The success of KB-Coder highlights that LLM failures in symbolic reasoning are often failures of syntax rather than logic. By wrapping complex logic in the familiar "skin" of Python, we unlock the latent reasoning capabilities of these models.

However, KB-Coder is sensitive to the choice of demo examples, showing higher performance variance than some baselines. Future research will focus on automated, robust example selection and extending the meta-function library to handle increasingly complex, multi-hop queries in web-scale knowledge bases.

Summary updated

BibTeX
@inproceedings{nie2024code,
  author = {Zhijie Nie and Richong Zhang and Zhongyuan Wang and Xudong Liu},
  title = {{Code-Style In-Context Learning for Knowledge-Based Question Answering}},
  booktitle = {AAAI Conference on Artificial Intelligence},
  year = {2024},
  doi = {10.1609/aaai.v38i17.29848},
}
Figure