hankcs/HanLP · error · ValueError

Unrecognized label encoding {self.label_encoding}

Error message

Unrecognized label encoding {self.label_encoding}

What it means

Raised by CoNNEval.update_state when the metric object was constructed with a label_encoding other than the supported chunk encodings. HanLP's chunking F1 metric only knows how to score sequences in IOBES or IOB2/BIO schemes, so any other string (or a typo like 'BIOES', 'iob2') is rejected before any counting happens.

Source

Thrown at hanlp/metrics/chunking/conlleval.py:81

        self.count = EvalCounts()

    def reset(self):
        self.count = EvalCounts()

    @property
    def score(self):
        return self.result(False, False).fscore

    def reset_state(self):
        self.count.reset_state()

    def update_state(self, true_seqs: List[str], pred_seqs: List[str]):
        if self.label_encoding == 'IOBES':
            count = evaluate_iobes(true_seqs, pred_seqs)
        elif self.label_encoding in ['IOB2', 'BIO']:
            count = evaluate_iob2(true_seqs, pred_seqs)
        else:
            raise ValueError(f'Unrecognized label encoding {self.label_encoding}')
        self.count.correct_chunk += count.correct_chunk
        self.count.correct_tags += count.correct_tags
        self.count.total_gold += count.total_gold
        self.count.total_pred += count.total_pred
        self.count.token_counter += count.token_counter
        for s, n in zip(self.count.states, count.states):
            for k, v in n.items():
                s[k] = s.get(k, 0) + v

    def batch_update_state(self, true_seqs: List[List[str]], pred_seqs: List[List[str]]):
        for t, p in zip(true_seqs, pred_seqs):
            self.update_state(t, p)

    def result(self, full=True, verbose=True) -> Union[Tuple[DetailedF1, dict, str], DetailedF1]:
        if full:
            out = io.StringIO()
            overall, by_type = report(self.count, out)
            text = out.getvalue()

View on GitHub (pinned to ddb1299bdd)

Solutions

  1. Set label_encoding to one of the supported values: 'IOBES', 'IOB2' or 'BIO' (note 'BIOES' is NOT accepted, the E-first name is 'IOBES').
  2. If your tags use another scheme (e.g. IOB1), convert them to IOB2 or IOBES before calling update_state.
  3. Check for case: the comparison is case-sensitive, so pass uppercase strings.

Example fix

// before
metric = CoNNEval(label_encoding='BIOES')
// after
metric = CoNNEval(label_encoding='IOBES')
Defensive patterns

Strategy: validation

Validate before calling

ALLOWED = {'IOBES', 'IOB2', 'BIO'}
assert metric.label_encoding in ALLOWED, f'use one of {ALLOWED}'

Try / catch

try:
    metric.update_state(gold, pred)
except ValueError as e:
    if 'Unrecognized label encoding' in str(e):
        raise  # fix config: only IOBES/IOB2/BIO supported
    raise

Prevention

When it happens

Trigger: Creating CoNNEval/Chunking metric with label_encoding not in {'IOBES','IOB2','BIO'} (e.g. 'BIOES', 'IOB1', lowercase 'iobes'), or passing a component's config value straight into the metric, then calling update_state (directly or via batch_update_state) during evaluation.

Common situations: Typos in metric config; porting code that used a different tagging scheme name (AllenNLP-style 'BIOES' vs HanLP's 'IOBES'); assuming the metric accepts IOB1/IOB or lowercase encodings.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of hankcs/HanLP@ddb1299bdd (2026-08-27). Data as JSON: /api/errors/ce2d13a33049cc03. Report an issue: GitHub.