Programmatic Schema Representation for Information Extraction

23 concepts17 papers1 proposal

Information Extraction (IE) tasks require transforming unstructured natural language into rigid, structured formats like tables, knowledge graphs, or relational databases. Traditionally, practitioners have prompted Large Language Models (LLMs) using natural language instructions or JSON templates to define target schemas. However, as highlighted in a comprehensive survey by Xu et al., models frequently struggle to adhere to these arbitrary formats, often hallucinating fields, violating structural constraints, or failing to capture nested dependencies.

To solve this, a powerful paradigm has emerged: representing extraction schemas directly as executable code. Instead of treating IE as a text-to-text or text-to-JSON task, this approach reframes it as a code generation or code completion problem. By defining target schemas as Python classes, type hints, and function calls, researchers are unlocking the deep structural reasoning capabilities that LLMs acquire during pre-training on massive source code repositories.

Why Code Works Better Than Natural Language

The core intuition behind programmatic schema representation lies in the difference between the structural priors of natural language and source code. Natural language is inherently ambiguous and permissive. Code, by contrast, enforces strict syntactic rules, explicit type boundaries, and logical constraints.

When an LLM is pre-trained on billions of lines of code, it learns to strictly respect these programmatic boundaries. By wrapping an information extraction task inside a code structure, we force the model to utilize its code-generation faculties rather than its free-form text-generation faculties.

Early implementations of this idea, such as CodeIE by Li et al. and Code4Struct by Ji et al., demonstrated that reframing event and entity extraction as a code completion task significantly reduces formatting errors. Instead of asking a model to list arguments, the schema is presented as a Python class definition, and the model is prompted to instantiate that class using entities found in the source text:

# Target Schema defined as code
class MarriageEvent:
    def __init__(self, person1: str, person2: str, location: str, date: str):
        self.person1 = person1
        self.person2 = person2
        self.location = location
        self.date = date

# Input Text: "Alice and Bob tied the knot in Paris last Sunday."
# Model Output:
MarriageEvent(person1="Alice", person2="Bob", location="Paris", date="last Sunday")

By replacing natural language instructions with programmatic syntax, the model naturally avoids formatting errors because it treats the output as code that must syntactically compile.

Mapping Linguistic Taxonomies to Object-Oriented Programming

Real-world information extraction involves complex, hierarchical taxonomies and intricate relational constraints. Programmatic representations excel here because the core features of object-oriented programming (OOP)—such as class inheritance, type hints, and docstrings—map perfectly to linguistic structures.

Hierarchical Taxonomies via Inheritance

In large-scale extraction tasks, entities and events often belong to nested hierarchies. Li et al. (KnowCoder) and Zuo et al. (KnowCoder-X) leverage class inheritance to model these relationships. For instance, an Organization class can inherit from a base LegalEntity class. The model inherently understands that any attributes or constraints belonging to the parent class apply to the child class, allowing it to navigate thousands of fine-grained knowledge types without getting confused by overlapping definitions.

Guideline Embedding via Docstrings

Human annotators rely on detailed guidelines to resolve edge cases. Programmatic schemas allow researchers to embed these guidelines directly where the model processes them: inside class and method docstrings. Sainz et al. (GoLLIE) and Srivastava et al. demonstrate that instruction-tuning LLMs on schemas wrapped with code docstrings dramatically improves zero-shot generalization. The model reads the docstring as an operational specification, using its code-parsing capabilities to execute precise extractions on completely unseen data types.

Enforcing Structural Rigor and Compositionality

Beyond simple entity extraction, programmatic framing provides a robust mechanism for capturing highly complex, nested, or procedural relationships.

  • Discourse and Dependency Parsing: Shen et al. apply code representation to discourse dependency parsing. By representing linguistic dependency trees as nested function calls and class instantiations, they show that code syntax enforces structural constraints far more effectively than traditional flat tuple representations, preventing the model from generating invalid or disconnected trees.
  • Procedural and Action Extraction: In scientific domains, extracting experimental workflows requires capturing strict sequential logic. Ji et al. (ActionIE) use programming classes to convert unstructured scientific text into executable Python code blocks, mapping experimental steps directly to class variables and methods.
  • Complex Event Induction: Hao et al. utilize Python class definitions to prompt models for complex event schema induction, demonstrating that programming logic helps the model map out entire event graphs simultaneously while maintaining strict relational consistency.

