This is an automated email from the ASF dual-hosted git repository.
Croway 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 d591c2406135 CAMEL-24608: camel-langchain4j-agent-api - enforce
LanguageGuardrail minLanguageRatio
d591c2406135 is described below
commit d591c24061352149ed34b578312d098332282146
Author: Andrea Cosentino <[email protected]>
AuthorDate: Thu Sep 3 10:15:08 2026 +0200
CAMEL-24608: camel-langchain4j-agent-api - enforce LanguageGuardrail
minLanguageRatio
The minLanguageRatio option was set from the constructor and the builder
but never
read in validate(), so it silently did nothing: text that is mostly a
foreign
script but contains a single allowed-language character still passed.
Enforce it by
computing the fraction of non-whitespace characters that belong to an
allowed
language and failing when it is below the configured ratio; also add the
missing
getMinLanguageRatio() getter. The check is a no-op at the default 0.0.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ka4dAcJMpxahMfk3kmG5Ls
Signed-off-by: Andrea Cosentino <[email protected]>
---
.../agent/api/guardrails/LanguageGuardrail.java | 36 ++++++++++++++++++++++
.../api/guardrails/LanguageGuardrailTest.java | 13 ++++++++
2 files changed, 49 insertions(+)
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 bf8eea8c7b34..adc862100cdc 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
@@ -212,9 +212,38 @@ public class LanguageGuardrail implements InputGuardrail {
}
}
+ // Enforce the minimum ratio of allowed-language characters, when
configured
+ if (minLanguageRatio > 0.0) {
+ double ratio = allowedLanguageRatio(text);
+ if (ratio < minLanguageRatio) {
+ return failure(String.format(
+ "Message does not meet the minimum allowed-language
ratio (required %.2f, found %.2f).",
+ minLanguageRatio, ratio));
+ }
+ }
+
return success();
}
+ /**
+ * Fraction of the non-whitespace characters that belong to at least one
allowed language/script. Returns 1.0 when
+ * there are no non-whitespace characters.
+ */
+ private double allowedLanguageRatio(String text) {
+ long total = text.codePoints().filter(cp ->
!Character.isWhitespace(cp)).count();
+ if (total == 0) {
+ return 1.0;
+ }
+ long allowed = text.codePoints()
+ .filter(cp -> !Character.isWhitespace(cp))
+ .filter(cp -> {
+ String ch = new String(Character.toChars(cp));
+ return allowedLanguages.stream().anyMatch(lang ->
lang.isPresent(ch));
+ })
+ .count();
+ return (double) allowed / total;
+ }
+
/**
* @return the set of allowed languages
*/
@@ -229,6 +258,13 @@ public class LanguageGuardrail implements InputGuardrail {
return new HashSet<>(blockedLanguages);
}
+ /**
+ * @return the minimum required ratio of allowed-language characters (0.0
disables the check)
+ */
+ public double getMinLanguageRatio() {
+ return minLanguageRatio;
+ }
+
/**
* Builder for creating LanguageGuardrail instances.
*/
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 3c2f65737e9e..565fdd0a8198 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
@@ -153,4 +153,17 @@ class LanguageGuardrailTest {
assertTrue(guardrail.getBlockedLanguages().contains(LanguageGuardrail.Language.CYRILLIC));
}
+
+ @Test
+ void testMinLanguageRatioIsEnforced() {
+ LanguageGuardrail guardrail = LanguageGuardrail.builder()
+ .allowedLanguages(LanguageGuardrail.Language.LATIN_SCRIPT)
+ .minLanguageRatio(0.8)
+ .build();
+
+ // "Hello 你好世界": 5 Latin characters out of 9 non-whitespace ~= 0.56,
below the 0.8 threshold
+ assertFalse(guardrail.validate(UserMessage.from("Hello
你好世界")).isSuccess());
+ // All-Latin text meets the ratio
+ assertTrue(guardrail.validate(UserMessage.from("Hello
world")).isSuccess());
+ }
}