Papers
Topics
Authors
Recent
Search
2000 character limit reached

KnowCoder: Code-Style UIE Framework

Updated 24 March 2026
  • KnowCoder is a code-style UIE framework that leverages Python class schemas to unify entity, relation, and event extraction.
  • It pairs a large-scale schema library with a two-phase learning system, achieving significant improvements in zero-shot, few-shot, and supervised extraction tasks.
  • The framework’s innovative use of inheritance, type hints, and docstrings enables efficient schema ingestion, constraint enforcement, and scalable post-processing.

KnowCoder is a code-style, LLM framework for Universal Information Extraction (UIE) that introduces a unified, Python-class-based schema representation, the largest open code-style schema library for structured knowledge, and a two-phase learning system to achieve strong zero-shot, few-shot, and supervised results across multiple extraction tasks. By encoding all information extraction schemas—entities, relations, and events—as Python classes with inheritance, natural-language docstrings, and type hints, KnowCoder achieves efficient, LLM-friendly schema ingestion, precise constraint enforcement, and scalable post-processing methods. A library of >30,000 class definitions derived primarily from Wikidata is paired with a two-step training process: large-scale code pretraining for schema understanding, followed by instruction tuning for schema adherence. The result is a high-accuracy, generalizable UIE component that supports multi-task, multi-schema IE in a unified prompt and code-generation pipeline (Li et al., 2024).

1. Code-Style Schema Representation

KnowCoder represents every schema element—entity, relation, event—as a Python class, exposing taxonomic hierarchy through inheritance, encoding task constraints via constructor type annotations, and supporting documentation and post-processing methods with docstrings and classmethods. Concretely, a UIE schema S=(E,R,V)S=(E,R,V), with EE entity types, RR relation types, and VV event types, is mapped into corresponding class definitions:

1
2
3
4
5
6
7
8
9
10
class PlaceOfBirth(Relation):
    """
    Description: Most specific known (e.g. city instead of country) birth location of a person, animal or fictional character.
    Examples: (Mozart, Salzburg), (Queen Myeongui, Goryeo)
    """
    def __init__(self, head_entity: Human, tail_entity: SpatialEntity):
        super().__init__(head_entity=head_entity, tail_entity=tail_entity)
    @classmethod
    def postprocess(cls, results):
        return [r for r in results if valid_span(r)]

Taxonomic structure is induced via class PlaceOfBirth(Relation), ensuring hierarchical consistency. Constraints are enforced by type hints (e.g., head_entity: Human). Definitions and processing logic reside within docstrings and class methods. This mapping is formally denoted as C:S{C(c)}cSC: S \mapsto \{C(c)\}_{c\in S}, where each C(c)C(c) is a Python class encapsulating name, parent class, docstring, argument types, and optional post-processing.

2. Schema Library Construction

The KnowCoder schema library is curated from a Wikidata dump (2022-07-04), filtered to concepts appearing in large open IE datasets (KELM, UniversalNER, InstructIE, LSEE). Entities, relations, and events are extracted alongside their SubclassOf relations for inheritance. Definitions derive from Wikidata or are synthesized via GPT-4 when missing. Argument type constraints are inferred from dataset co-occurrences. The final schema scale:

Concept Type Count
Entities 29,177
Relations 876
Events 519
Total >30,000

This is the largest open-source code-style schema resource for IE (Li et al., 2024). The library supports deep inheritance chains and arbitrarily complex constructor signatures, enabling uniform handling of simple to highly nested schemas.

3. Two-Phase Learning Framework

KnowCoder employs a two-stage training architecture:

(1) Code Pretraining (Schema Understanding):

The LLM autoregressively predicts tokens over ≈1.5B adversarially generated code samples consisting of schema definitions and instantiation patterns. Data includes mixtures of class definitions and instance creation examples:

