22  Evaluate inference (NER)

Run a token-classification model over a registered source and score its predicted entity spans against gold spans.

When to use this pattern. You have a labelled NER holdout set (one gold span per row) and you want strict entity-level precision, recall, and F1 — both overall and per entity type — to compare two NER models or to track regressions on the same one.

22.1 API surface exercised

  • Session.eval_inference(*, model, source, columns, task, golden_source, label_column)

The returned dict carries aggregate (tagged by "task" — "ner" for this recipe) with precision, recall, f1, and per_type (one breakdown per entity type the model emitted or the gold set carried), plus per_record (one entry per aligned {record_id, predicted, gold} where predicted and gold are entity-span lists, each tagged "task": "ner").

The task argument is the string form of the inference task — "ner" here. For classification, see ../eval_inference/.

22.2 Golden source shape

eval_inference with task="ner" requires a registered source with these columns — one row per entity span (multiple spans on the same id accumulate into one per-row gold set):

column type example
id utf8 "1"
<label_column> utf8 PER
start i64 0
end i64 13

label_column is the kwarg you pass at call time — label in this recipe. start is inclusive, end is exclusive, both byte offsets into the source row’s text column. The label set must match the shipped model’s id2label minus the B-/I- prefixes — tiny_modernbert_ner knows PER and ORG only.

Rows in the source without a matching gold id are silently dropped from the metric (same alignment rule the classification recipe uses).

The program is cookbook/recipes/eval_inference_ner/example.py; its steps run here in order.

import tempfile

import jammi
from jammi_cookbook import fixtures

MODEL = fixtures.model("tiny_modernbert_ner")

db = jammi.connect(f"file://{tempfile.mkdtemp()}")

22.3 Register the text and the gold spans

The corpus is the text to tag. The gold source holds one row per entity span, (id, label, start, end); spans sharing an id make up that row’s gold set.

db.add_source("corpus", url=str(fixtures.path("tiny_ner_corpus.parquet")), format="parquet")
db.add_source("golden", url=str(fixtures.path("tiny_ner_gold.csv")), format="csv")

22.4 Run the model and score it

eval_inference with task="ner" runs the model over columns and matches its predicted spans against the gold ones. Matching is strict at the entity level: a prediction is a true positive only when its (label, start, end) equals a gold span’s.

metrics = db.eval_inference(
    model=MODEL,
    source="corpus",
    columns=["text"],
    task="ner",
    golden_source="golden.public.tiny_ner_gold",
    label_column="label",
)

aggregate = metrics["aggregate"]
assert aggregate["task"] == "ner", aggregate["task"]
for key in ("precision", "recall", "f1"):
    assert 0.0 <= aggregate[key] <= 1.0, f"{key} out of range: {aggregate[key]}"
print(f"precision: {aggregate['precision']:.4f}")
print(f"recall:    {aggregate['recall']:.4f}")
print(f"f1:        {aggregate['f1']:.4f}")
precision: 0.0000
recall:    0.0000
f1:        0.0000
2026-10-03T07:44:05.471346Z  WARN jammi_kernels::admission: fused-kernel domain check failed; falling back to the eager composition op="attention_block_fused" predicate="head_dim_is_attention_block_fixed_head_dim"

22.5 Read the breakdowns

aggregate["per_type"] scores each entity type on its own. per_record holds one entry per row, with the predicted and the gold span lists side by side, which is where to look when a type scores low.

per_type = aggregate.get("per_type", {})
assert isinstance(per_type, dict), f"per_type shape: {type(per_type)}"
for label, stats in per_type.items():
    print(
        f"{label:<6} precision={stats['precision']:.4f}"
        f"  recall={stats['recall']:.4f}  f1={stats['f1']:.4f}"
        f"  support={stats['support']}"
    )

per_record = metrics["per_record"]
assert len(per_record) > 0, "per_record must carry one entry per aligned row"
for entry in per_record:
    assert entry["task"] == "ner", entry["task"]
    assert isinstance(entry["predicted"], list)
    assert isinstance(entry["gold"], list)
print(f"{len(per_record)} rows scored; the first: {per_record[0]}")
ORG    precision=0.0000  recall=0.0000  f1=0.0000  support=23
PER    precision=0.0000  recall=0.0000  f1=0.0000  support=18
20 rows scored; the first: {'task': 'ner', 'record_id': '1', 'predicted': [], 'gold': [{'label': 'PER', 'start': 0, 'end': 13, 'text': '', 'confidence': 0.0}, {'label': 'ORG', 'start': 21, 'end': 30, 'text': '', 'confidence': 0.0}]}
db.close()