Skip to content

Implement a detector against detect and check it with a contract suite (#140) - #188

Merged
hslev merged 8 commits into
stateless_corefrom
140-implement-a-detector-against-detect-and-check-it-with-a-contract-suite
Nov 6, 2026
Merged

hslev merged 8 commits into
stateless_corefrom
140-implement-a-detector-against-detect-and-check-it-with-a-contract-suite

Conversation

@hslev

@hslev hslev commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Summary

Builds the detector contract on BaseDetector in three steps (parent #140, epic #126):

  • detect / adetect (Add BaseDetector.detect and adetect with min_score and support flags #141). BaseDetector.detect(request, *, min_score, include_tokens, include_detail) -> Detection validates its arguments, calls the _detect hook and drops spans below min_score. adetect runs detect through asyncio.to_thread; its docstring states the shared default executor (min(32, cpu + 4) workers) and that cancelling stops the wait, not the thread. Adds the supports_tokens, supports_detail and model_id attributes. Errors are typed InvalidInputErrors with the codes invalid_min_score, tokens_not_supported and detail_not_supported.

  • Bridge between old and new interfaces (Bridge old and new BaseDetector interfaces #142).

    • A subclass that implements only _detect gets working predict, predict_prompt and predict_prompt_batch.
    • A subclass that implements only the legacy predict* methods works through detect and adetect. A missing confidence becomes a score of 1.0 with one synthetic_score warning. It emits one LettuceDetectDeprecationWarning per class, thread-safe.
    • A subclass that overrides none of detect, _detect and predict raises TypeError when it is defined.
    • A legacy class that has no predict_prompt raises InvalidInputError(code="prompt_input_not_supported") for a prompt request instead of recursing.
  • Reusable contract suite (Add the reusable detector contract suite #143). tests/detector_contract.py is a base class that a detector task subclasses by supplying a factory and two sample requests. It checks, through detect and adetect only:

    • span offsets and scores, and min_score filtering;
    • usage with model_id and model_version;
    • repeatable results;
    • no files written to cwd or HOME;
    • no printing, and no customer text on loggers other than lettucedetect.content;
    • that adetect equals detect, runs off the loop thread and keeps the loop free, and that concurrent calls return in call order;
    • that errors from the await match those from detect;
    • that cancelling with Task.cancel() or asyncio.wait_for leaves the detector usable. The test shows the worker thread keeps running until released.

    It runs against a detect-only and a predict-only stand-in, and against nine deliberately broken stand-ins that must each fail the matching check.

pyproject.toml gets asyncio_mode = "auto" for the async tests. The existing detectors still override predict*, so their behaviour is unchanged. HallucinationDetector in models/inference.py does not subclass BaseDetector and is unaffected.

Related issue

Closes #141, closes #142, closes #143. Parent: #140.

Type of change

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

Testing

  • ruff format --check lettucedetect/ lettucedetect_api/ tests/ (84 files already formatted)
  • ruff check lettucedetect/ lettucedetect_api/ tests/ --extend-exclude lettucedetect/integrations/ (all checks passed)
  • python -m pytest (395 passed; 357 before the contract suite)
  • Other: uv run --extra dev pytest tests/test_detector_contract_pytest.py -q (38 passed). ty check is clean on lettucedetect/detectors/base.py and tests/detector_contract.py.

Notes for reviewers

  • Changed test: one test I wrote for Add BaseDetector.detect and adetect with min_score and support flags #141, test_default_hook_raises_not_implemented, now builds BaseDetector() directly. A subclass that overrides nothing is a TypeError after Bridge old and new BaseDetector interfaces #142, so the old form could not exist. No test for the existing detectors changed.
  • _detect is not abstract. Making it abstract broke LLMDetector, RAGFactCheckerDetector and TransformerDetector, which still implement only predict*. The __init_subclass__ guard gives the same safety for new subclasses.
  • lettucedetect.content logger: nothing in the code creates it yet. The contract only exempts that name, as CONTRIBUTING describes.
  • Out of scope: the three existing detectors do not yet implement _detect, and predict* is not deprecated for them. Those are later tickets.
  • Base branch: the issues name stateless_core as the target.

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 and others added 8 commits November 6, 2026 14:28
…convention-in-contributingmd' into 140-implement-a-detector-against-detect-and-check-it-with-a-contract-suite
…or-every-deprecated-name' into 140-implement-a-detector-against-detect-and-check-it-with-a-contract-suite
…rors' into 140-implement-a-detector-against-detect-and-check-it-with-a-contract-suite
@hslev
hslev merged commit 2b21986 into stateless_core Nov 6, 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