This is an automated email from the ASF dual-hosted git repository.

oscerd pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 75acda3d7b43 CAMEL-24613: camel-langchain4j-agent-api - 
LanguageGuardrail must not reject plain English with allowMixed=false
75acda3d7b43 is described below

commit 75acda3d7b4397694fc807e2ea2c23b9a8b6deb2
Author: Andrea Cosentino <[email protected]>
AuthorDate: Thu Sep 3 18:29:48 2026 +0200

    CAMEL-24613: camel-langchain4j-agent-api - LanguageGuardrail must not 
reject plain English with allowMixed=false
    
    With allowMixed=false and allowedLanguages={ENGLISH}, plain English was 
falsely rejected as mixed content because the ENGLISH and LATIN_SCRIPT patterns 
overlap on ASCII letters. Treat LATIN_SCRIPT as acceptable when ENGLISH is 
allowed.
    
    Closes #26081
    
    Co-Authored-By: Claude Opus 4.8 <[email protected]>
    Claude-Session: https://claude.ai/code/session_01SC3EbLbPUMdbqy7butoKCD
---
 .../langchain4j/agent/api/guardrails/LanguageGuardrail.java   |  7 ++++++-
 .../agent/api/guardrails/LanguageGuardrailTest.java           | 11 +++++++++++
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git 
a/components/camel-ai/camel-langchain4j-agent-api/src/main/java/org/apache/camel/component/langchain4j/agent/api/guardrails/LanguageGuardrail.java
 
b/components/camel-ai/camel-langchain4j-agent-api/src/main/java/org/apache/camel/component/langchain4j/agent/api/guardrails/LanguageGuardrail.java
index adc862100cdc..2ee237f9d9c2 100644
--- 
a/components/camel-ai/camel-langchain4j-agent-api/src/main/java/org/apache/camel/component/langchain4j/agent/api/guardrails/LanguageGuardrail.java
+++ 
b/components/camel-ai/camel-langchain4j-agent-api/src/main/java/org/apache/camel/component/langchain4j/agent/api/guardrails/LanguageGuardrail.java
@@ -206,7 +206,12 @@ public class LanguageGuardrail implements InputGuardrail {
         if (!allowMixed && detectedLanguages.size() > 1) {
             // Check if all detected languages are in allowed set
             for (Language detected : detectedLanguages) {
-                if (!allowedLanguages.contains(detected) && detected != 
Language.ENGLISH) {
+                // ENGLISH and LATIN_SCRIPT overlap (ASCII letters match 
both), so LATIN_SCRIPT must not be
+                // treated as foreign when ENGLISH is allowed - otherwise 
plain English is falsely rejected.
+                boolean acceptable = allowedLanguages.contains(detected)
+                        || detected == Language.ENGLISH
+                        || (detected == Language.LATIN_SCRIPT && 
allowedLanguages.contains(Language.ENGLISH));
+                if (!acceptable) {
                     return failure("Mixed language content is not allowed.");
                 }
             }
diff --git 
a/components/camel-ai/camel-langchain4j-agent-api/src/test/java/org/apache/camel/component/langchain4j/agent/api/guardrails/LanguageGuardrailTest.java
 
b/components/camel-ai/camel-langchain4j-agent-api/src/test/java/org/apache/camel/component/langchain4j/agent/api/guardrails/LanguageGuardrailTest.java
index 565fdd0a8198..d5a930a6a93b 100644
--- 
a/components/camel-ai/camel-langchain4j-agent-api/src/test/java/org/apache/camel/component/langchain4j/agent/api/guardrails/LanguageGuardrailTest.java
+++ 
b/components/camel-ai/camel-langchain4j-agent-api/src/test/java/org/apache/camel/component/langchain4j/agent/api/guardrails/LanguageGuardrailTest.java
@@ -166,4 +166,15 @@ class LanguageGuardrailTest {
         // All-Latin text meets the ratio
         assertTrue(guardrail.validate(UserMessage.from("Hello 
world")).isSuccess());
     }
+
+    @Test
+    void allowMixedFalseWithEnglishAllowsPlainEnglish() {
+        LanguageGuardrail guardrail = LanguageGuardrail.builder()
+                .allowedLanguages(LanguageGuardrail.Language.ENGLISH)
+                .allowMixed(false)
+                .build();
+
+        // Plain English detects both ENGLISH and the overlapping 
LATIN_SCRIPT; it must not be rejected as mixed.
+        assertTrue(guardrail.validate(UserMessage.from("Hello world, this is 
plain English.")).isSuccess());
+    }
 }

Reply via email to