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

CodeIE: Large Code Generation Models are Better Few-Shot Information Extractors

Peng Li, Tianxiang Sun, Qiong Tang, Hang Yan, Yuanbin Wu, Xuanjing Huang, Xipeng Qiu

Abstract

Information Extraction (IE) tasks, such as Named Entity Recognition (NER) and Relation Extraction (RE), require transforming unstructured text into structured data. While Large Language Models (LLMs) have shown promise in few-shot learning, they often struggle with the rigid structural requirements of IE outputs, leading to formatting errors and hallucinations. We propose CodeIE, a framework that reformulates IE tasks as code generation problems. By leveraging Code-LLMs (e.g., Codex) and structured code-style prompting, CodeIE aligns the inference task with the structured data seen during pre-training. Our experiments across seven benchmarks demonstrate that CodeIE significantly outperforms standard natural language prompting, achieving higher F1 scores and near-zero structural errors.

Introduction: The Structure Mismatch in IE

Information Extraction is fundamentally a "structure-to-structure" problem. To adapt modern Natural Language LLMs (NL-LLMs) like GPT-3 for IE, researchers typically "linearize" structured outputs into strings (e.g., ((person: Steve) (organization: Apple))). However, these linearized strings are often "unnatural" compared to the unstructured natural language these models are primarily trained on. This format mismatch leads to "fragile" outputs, where models frequently hallucinate invalid formats or fail to follow the required schema, especially in few-shot settings.

Comparison of model types for IE tasks, highlighting the advantages of Code-LLMs in structured pre-training and few-shot performance.

As shown in the comparison above, while specialized models like UIE are generative and structured, they lack the scale and few-shot capabilities of LLMs. Conversely, NL-LLMs lack the structured pre-training necessary for robust IE. Code-LLMs (e.g., Codex) bridge this gap by combining extreme scale with a pre-training objective focused on structured programming languages.

Methodology: The CodeIE Framework

CodeIE addresses the structure mismatch by recasting IE tasks as Python code generation. Instead of providing instructions in plain English, CodeIE utilizes code-style prompts that mimic the structure of a Python script. This approach leverages the fact that structured data formats (like dictionaries and lists) are native to the codebases on which these models were trained.

The CodeIE framework converts NER and RE tasks into code generation problems using Python-style function prompts.

A typical CodeIE prompt consists of:

  1. Function Definition: A signature like def named_entity_recognition(input_text):.
  2. Docstring: A natural language description of the task embedded within the function.
  3. Variable Assignment: Setting the input_text variable to the sentence being analyzed.
  4. Structured Output: Initializing an empty list (e.g., entity_list = []) and prompting the model to populate it using .append() calls with Python dictionaries.

By using In-Context Learning, a few of these code-style examples are prepended to the prompt, allowing the model to follow the pattern and "complete" the code for a new test case with high precision.

Key Results and Insights

The researchers evaluated CodeIE across seven benchmarks, including CoNLL and ACE datasets for both NER and RE. The results highlight several key advantages of the code-generation approach:

  • Superior Performance: CodeIE (Codex with code prompts) achieved an average F1 score of 42.78, significantly outperforming GPT-3 with standard text prompts (32.65). Even NL-LLMs like GPT-3 saw a 23% performance boost when switched to code-style prompts.
  • Structural Fidelity: Code-style prompts reduced structural error rates to nearly 0%. In contrast, text-style prompts often resulted in 10-30% of outputs being unparseable due to formatting inconsistencies.
  • Few-Shot Efficiency: In extreme 1-shot settings, CodeIE improved upon specialized models like UIE-large by over 60% on certain benchmarks, demonstrating its robustness when data is scarce.
  • Predictability: Perplexity analysis confirmed that Code-LLMs find code-formatted IE tasks much more predictable than NL-LLMs find text-formatted ones, validating the hypothesis that structure is "natural" for code models.

Conclusion and Future Work

CodeIE demonstrates that the inherent structure of programming languages is a powerful asset for structured NLP tasks. By aligning the prompting strategy with the model's pre-training data, CodeIE achieves state-of-the-art few-shot performance in Information Extraction. Future research will explore the vast design space of code prompts—such as using classes instead of functions—and extend the framework to non-English IE tasks to overcome current language biases in code-trained models.

Summary updated

BibTeX
@inproceedings{li2023codeie,
  author = {Peng Li and Tianxiang Sun and Qiong Tang and Hang Yan and Yuanbin Wu and Xuanjing Huang and Xipeng Qiu},
  title = {{CodeIE: Large Code Generation Models are Better Few-Shot Information Extractors}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2023},
  doi = {10.18653/v1/2023.acl-long.855},
}
Figure