Repository navigation
Description
lettucedetect/integrations/claude_code/check_answer.py:231-235
flagged = [
s
for s in spans
if (s.get("confidence", s.get("hallucination_score")) or 0.0) >= args.min_confidence
]In --llm-model mode, the spans the hook receives never carry a confidence or hallucination_score key:
- Judge path.
detect_spans_llm(check_answer.py:153-165) constructs the detector with onlymethod,modelandbase_url, soLLMDetectorruns withinclude_reasoning=False(lettucedetect/detectors/llm.py:74). With that setting the model is never asked for a score, and_to_spans(llm.py:297-344) copies aconfidencekey onto a span only when the model returned one (llm.py:340-342). The hook's own test (tests/test_claude_code_hook_pytest.py:244-248) pins these three constructor kwargs. - Native path. The model the hook documents for this mode,
KRLabsOrg/lettucedect-v2-qwen-2b, is detected as native by_looks_native(llm.py:26-37) and served by_predict_native(llm.py:437-485). That method builds spans withgen.spans_to_offsets(lettucedetect/prompts/generative.py:103-133), which copies onlycategory,subcategoryandexplanation. No confidence is produced in any configuration.
For such a span, s.get("confidence", s.get("hallucination_score")) is None, and None or 0.0 is 0.0. Once --min-confidence is set above 0.0, which is what its --help text ("Report only spans at or above this confidence") and the README's example (--min-confidence 0.5, README.md:40) invite, every span fails 0.0 >= args.min_confidence and is dropped. main() then reaches if not flagged: return 0, which the module docstring defines as "nothing to report" (line 28). A hallucinated answer is reported to Claude Code as a clean run.
The other two modes are not affected. API spans always carry hallucination_score, a required float in SpanDetectionItem (lettucedetect_api/models.py:23-29). Transformer spans always carry confidence (lettucedetect/detectors/transformer.py:220-231).
This contradicts the library's own filter, BaseDetector._filter_spans_by_confidence (lettucedetect/detectors/base.py:74-96), which every detector's predict / predict_prompt / predict_prompt_batch uses (e.g. llm.py:557-563). Its docstring (base.py:82-83) says:
Spans without a
confidencekey are kept, mirroring the existing behaviour of the LLM detector's_to_spans.
Its implementation (base.py:91-96) keeps any span where span.get("confidence") is None. The hook's format_report also expects unscored spans and prints confidence n/a for them (check_answer.py:110-111). The hook does not use the shared filter: none of detect_spans_api, detect_spans_local or detect_spans_llm (check_answer.py:129-165) passes a threshold to the detector. main() implements its own filter with the opposite default.
Measured
Both filter bodies were copied verbatim into a standalone script: hook_filter is the comprehension at check_answer.py:231-235, and base_filter_spans_by_confidence is the body of base.py:74-96. The script imports nothing from the repository, uses no network, and feeds hand-built span lists. Both functions are pure data transforms.
| Case | Input | check_answer.py filter (actual) |
base.py._filter_spans_by_confidence (documented behaviour) |
Match? |
|---|---|---|---|---|
Degenerate: 2 hallucinated spans, no confidence key, --min-confidence 0.3 |
[{"text":"invented API"}, {"text":"wrong version"}] |
keeps 0/2 | keeps 2/2 | No |
Native-path span shape, --min-confidence 0.5 |
[{"start":0,"end":5,"text":"added","category":"unsupported_addition","subcategory":"x"}] |
keeps 0/1 | keeps 1/1 | No |
Control A: explicit low confidence, --min-confidence 0.3 |
[{"text":"maybe","confidence":0.1}] |
keeps 0/1 | keeps 0/1 | Yes |
Control B: explicit high confidence, --min-confidence 0.3 |
[{"text":"surely","confidence":0.9}] |
keeps 1/1 | keeps 1/1 | Yes |
Default threshold: no --min-confidence (0.0), no confidence key |
[{"text":"invented API"}] |
keeps 1/1 | keeps 1/1 | Yes |
No test in tests/test_claude_code_hook_pytest.py passes an unscored span through --min-confidence. test_min_confidence_filters (lines 161-168) uses a span with an explicit confidence. The --llm-model end-to-end test (line 209) stubs a span with confidence: 0.9 (line 224), a shape the real native path for KRLabsOrg/lettucedect-v2-qwen-2b does not produce.
Consequence
In --llm-model mode, any --min-confidence above 0.0 suppresses every flagged span. This covers both the generative lettucedect-v2 models and plain LLM judges, and the result looks the same as a clean answer. The user sees no warning, and the agent is never asked to revise. At the default threshold of 0.0 nothing is dropped. The --api-url and --model-path modes behave correctly.
Suggested fix
Change the post-filter in main() to follow base.py:91-96: keep a span whose score is missing instead of treating it as 0.0.
def _score(span):
return span.get("confidence", span.get("hallucination_score"))
flagged = [
s for s in spans
if args.min_confidence <= 0.0 or _score(s) is None or _score(s) >= args.min_confidence
]The local post-filter has to stay. LettuceClient.detect_spans(contexts, question, answer) (lettucedetect_api/client.py:105-107) takes no threshold, and API spans use hallucination_score rather than confidence. Forwarding min_confidence into detect_spans_local / detect_spans_llm would also work for those two modes, but it adds _validate_min_confidence, which raises ValueError for values outside [0, 1]. A regression test should run an unscored span with --min-confidence 0.5 and expect exit code 2. The --help text and README should say that --min-confidence does not apply to spans the detector did not score, which includes every span in --llm-model mode.
Happy to open the PR.
Activity
adaamko commentedon Sep 28, 2026
Thanks, confirmed on
main.check_answer.py:234treats a missing score as0.0, and neither--llm-modelpath (judge or native) produces one, so any--min-confidenceabove 0 drops every span and the hook reports a clean answer. Your suggested fix is the right one: keep unscored spans, the same rule asBaseDetector._filter_spans_by_confidence.A PR is welcome. Please include the regression test you describe (an unscored span with
--min-confidence 0.5must exit 2), the--helpand README note that the threshold only applies to scored spans, andCloses #117in the description.fix: keep unscored spans in the Claude Code hook --min-confidence filter