fix(schemas): normalize confidence from 0-100 scale before Pydantic validation
Problem
LLMFinding and MetaAnalyzerFinding both declare confidence: float = Field(ge=0.0, le=1.0). When a local model (e.g. Ollama-hosted Llama or Mistral) returns confidence as an integer on a 0–100 scale ("confidence": 85), Pydantic raises a ValidationError at the le=1.0 boundary and crashes the meta-analyzer for the entire file — all findings for that file are silently dropped.
Root Cause
The constraint is evaluated after type coercion but before any user logic, so there is no opportunity to rescale at the model level.
Fix
Add a mode="before" @field_validator on confidence in both schemas:
@field_validator("confidence", mode="before")
@classmethod
def _normalize_confidence(cls, v: object) -> float:
v = float(v) # raises on non-numeric
if v > 1.0:
v = v / 100.0 # rescale 0-100 → 0-1
return max(0.0, min(1.0, v))
This also clamps negative values and values above 100 (e.g. 110) gracefully instead of crashing.
Tests
- Confidence
0.85(already valid) passes through unchanged - Confidence
85(0-100 integer) is rescaled to0.85 - Confidence
100→1.0,0→0.0 - Confidence
-5→0.0,110→1.0 - Non-numeric input raises
ValueError - Both
LLMFindingandMetaAnalyzerFindingvalidated
Closes #89
Checklist
- [x]
make lintpasses - [x] Tests added for new behaviour
- [x] DCO sign-off on commit (
git commit -s)
@Shrotriya-lalit - Please resolve the conflicts, minor issues and merge the PR.
Thanks for the review @rng1995!
Conflicts have been resolved — merged origin/main into the branch and combined both changes:
- Kept upstream's
_clamp_start_lineand the removal ofge/leField bounds (for OpenAI-compatible endpoint compatibility) - Replaced upstream's
_clamp_confidencewith themode="before"_normalize_confidencefrom this PR, which handles both the 0–100 rescaling and clamping in one pass
Regarding the 1.5 ambiguity you flagged: agreed it's an inherent edge case of the threshold approach. A model emitting 1.5 on a 0–1 scale is operating outside spec; the rescaling assumption is documented in the validator docstring. If this becomes a problem in practice a two-pass approach (try clamping first, rescale only when the value looks like an integer percentage) could be added as a follow-up.
727 tests pass, lint clean. Ready to merge.