TL;DR
CodeNER introduces a novel code-based prompting method that structures Named Entity Recognition (NER) tasks as executable-style code blocks (e.g., Python or C++) for Large Language Models (LLMs). By leveraging the inherent ability of LLMs to understand programmatic logic and loops, CodeNER significantly improves boundary detection and sequential labeling accuracy across multiple languages without requiring any model fine-tuning.
What Problem Does This Paper Address?
Named Entity Recognition (NER) involves identifying specific spans of text and classifying them into predefined categories (such as Person, Location, or Organization). While LLMs excel at zero-shot tasks, conventional text-based prompting struggles with NER due to a fundamental format mismatch: LLMs operate on a text-in-text-out schema, whereas NER is inherently a text-in-span-out task.
This mismatch makes it difficult for LLMs to strictly follow sequential token-labeling formats like the BIO (Beginning, Inside, Outside) schema. Standard text prompts often lead to overlapping labels, missed duplicate words, or poor boundary detection for complex tokens like URLs or special characters.
Methods
The authors propose CodeNER, a method that wraps the NER task inside a structured programming language format. Instead of asking the model in plain English to "find entities," the prompt defines a mock programmatic environment containing:
- A tokenized
sentencevariable. - A dictionary (
ner_tag_labels) mapping BIO tags to numeric identifiers, complete with code comments explaining each label. - A function (
find_ner_tags) containing an explicitforloop that guides the LLM to process the input sequence token-by-token.
This setup forces the LLM to utilize its code-comprehension capabilities to maintain strict sequential tracking. CodeNER can also be combined with Chain-of-Thought (CoT) prompting ("Let's think step by step") to further boost reasoning performance.
Datasets
The method was comprehensively evaluated on 10 benchmark datasets spanning 5 languages:
- English: CoNLL03, FIN, WNUT-17, MIT Movie Trivia, and MIT Restaurant.
- Arabic: ArabicDatasets A and B.
- Finnish: FinnishDataset A.
- German: SwissNER.
- Danish: DaNE.
Key Insights and Ideas
- Code as a Structural Constraint: Programming languages possess strict syntax and explicit execution flows (like loops). By embedding natural language tasks inside code structures, researchers can force LLMs to adhere to sequential constraints that natural language prompts fail to guarantee.
- Looping Eliminates Overlaps: Forcing the model through a simulated token-by-token loop prevents it from generating overlapping entity spans or skipping repeated words—common failure modes in standard text prompting.
- Language Agnosticism (Mostly): The strategy works well across both Python and C++, showing that the underlying logical structure of the prompt is what drives the improvement, not a specific language syntax. However, it fails with languages like Java if the model lacks sufficient pre-trained alignment or structural familiarity for this specific task setup.
Key Results
- GPT-4 & GPT-4 Turbo: CodeNER consistently outperformed vanilla text prompting. On GPT-4, it achieved an average macro-F1 improvement of 2.41 points, highlighted by a massive 18.30 point jump on the FIN dataset and a 9.48 point increase on MIT Restaurant. For GPT-4 Turbo, CodeNER boosted the average F1 score by 3.07 points.
- Open-Source Models: When tested on open-source models like Llama-3-8B and Phi-3-mini, CodeNER significantly outperformed existing zero-shot structural prompting baselines like GoLLIE and GNER.
Limitations and Future Work
- Dependency on Code Pre-training: CodeNER relies heavily on the model's inherent ability to understand code. LLMs without robust code pre-training may require explicit fine-tuning to benefit from this method.
- The "Long Span" Weakness: CodeNER struggles with datasets where entities span massive chunks of text or entire sentences containing many function words (e.g., "Plot" or "Quote" tags in the MIT-Movie dataset). In these specific scenarios, vanilla text prompts that look at the broader context without token-level constraints can actually perform better.
- Future Directions: The authors plan to explore pre-training strategies that incorporate auxiliary tasks designed to help the model better identify function words and complex syntactic structures.
Summary updated