API-Assisted Code Generation for Question Answering on Varied Table Structures

Yihan Cao, Shuyi Chen, Ryan Liu, Zhiruo Wang, Daniel Fried

Abstract

Table Question Answering (TableQA) has traditionally struggled with complex, non-relational table structures such as hierarchical headers and multi-table schemas. This paper presents a unified framework developed by researchers at Carnegie Mellon University that leverages Large Language Models (LLMs) to generate Python code for data retrieval. By representing tables as Pandas multi-index objects and providing the LLM with specialized Assistant APIs, the framework achieves significant performance improvements, including a 24.5% absolute gain on hierarchical table benchmarks.

Introduction

The goal of Table Question Answering (TableQA) is to enable users to query tabular data using natural language. While simple relational tables are well-handled by existing SQL-based methods, real-world data often exists in "messy" formats: hierarchical financial reports, nested statistical tables, or distributed relational databases. Traditional systems often rely on brittle, domain-specific logical forms that fail when encountering structural variety. This research addresses the need for a unified representation capable of handling diverse table structures without architectural redesign.

Methodology: A Unified Framework

The proposed framework utilizes a three-stage pipeline to bridge the gap between natural language questions and complex tabular data.

1. Multi-index Parsing

Instead of flattening tables into plain text—a process that destroys structural context—the framework parses tables into Pandas multi-index DataFrames. This approach preserves parent-child relationships within hierarchical headers by representing them as nested tuples in a tree structure.

Transformation of a hierarchical table into a multi-index object via a tree structure.

2. Few-Shot Code Generation and Assistant APIs

The system prompts an LLM (e.g., Codex or StarCoder) to translate the user's question into a Python program. LLMs are uniquely suited for this task due to their extensive training on programming languages like Python and libraries like Pandas.

To handle complex reasoning beyond standard data manipulation, the framework introduces Assistant APIs:

  • Operation APIs: Functions like compare_larger() for qualitative comparisons.
  • QA APIs: "Callback" functions that allow the LLM to resolve external knowledge queries (e.g., geographic facts) not present in the table.
End-to-end flow of the API-assisted code generation framework.

Experimental Evaluation

The framework was evaluated across four distinct datasets: WikiTQ (standard relational), HiTab and AIT-QA (hierarchical matrix tables), and Spider (multi-table relational queries).

Key Results

The framework demonstrated state-of-the-art (SOTA) performance across all complex benchmarks:

  • HiTab: 69.3% accuracy (a 24.5% absolute improvement over previous SOTA).
  • AIT-QA: 78.0% accuracy (a 26.2% improvement).
  • Spider: 2.3% improvement over previous SOTA, confirming effectiveness for multi-table relational queries.
  • Open-Source Viability: Even using the smaller StarCoder (15B) model, the framework provided significant boosts when Assistant APIs were included.

Discussion and Insights

The success of this approach highlights Python as a universal interlingua for TableQA. By tapping into the pre-existing programming knowledge of LLMs, the system avoids the need for custom logical forms. Ablation studies confirmed that multi-indexing is essential; flattening hierarchical tables led to significant performance drops as semantic links between nested headers were lost. Furthermore, the modular API design allows for easy extensibility—new capabilities can be added by defining simple helper functions rather than retraining the model.

Limitations and Future Directions

Currently, the Assistant APIs must be manually defined for specific datasets. Future research will focus on automating API discovery, creating an interactive framework where the system identifies its own reasoning gaps and constructs necessary helper functions dynamically. Additionally, improving "fuzzy" matching between natural language questions and technical table headers remains a priority.

Summary updated

BibTeX
@inproceedings{cao2023api,
  author = {Yihan Cao and Shuyi Chen and Ryan Liu and Zhiruo Wang and Daniel Fried},
  title = {{API-Assisted Code Generation for Question Answering on Varied Table Structures}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2023},
  doi = {10.18653/v1/2023.emnlp-main.897},
}
Figure