Constrained tagging ensures exact span labeling in text outputs
Constrained Tagging for Span Labeling
Information Retrieval
Summary
Labeling parts of text accurately is tricky because sometimes models guess spans that don’t exactly match the input or appear multiple times. The authors propose a method called constrained tagging, which forces the model to reproduce the input text exactly but wrap certain parts with special labels. This method uses rules to allow only valid tokens that keep the input unchanged while adding tags, making it easier to know precisely where each labeled span starts and ends. Tests on many datasets showed this method found every span it labeled and did so without slowing down the model.
What this means in practice
- •For natural language processing engineers: Enhance text annotation systems by accurately marking spans within inputs without training on specific tasks.
- •For software developers building ai tools: Build text analysis tools that guarantee labeled spans exactly match the source content, improving reliability in information extraction.
Authors
Radek Štulc, Jan Drchal
Abstract
Generative LLMs can label spans without task-specific training, but their output is not tied to positions in the input: a returned span may be absent from the input or occur several times. We present constrained tagging, a training-free method in which the model reproduces the input and wraps each span in labeled inline markup. A logits processor using a byte-level token trie and a finite-state machine admits only tokens that continue the input verbatim or form a valid tag, so the output reproduces the input exactly with labels from the given set, and spans may begin or end at any character boundary, even inside a token. Two-phase decoding lets reasoning models reason before the constraint engages. On six datasets and six open-weight models, the guarantee holds in all but 0.022% of 255,312 constrained generations, each failure a reasoning model exhausting its token budget or never closing its reasoning, and the constraint adds no measurable cost. Unlike context-based prompting, which loses 8.5% of its spans, constrained tagging locates every span it proposes on every dataset; it is as accurate on two datasets, and on the other four its errors lie in which spans the model chooses, not where they are.