Skip to content

Implement a detector against detect and check it with a contract suite #140

Closed
3 of 3 issues completed
Closed

Description

@hslev

Parent

#126

Branch

Branch from stateless_core and open the pull request against it, not main. Close this ticket by hand once its pull request merges there: GitHub only closes issues automatically on merges into main.

What to build

A contributor can write a detector by implementing detect and get adetect and the three old predict* methods from BaseDetector, and can run one reusable contract suite against it. A subclass written against today's interface (only predict, predict_prompt, predict_prompt_batch) keeps working through detect, with one warning per class.

This task adds the interface and the suite before any concrete detector implements it, so every detector task after it has a suite to pass. It does not change TransformerDetector, LLMDetector or RAGFactCheckerDetector, and batch methods are not part of it.

Acceptance criteria

  • BaseDetector.detect(request, *, min_score=0.0, include_tokens=False, include_detail=False) and adetect with the same arguments exist; adetect is a coroutine function that runs detect in a worker thread, so every detector gets it from the base class and none overrides it. The adetect docstring says that it uses the event loop's default executor (min(32, cpu+4) workers), that this executor is shared with the rest of the service (DNS lookups included), and that cancelling it stops the wait, not the thread.
  • adetect returns a result equal to detect, runs the subclass hook off the event loop's thread, and leaves the loop free while it waits. Concurrent adetect calls under asyncio.gather return results in call order.
  • adetect raises the same errors as detect (same class, code and param, hook not called) from the awaited call, and an exception raised in the hook propagates unchanged with its __cause__.
  • A min_score outside [0, 1] raises InvalidInputError (invalid_min_score, param min_score) before the subclass is called; spans whose score is below min_score are dropped; tokens are not filtered.
  • include_tokens=True and include_detail=True raise InvalidInputError (tokens_not_supported, detail_not_supported) on a detector that doesn't override the support, before the subclass is called. The base class supports neither.
  • A subclass that implements only the old methods works through detect: a SourcesInput goes to predict, a PromptInput to predict_prompt, confidence becomes score, and category, subcategory and reasoning are carried. A span without confidence gets score 1.0 and the Detection has one synthetic_score warning.
  • That subclass emits one LettuceDetectDeprecationWarning per class, on its first detect call, not one per call. Its usage has one entry whose model_id is the subclass's model_id attribute (None when absent).
  • A subclass that implements only detect gets predict, predict_prompt and predict_prompt_batch from the base class: they build the request, call detect (one pair at a time for the batch method), and return today's dicts with confidence and prob taken from score.
  • The old methods raise InvalidInputError for an output_format other than "tokens" and "spans" (unsupported_output_format) and for lists of different lengths (length_mismatch), and for a min_confidence outside [0, 1] (invalid_min_score, param min_confidence).
  • adetect on a subclass that implements only the old methods returns the same result as detect, and the once-per-class deprecation warning still fires once across sync and awaited calls.
  • A subclass that implements neither detect nor predict raises TypeError when the class is defined. BaseDetector is not marked deprecated, and defining a subclass emits no warning.
  • A reusable contract suite, run here against an in-test detector and an old-style subclass, checks that:
    • every span's text equals the answer between its offsets, for SourcesInput and PromptInput
    • adetect equals detect, and the loop keeps running while it awaits
    • a call in an empty temporary directory set as working directory and HOME leaves it empty
    • a second call with the same input returns the same result, also after a call with other input
    • usage has an entry with the detector's model_id and a model_version field
    • every span has a numeric score, and min_score filters on it
    • include_tokens and include_detail raise where unsupported
    • a call prints nothing and logs no answer or source text on any logger other than lettucedetect.content
  • The existing tests pass unchanged.
  • The changed code follows the Errors and logging section of CONTRIBUTING.md, and no customer text is logged outside lettucedetect.content.

Blocked by

#134, #137

User stories covered

3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 49, 52, 53, 54, 55

Sub-issues

Sub-issues

3 of 3 Issues completed
Loading 3 sub-issues

Activity

  1. added
    ready-for-agent
    on Nov 5, 2026
  2. added theissue type on Nov 5, 2026
  3. added a commit that references this issue on Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ready-for-agent

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions