Repository navigation
Closed
Labels
ready-for-agentScoped and ready for a contributor or agent to pick up
Milestone
Description
Parent: #140 · Epic: #126 · Blocked by: #135, #139
Work on branch 140-implement-a-detector-against-detect-and-check-it-with-a-contract-suite; commit the changes and close the ticket.
Problem
BaseDetector has only the three predict* methods, whose result shape depends on an argument. There is no typed detect, no async variant, and min_confidence validation raises a bare ValueError.
Current behavior
lettucedetect/detectors/base.py:12predict,:33predict_prompt,:47predict_prompt_batchare abstract.base.py:65_validate_min_confidenceraisesValueError;:75_filter_spans_by_confidencekeeps spans that have noconfidencekey.
Steps
- Add
detect(request, *, min_score=0.0, include_tokens=False, include_detail=False) -> DetectiontoBaseDetector. It validates arguments, then calls a protected hook (_detect) that detectors implement. min_scoreoutside [0, 1] raisesInvalidInputError(invalid_min_score, parammin_score) before the hook runs. Spans belowmin_scoreare dropped; tokens are not.- Add class attributes
supports_tokens = Falseandsupports_detail = False.include_tokens/include_detailon an unsupported detector raiseInvalidInputError(tokens_not_supported,detail_not_supported) before the hook runs. - Add
async def adetect(request, *, min_score=0.0, include_tokens=False, include_detail=False) -> DetectiontoBaseDetector. It runsdetectin a worker thread (asyncio.to_thread), so it never blocks the event loop and every detector gets it without writing it. Detectors implement only_detect; none overridesadetect. The docstring says three things:adetectuses the event loop's default executor, which hasmin(32, cpu+4)workers (a service with many concurrent calls should size it); that executor is shared with the rest of the service (loop.getaddrinfouses it too), so a burst of long LLM calls can delay DNS lookups and otherto_threadusers; and cancellingadetectstops the wait but not the thread. There is noexecutorargument. adetectgoes throughdetect, so argument validation,min_scorefiltering and the support-flag checks apply unchanged. The errors from step 2 and 3 are raised from the awaited call (not at call time), as the same classes with the samecodeandparam; an error raised in the hook propagates unwrapped, with__cause__intact.- Add a
model_idattribute (defaultNone). - The async tests here need
asyncio_mode = "auto"under[tool.pytest.ini_options]inpyproject.toml(pytest-asynciois already in thedevextra). Add the ResponseCache protocol, make_cache_key and InMemoryCache #145 adds that line. Whichever of this ticket and Add the ResponseCache protocol, make_cache_key and InMemoryCache #145 lands first adds it, and the other finds it present.
Acceptance criteria
-
detectandadetectexist with the signature above;adetectreturns an equal result. -
adetectis a coroutine function; the hook runs on a thread other than the event loop's (test: the hook recordsthreading.get_ident()), and a loop ticker keeps running while a slow hook sleeps. - Concurrent
adetectcalls viaasyncio.gatherreturn results in call order, each equal to itsdetectresult. - The docstring of
adetectstates the three points of step 4 (default executor and its size; shared with the rest of the service; cancelling stops the wait, not the thread) and says there is noexecutorargument. - The same error cases as
detect(out-of-rangemin_score, unsupportedinclude_tokens/include_detail) raise the same class,codeandparamfromawait adetect(...), with the hook not called. - An exception raised in the hook propagates from
await adetect(...)as the same exception object. - Out-of-range
min_scoreand unsupported flags raise the listed errors before the hook is called (test with a hook that fails if called). - Spans are filtered by
score; tokens are not. - Existing subclasses are not yet affected; existing tests pass; follows CONTRIBUTING "Errors and logging".
Start here
uv run --extra dev pytest tests/test_benchmark_detectors_pytest.py tests/test_factory_pytest.py -q
Metadata
Metadata
Assignees
Labels
ready-for-agentScoped and ready for a contributor or agent to pick up
Activity
feat: add BaseDetector.detect and adetect with min_score and support …