This structural rigor also serves as a powerful defense against model hallucinations. As shown by You et al. (FACT), translating factual natural language into programming abstractions—where subjects become objects, attributes become properties, and causal relationships become functions—acts as a "semantic anchor." The inherent logical rigor of code prevents the model from generating contradictory or ungrounded claims.

A Language-Agnostic Semantic Anchor

One of the most remarkable properties of programmatic schema representation is its effectiveness in cross-lingual and low-resource settings. Natural language prompts are highly sensitive to translation nuances; a prompt written in English may perform poorly when translated into a low-resource language.

Programming languages, however, are universal. A Python class definition remains identical regardless of the source language being processed.

[English Text]  ──┐
[Spanish Text]  ──┼─> [ Universal Python Schema ] ─> [ Structured Output ]
[Swahili Text]  ──┘

Zuo et al. (KnowCoder-X) and Chen et al. (TransFusion) exploit this invariance to achieve state-of-the-art performance in multilingual information extraction. By standardizing target schemas into Python classes, the code acts as a language-agnostic bridge. The model can focus its parameters entirely on semantic understanding and cross-lingual alignment, rather than wrestling with language-specific formatting instructions.

Optimizing the Programmatic Prompt

To maximize the performance of this paradigm, researchers have focused on optimizing how these programmatic prompts are structured and selected:

  • API-Style Semantic Parsing: Bogin et al. show that providing structured domain descriptions allows models to treat semantic parsing as an API-calling task, enabling the model to apply standard software development patterns to solve compositional generalization problems.
  • In-Context Selection and Contrastive Learning: To improve few-shot performance, frameworks like TISE (Cao et al.) format complex event arguments as programmatic structures to guide the model toward consistent outputs. Similarly, Mo et al. (C-ICL) introduce contrastive in-context learning, presenting hard negative errors and corrections as Python-like code structures to teach the model to maintain strict schema compliance.
  • Schema-Guided Recovery: Even when dealing with highly messy, heterogeneous data layouts like scientific tables, providing a formal template (such as a JSON schema) allows models to perform iterative error recovery, as demonstrated by Bai et al. (INSTRUCTE).

By shifting the interface of information extraction from natural language to code, this research direction fundamentally changes how LLMs interact with structured tasks. It transforms a fuzzy, generative text problem into a deterministic, syntax-constrained execution problem, fully capitalizing on the structural intelligence embedded within code-trained models.

Key Foundational Work

Large language models for generative information extraction: a survey (2024) — contributed 2 concept elements to this direction.
KnowCoder: Coding Structured Knowledge into LLMs for Universal Information Extraction (2024) — contributed 2 concept elements to this direction.
ActionIE: Action Extraction from Scientific Literature with Programming Languages (2024) — contributed 2 concept elements to this direction.
History & citationVersion 1 · 2026-10-04

Current · Permanent link

View frozen version 1

Cite this direction

Latest version (a living page):

@misc{lacuna_code_based_schema_representation_7be7e2d8,
  title = {code-based schema representation},
  author = {{Lacuna}},
  howpublished = {Lacuna research direction},
  year = {2026},
  url = {https://lacuna.tiptreesystems.com/direction/code-based-schema-representation/rdr_7be7e2d8b3e1499ab57df283e9a0e6ba},
  note = {Latest version of a living research direction; cite a version for a fixed text. Accessed 2026-11-19},
}

This exact version (version 1, frozen text):

@misc{lacuna_code_based_schema_representation_7be7e2d8_v1,
  title = {code-based schema representation},
  author = {{Lacuna}},
  howpublished = {Lacuna research direction},
  year = {2026},
  url = {https://lacuna.tiptreesystems.com/direction/code-based-schema-representation/rdr_7be7e2d8b3e1499ab57df283e9a0e6ba/v/1},
  note = {Version 1, frozen 2026-10-04. Accessed 2026-11-19},
}
About

These directions overlap with this one through shared papers.

See something wrong? Report a mistake