When prompting Large Language Models (LLMs) to reason over structured data—such as databases, knowledge graphs, or matrices—our natural instinct is often to translate that data into prose. We write descriptive sentences like "The user table contains an ID column which links to the orders table."
However, a robust body of evidence demonstrates that this prose-centric approach is fundamentally flawed. Instead, formatting structured data as declarative or object-oriented code (such as SQL DDL statements, Python classes, or JSON dictionaries) unlocks significantly higher reasoning accuracy.
To understand why this technique works, we must look at how language models are trained, how they manage structural dependencies, and how they map abstract schemas to concrete operations.
The Pre-training Advantage: Why Code Beats Prose
Why does code syntax outperform natural language for data representation? The answer lies in the nature of LLM pre-training. Modern models are exposed to massive repositories of source code. To predict the next token in a code snippet, a model cannot simply rely on loose semantic associations; it must internalize rigid syntactic constraints, scope boundaries, hierarchical nesting, and strict relational logic.
Prose is inherently permissive and ambiguous. Code, by contrast, is precise and structured. When we present data using a programming syntax, we activate the model's highly optimized code-parsing and logical reasoning circuits. The model treats the data not as a collection of loose sentences, but as a formal system with explicit constraints.
Database Schemas as Declarative Code
In Text-to-SQL and Text-to-GQL (Graph Query Language) tasks, an LLM must map a user's natural language question to a precise query over a database schema. Traditional prompting methods describe the tables and columns in plain text. However, Nan et al. and Gao et al. demonstrate that presenting database structures via SQL Data Definition Language (DDL) statements (such as CREATE TABLE) dramatically improves execution accuracy. DDL provides a native, structured context that mirrors the exact programming environments the models encountered during pre-training, allowing them to interpret schema constraints and data types more accurately.
-- Code-style schema representation (DDL)
CREATE TABLE Users (
user_id INT PRIMARY KEY,
email VARCHAR(255)
);
CREATE TABLE Orders (
order_id INT PRIMARY KEY,
user_id INT,
FOREIGN KEY (user_id) REFERENCES Users(user_id)
);
This code-centric schema prompting strategy helps the model map natural language instructions directly to structured operations, as highlighted by Kerbusch et al.. The benefits extend beyond SQL. When translating natural language to graph queries, Zhou et al. show that representing graph schemas and query skeletons as Python classes reduces ambiguity and enforces strict syntactic constraints, outperforming standard natural language descriptions. Similarly, Wu et al. confirm this advantage in automated data visualization tasks, finding that SQL schemas or Python classes are far more effective than natural language summaries for helping a model parse and utilize tabular relationships.
Preserving Graph Topologies with Object-Oriented Syntax
Graphs and knowledge networks are notoriously difficult to serialize into flat text strings because their relationships are non-linear and multi-directional. If you flatten a graph into a standard sentence, the topological consistency is often lost, and the model struggles to trace multi-hop paths.
To solve this, Madaan et al. introduced an approach that models structured graphs as Python classes or objects. By explicitly defining relationships as object attributes or function calls, the model can leverage programming language syntax to maintain topological consistency, avoiding the information loss common in flat string serialization.
# Object-oriented graph representation
class Node:
def __init__(self, name):
self.name = name
self.neighbors = []
node_A = Node("Airport_A")
node_B = Node("Airport_B")
node_A.neighbors.append(node_B)
This paradigm is highly effective for complex tasks like entity alignment across knowledge graphs. In their work on relational and multimodal entity alignment, Jiang et al. and Jiang et al. translate graph structures into object-oriented Python classes with dedicated member functions for retrieving neighbors and temporal attributes. This allows the LLM to leverage its pre-trained logical reasoning to navigate complex network topologies and graph neighborhoods far better than raw text descriptions.
Furthermore, Wang et al. demonstrate that representing graph structures using code-like syntax (such as programmatic node and edge lists) allows models to process complex connectivity natively, achieving high performance without requiring specialized graph encoders.
Linearizing Tables and Sequences as Code Snippets
The power of code-style representation is not limited to complex databases and graphs; it also applies to simpler data structures like tables and numerical sequences.
When models process tables, standard text delimiters (like commas or tabs) can become confusing over long contexts. Singha et al. demonstrate that representing tabular data as executable data frame initialization code allows LLMs to navigate and transform data more effectively by leveraging their code-completion capabilities. When linearizing tables into text, Deng et al. note that nested bracketed structures—which mimic programming syntax—outperform simple delimited text because they align with the structured data patterns prevalent in training sets.
For generative information extraction, where models must extract entities and relations from raw text into a structured format, Li et al. find that code-style linearization (such as JSON or Python dictionaries) ensures better structural consistency and schema adherence than arbitrary special tokens.
# Code-style linearization for sequential/tabular data
market_data = {
"timestamp": "2023-10-27",
"indices": [
{"name": "SP500", "value": 4117, "change": -0.0048},
{"name": "NASDAQ", "value": 12643, "change": -0.0036}
]
}
This phenomenon even extends to purely numerical data. In a study on generating market commentaries from numerical sequences, Kawarada et al. discovered that formatting numbers as Python dictionaries or nested lists significantly boosts performance over natural language templates or complex markup languages like HTML and LaTeX.
The structured syntax of code acts as a "Goldilocks zone" for the model's attention mechanisms: it provides enough rigid structure for the model to track boundaries and positions perfectly, without the overhead or token inefficiency of verbose natural language.
Key Foundational Work
Text-to-SQL Empowered by Large Language Models: A Benchmark Evaluation (2023) — contributed 2 concept elements to this direction.
Enhancing Text-to-SQL Capabilities of Large Language Models: A Study on Prompt Design Strategies (2023) — contributed 2 concept elements to this direction.
Language Models of Code are Few-Shot Commonsense Learners (2022) — contributed 1 concept elements to this direction.