1
2
3
4
sentence = "Mozart was born in Salzburg."
from Relations import PlaceOfBirth
from Entities import Human, SpatialEntity
results = [PlaceOfBirth(Human("Mozart"), SpatialEntity("Salzburg"))]

This pretraining phase yields substantial few-shot generalization: on seven NER datasets, KnowCoder-7B yields 46.3% average F1 (5-shot), a 49.8% relative improvement over LLaMA2-7B at 30.9%.

(2) Instruction Tuning (Schema Following):

The LLM is further tuned to follow composite extraction instructions per prompt, with class definitions, input sentences, and output lists of correctly instantiated objects. Each training example includes distractor classes (20% of sampled types are not present in the sentence) and some negative prompts with no correct label. The objective remains token-level cross-entropy over correct extractions. This phase drives generalization: in zero-shot NER evaluation, KnowCoder-7B yields 60.1% average F1 (vs. 53.4% best unfine-tuned baseline, a +12.5% lift). In low-resource settings (1% data), it achieves 21.9% relative gains over UIE-base.

4. Supervised Refinement and Multi-Task Integration

KnowCoder supports supervised fine-tuning across NER, RE, ED, and EAE datasets, requiring no schema unification or task-specific model heads, as every label set is mapped to the code-style class format. Supervised refinement is conducted with LoRA-fine-tuning (rank=32, α=64, dropout=0.1, LR=3×10⁻⁴, 3 epochs, 1.9M samples), yielding consistent absolute and relative F1 improvements:

  • NER: from SOTA 85.2% to 86.1%
  • RE: from 66.7% to 71.7% (+7.5% relative)
  • ED/EAE: +>1 point each, surpassing prior baselines

Disambiguation of overlapping type granularities is handled via post-processing, climbing the Wikidata taxonomy when the model predicts a fine-grained type not present in the dataset labelset.

5. Inference Pipeline and Post-processing

At inference, the target task is formulated as a composite prompt containing class imports, schema docstrings, and the input text:

1
2
3
from Entities import Human
from Relations import PlaceOfBirth
sentence = "Mozart was born in Salzburg."

The model generates results = [PlaceOfBirth(Human("Mozart"), SpatialEntity("Salzburg"))]. Output is parsed via regex and optionally mapped to higher-level types for compatibility with evaluation scripts. As an example, the extraction pseudocode:

1
2
3
4
5
6
7
8
def extract_structured(text, classes):
    prompt = render_imports(classes)
    prompt += TASK_COMMENT
    prompt += f'sentence = "{text}"\n'
    code = model.generate(prompt)
    insts = parse_results(code)
    insts = superclass_induction(insts)
    return insts

This enables streamlined integration of arbitrary extraction tasks with any combination of classes.

6. Experimental Results and Impact

KnowCoder demonstrates state-of-the-art or near-SOTA performance in multiple regimes:

  • Few-shot (5-shot NER): +49.8% improvement over LLaMA2-7B
  • Zero-shot (7 NER domains): +12.5% over best baseline
  • Low-resource (1% data, NER/RE/ED/EAE): +21.9% over UIE-base
  • Supervised (NER/RE/ED/EAE): +1.1% to +7.5% F1 improvements

The code-style schema framework enables multi-task instruction prompts, scalable new-type adaptation, and post-processing for label-set reconciliation. The 30K-class schema library sets a new scale for UIE research. Empirical ablations confirm that the code representation and instruction-tuning drive the observed robustness and generalization (Li et al., 2024).


In summary, KnowCoder defines a unified, code-style UIE framework combining a large-scale Python-class schema library, two-phase schema-centric pretraining and tuning, and a modular, inference-time parsing system, resulting in SOTA robustness and generalization for structured information extraction.

Definition Search Book Streamline Icon: https://streamlinehq.com
References (1)

Topic to Video (Beta)

No one has generated a video about this topic yet.

Whiteboard

No one has generated a whiteboard explanation for this topic yet.

Follow Topic

Get notified by email when new papers are published related to KnowCoder.