Skip to content

feat: get typed spans from the encoder (#153) - #191

Merged
hslev merged 2 commits into
stateless_corefrom
153-get-typed-spans-from-the-encoder
Nov 9, 2026
Merged

hslev merged 2 commits into
stateless_corefrom
153-get-typed-spans-from-the-encoder

Conversation

@hslev

@hslev hslev commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Summary

Typed spans from the encoder (#153, sub-issues #154 and #155). TaxonomyTyper becomes a loaded object like the encoder, and EncoderDetector.detect uses it to give every span a category and subcategory. A typed call is metered with two usage entries: the encoder and the typer.

Base branch is stateless_core, not main. Close #153 by hand after the merge.

 TaxonomyTyper
-  __init__(model_path, device, max_length, categories, subcategories, **tok_kwargs)   # downloads
+  __init__(encoder, tokenizer, *, categories, subcategories, max_length, model_id, model_version)  # loaded objects, no download
+  from_pretrained(model_id, *, device, categories, subcategories, model_version, **loader_kwargs)  # loads, logs model_loaded
+  type_spans(...)                                                                     # now runs under a private lock
 EncoderDetector.detect
   score tokens -> aggregate -> spans
+  if typer and spans
+    typer.type_spans(answer, context, spans)     # context = sources joined by "\n", or the prompt
+  DetectedSpan(..., category, subcategory)       # both None without a typer
   usage = [encoder]
+  usage += [typer: model_id, model_version, {"spans": n}]   # n = 0 when there are no spans
EncoderDetector.from_pretrained(taxonomy_head=..., include_taxonomy=...)
  _finish_load
    TaxonomyTyper.from_pretrained(head, device, categories, subcategories,
                                  cache_dir / local_files_only / token only)

Details:

  • usage reports the typer's spans as the number of spans it was given, before min_score filters them.
  • Typing runs outside the encoder's scoring lock. The typer serializes itself, so concurrent callers sharing one typer run one at a time.
  • A local-path typer takes the caller's model_version. A hub typer records the resolved commit.
  • revision is not forwarded to the typing head, because it names a revision of the encoder's repo. Only cache_dir, local_files_only and token are shared.
  • The model-load error mapping moved into _model_load_error and is shared by the encoder and the typer. A local path raises ConfigurationError and a hub id raises UpstreamError, both with model_load_failed.
  • TransformerDetector(taxonomy_head=...) still types its spans. It now builds the typer through TaxonomyTyper.from_pretrained, and the output equals the typed golden fixture.
  • No customer text is logged. The new debug lines carry counts, ids and timings only.

Related issue

Closes #153 (also covers #154 and #155). Part of #126.

Type of change

  • Bug fix
  • Feature
  • Documentation
  • Tests
  • Refactor or maintenance

Testing

New tests: tests/test_taxonomy_typer_pytest.py (24 tests), typed cases in tests/test_encoder_detector_pytest.py, and a typed golden case in tests/test_golden_transformer_pytest.py. Existing tests, including the custom taxonomy tests, pass with only mechanical edits.

  • ruff format --check lettucedetect/ lettucedetect_api/ tests/ → 96 files already formatted
  • ruff check lettucedetect/ lettucedetect_api/ tests/ --extend-exclude lettucedetect/integrations/ → All checks passed
  • python -m pytest → 689 passed
  • Other:

Before and after for the main behavior:

before: detector.detect(request).spans[0].category      -> None
        detector.detect(request).usage                  -> [encoder]
after:  detector.detect(request).spans[0].category      -> "<category>" (subcategory set too)
        detector.detect(request).usage                  -> [encoder, typer(spans=n)]

Checklist

  • I kept the PR focused on one change.
  • I added or updated tests/docs when needed.
  • I checked that no secrets, API keys, or credentials are included.

Rights & sign-off (required)

  • I certify that I have the right to submit this code and that it may be
    distributed under the repository's MIT license
    (see CONTRIBUTING).

@hslev
hslev merged commit d0a7d5c into stateless_core Nov 9, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant