Repository navigation
Implement a detector against detect and check it with a contract suite (#140) - #188
Merged
hslev merged 8 commits intoNov 6, 2026
Conversation
…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
…etect-and-check-it-with-a-contract-suite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Builds the detector contract on
BaseDetectorin 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) -> Detectionvalidates its arguments, calls the_detecthook and drops spans belowmin_score.adetectrunsdetectthroughasyncio.to_thread; its docstring states the shared default executor (min(32, cpu + 4)workers) and that cancelling stops the wait, not the thread. Adds thesupports_tokens,supports_detailandmodel_idattributes. Errors are typedInvalidInputErrors with the codesinvalid_min_score,tokens_not_supportedanddetail_not_supported.Bridge between old and new interfaces (Bridge old and new BaseDetector interfaces #142).
_detectgets workingpredict,predict_promptandpredict_prompt_batch.predict*methods works throughdetectandadetect. A missing confidence becomes a score of 1.0 with onesynthetic_scorewarning. It emits oneLettuceDetectDeprecationWarningper class, thread-safe.detect,_detectandpredictraisesTypeErrorwhen it is defined.predict_promptraisesInvalidInputError(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.pyis a base class that a detector task subclasses by supplying a factory and two sample requests. It checks, throughdetectandadetectonly:min_scorefiltering;usagewithmodel_idandmodel_version;HOME;lettucedetect.content;adetectequalsdetect, runs off the loop thread and keeps the loop free, and that concurrent calls return in call order;awaitmatch those fromdetect;Task.cancel()orasyncio.wait_forleaves the detector usable. The test shows the worker thread keeps running until released.It runs against a
detect-only and apredict-only stand-in, and against nine deliberately broken stand-ins that must each fail the matching check.pyproject.tomlgetsasyncio_mode = "auto"for the async tests. The existing detectors still overridepredict*, so their behaviour is unchanged.HallucinationDetectorinmodels/inference.pydoes not subclassBaseDetectorand is unaffected.Related issue
Closes #141, closes #142, closes #143. Parent: #140.
Type of change
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)uv run --extra dev pytest tests/test_detector_contract_pytest.py -q(38 passed).ty checkis clean onlettucedetect/detectors/base.pyandtests/detector_contract.py.Notes for reviewers
test_default_hook_raises_not_implemented, now buildsBaseDetector()directly. A subclass that overrides nothing is aTypeErrorafter Bridge old and new BaseDetector interfaces #142, so the old form could not exist. No test for the existing detectors changed._detectis not abstract. Making it abstract brokeLLMDetector,RAGFactCheckerDetectorandTransformerDetector, which still implement onlypredict*. The__init_subclass__guard gives the same safety for new subclasses.lettucedetect.contentlogger: nothing in the code creates it yet. The contract only exempts that name, as CONTRIBUTING describes._detect, andpredict*is not deprecated for them. Those are later tickets.stateless_coreas the target.Checklist
Rights & sign-off (required)
distributed under the repository's MIT license
(see CONTRIBUTING).