From 474b0b14cbccab0099ac04dc712272c11d59b9ae Mon Sep 17 00:00:00 2001 From: Stuart Gano Date: Thu, 12 Feb 2026 12:17:09 -0800 Subject: [PATCH] fix: clamp dispatcher confidence score to valid range [0.0, 1.0] LLMs occasionally return confidence scores > 1.0 (e.g., 1.2) when highly confident in their intent classification. This causes Pydantic validation errors since the DispatcherResponse schema constrains confidence to [0, 1]. This fix clamps the confidence value to the valid range before creating the DispatcherResponse object, preventing validation errors while preserving the intent that the model was highly confident. Error observed: pydantic_core._pydantic_core.ValidationError: 1 validation error for DispatcherResponse confidence Input should be less than or equal to 1 [type=less_than_equal, input_value=1.2, input_type=float] Changes: - Added confidence clamping: min(max(float(confidence), 0.0), 1.0) - Added explanatory comment about LLM behavior Fixes dispatcher crashes during chat intent detection. Co-Authored-By: Claude Opus 4.5 --- src/backend/src/services/dispatcher_service.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/backend/src/services/dispatcher_service.py b/src/backend/src/services/dispatcher_service.py index 8c496510..b5242d17 100644 --- a/src/backend/src/services/dispatcher_service.py +++ b/src/backend/src/services/dispatcher_service.py @@ -944,9 +944,11 @@ async def dispatch( ) # Create dispatcher response + # Clamp confidence to [0.0, 1.0] range (LLM sometimes returns >1.0) + confidence = min(max(float(intent_result["confidence"]), 0.0), 1.0) dispatcher_response = DispatcherResponse( intent=IntentType(intent_result["intent"]), - confidence=intent_result["confidence"], + confidence=confidence, extracted_info=intent_result["extracted_info"], suggested_prompt=intent_result["suggested_prompt"], )