Traditional Information Extraction (IE) involves identifying entities, relations, and events within unstructured text and mapping them to a predefined schema. While Large Language Models (LLMs) are naturally capable of this, they often struggle with "structural hallucinations"—generating outputs that fail to adhere to rigid formats or misinterpreting complex, nested relationships.
A growing body of research suggests that the solution lies in treating IE not as a natural language task, but as a code generation task. By representing extraction schemas as Python classes and task instructions as executable code, researchers are leveraging the logical rigor and structural priors that LLMs acquire during code-heavy pre-training.
The Structural Mismatch of Natural Language
When we prompt an LLM in natural language to "extract all person-organization relationships," we are asking it to map fluid, ambiguous prose into a rigid data structure. As noted in Large language models for generative information extraction: a survey, this often leads to a mismatch where the model prioritizes conversational fluency over structural accuracy.
In contrast, programming languages are designed to be unambiguous. Researchers like Peng Li in CodeIE: Large Code Generation Models are Better Few-Shot Information Extractors argue that because models like Codex are trained on millions of lines of structured code, they possess an inherent "structural prior." When an extraction task is framed as a code completion problem—wrapping the input text inside a Python function or class definition—the model enters a state of higher precision, significantly reducing formatting errors.
Code as a Semantic Scaffold
The core mechanism of this research direction is the transformation of an extraction schema into an object-oriented structure. Instead of providing a list of labels, researchers define the schema using Python constructs:
- Classes as Entities/Events: Each target type is defined as a class.
- Attributes as Roles: Fields within the class represent the attributes or arguments of the entity (e.g., a
Personclass with abirth_dateattribute). - Docstrings as Guidelines: Detailed definitions and annotation rules are placed within class docstrings.
- Inheritance as Taxonomies: Hierarchical relationships between types are modeled using class inheritance.
In GoLLIE: Annotation Guidelines improve Zero-Shot Information-Extraction, Oscar Sainz and colleagues demonstrate that this approach allows models to "read the manual." By embedding annotation guidelines directly into the code structure, the model treats the task as an executable specification rather than a vague request. This allows the model to generalize to unseen tasks (zero-shot) far more effectively than standard prompting.
Leveraging Programming Logic for Reasoning
Beyond simple formatting, code-centric representations enable more complex reasoning. Zixuan Li and the team behind KnowCoder: Coding Structured Knowledge into LLMs for Universal Information Extraction utilize Python's type hints and inheritance to model massive taxonomies of over 30,000 knowledge types. This structure prevents the "logical drift" often seen in long-form text reasoning.
This "scaffolding" effect is also evident in Natural Language Embedded Programs for Hybrid Language Symbolic Reasoning, where Tianhua Zhang shows that storing natural language facts in structured data types (like dictionaries) within a program helps the model maintain consistency between extracted knowledge and final algorithmic outputs.
Cross-Lingual and Domain Robustness
One of the most striking findings in this area is the effectiveness of code as a "universal language." Yuxin Zuo introduces KnowCoder-X: Boosting Multilingual Information Extraction via Code, which uses Python classes to bridge the gap between different natural languages. Because the output format (Python code) remains identical regardless of whether the input is in English, Chinese, or Swahili, the model can leverage its cross-lingual alignment to extract information more accurately in low-resource settings.
Similarly, in specialized fields like scientific literature, ActionIE by Heng Ji uses programming classes to extract experimental procedures. By framing actions as functions with specific arguments, the model can capture the procedural logic of a laboratory experiment with a level of detail that traditional sequence labeling cannot reach.
Why It Works: The Intuition
The success of this direction rests on three pillars:
- Constraint Enforcement: The rigid syntax of programming languages (brackets, colons, indentation) acts as a natural constraint that prevents the model from rambling or deviating from the schema.
- Deterministic Validation: Because the output is code, it can be passed through a Python interpreter or a type checker to verify its structural integrity immediately, as explored in the Agent-Event-Coder (AEC) framework.
- Pre-training Alignment: LLMs are trained on vast amounts of high-quality, logically consistent code. By framing IE as code generation, we are prompting the model to use its most "logical" and "structured" latent representations.
By moving away from natural language prompts and toward executable schemas, this research direction transforms LLMs from conversationalists into precise, schema-aware extraction engines.